Step-by-Step: Setting Up a Private Git Server on Raspberry Pi 5 with Gitea
GitHub and GitLab are fantastic, but there are times when hosting your code privately and locally is essential for privacy, speed, and learning. Thanks to the massive performance boost of the **Raspberry Pi 5**, setting up a reliable, self-hosted Git server is now easier and more powerful than ever before.
This tutorial is designed for developers, hobbyists, and IT professionals who need a secure, low-power repository manager. We will use **Gitea**, a fast, open-source, and extremely lightweight alternative to GitLab that is perfectly suited for the Pi 5's ARM architecture.
Phase 1: Raspberry Pi 5 Preparation and Security Baseline
Before installing any server software, we must ensure the Pi 5 is configured securely and reliably. These steps are mandatory for a persistent server setup.
1. Update the System and Install Dependencies
Always start by ensuring your Raspberry Pi OS is fully up-to-date and installing the required components: Git and SQLite3.
sudo apt update
sudo apt upgrade -y
sudo apt install git sqlite3 -y
(Note on SQLite: We use SQLite as the default database. It's excellent for low-traffic, single-server applications like this due to its minimal resource usage. If you anticipate high traffic or need advanced features, you could opt for PostgreSQL instead.)
2. Set a Static IP Address
A server needs a consistent address. You cannot rely on DHCP (dynamic IP) or your server's access will break every time your router reboots.
Configure a static IP address for the Pi 5 directly in your router or by editing the /etc/dhcpcd.conf file.
Expert Tip: Always reboot the Pi after setting the static IP to ensure the change takes effect: sudo reboot.
Phase 2: Create a Dedicated Gitea System User
For security and adherence to Linux best practices, Gitea must run under its own, non-privileged user account. This prevents unauthorized access to the root operating system if the Gitea process is ever compromised.
1. Create the Git User and Home Directory
Use the following command to create a new system user named git. The flags ensure it has no login shell and cannot access any files outside its designated directory.
sudo adduser \
--system \
--shell /bin/bash \
--gecos 'Git Version Control' \
--group \
--disabled-password \
--home /home/git \
git
2. Create Necessary Directories
Gitea needs specific folders for its configuration, logs, and database. We create these and grant ownership to the new git user.
sudo mkdir -p /var/lib/gitea/{custom,data,log}
sudo chown -R git:git /var/lib/gitea/
sudo chmod -R 750 /var/lib/gitea/
sudo mkdir /etc/gitea
sudo chown root:git /etc/gitea
sudo chmod 770 /etc/gitea
Phase 3: Install Gitea Binary and Service File
Since the Raspberry Pi 5 runs a 64-bit operating system (ARM64 or aarch64), we must download the correct binary file directly from the Gitea releases page to ensure optimal performance.
1. Download the ARM64 Binary
As of the time of writing, the latest stable version of Gitea is 1.21.7 (version numbers will need updating for future compatibility). We use the `wget` command to grab the correct binary.
# Replace 1.21.7 with the latest stable version if necessary
wget -O gitea https://dl.gitea.io/gitea/1.21.7/gitea-1.21.7-linux-arm64
sudo mv gitea /usr/local/bin
sudo chmod +x /usr/local/bin/gitea
2. Configure the Systemd Service
To ensure Gitea starts automatically every time your Pi 5 reboots and runs securely as the `git` user, we create a systemd service file. This is crucial for server stability.
Use sudo nano /etc/systemd/system/gitea.service to create the file and paste the following content exactly:
[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
# If using PostgreSQL: After=postgresql.service
# If using MySQL: After=mysql.service
[Service]
# Modify these to fit your setup
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_CUSTOM=/var/lib/gitea
[Install]
WantedBy=multi-user.target
3. Start and Enable the Service
Now, we tell the system to load the new service and start Gitea immediately.
sudo systemctl daemon-reload
sudo systemctl enable gitea
sudo systemctl start gitea
You can check the status of your new Git server with sudo systemctl status gitea. It should show a status of **active (running)**.
Phase 4: Gitea Initial Web Configuration
Gitea is now running on your Raspberry Pi 5, ready for the final setup via its web interface. It runs on the default HTTP port: 3000.
1. Access the Web Installer
From any computer on your local network, open a web browser and navigate to the static IP address you set earlier, followed by port 3000. (e.g., http://192.168.1.50:3000)
2. Fill Out the Initial Configuration
On the initial setup page, configure the following key settings:
- **Database Type:** SQLite3
- **SQLite3 Path:**
/var/lib/gitea/data/gitea.db - **Application Name:** Your-Website-Git-Server
- **Repository Root Path:**
/home/git/gitea-repositories - **SSH Port:**
22(Standard Pi SSH port) - **Gitea HTTP Listen Port:**
3000 - **Gitea Base URL:**
http://[Your Static IP]:3000/(Use your Static IP address here)
Click **"Install Gitea"** to finalize the setup. You will be redirected to the login screen.
3. Create the Administrator Account
The first account created after installation becomes the main administrator. Ensure you set a strong, unique password for this account before creating any other users.
Phase 5: Secure Remote Access via SSH
The standard way to push and pull code is via SSH. This is the last and most important security step.
1. Ensure SSH is Working
On your development computer, you need to configure an SSH key to access the Gitea server. In your Gitea web interface, navigate to the administrator settings and find the option to **Add SSH Key**.
2. Generate and Upload Your SSH Key
- On your local development machine (Windows, macOS, or Linux), generate a new key pair:
ssh-keygen -t ed25519 -C "your_email@example.com" - Copy the public key content (usually from
~/.ssh/id_ed25519.pub) and paste it into the Gitea SSH key field.
You can now use Git commands on your development machine to push and pull code using SSH authentication. For example:
git remote add origin git@<Static IP>:<Admin User>/<Repository Name>.git
Your self-hosted, private Git server on the powerful Raspberry Pi 5 is now fully operational, secure, and ready to handle all your private code projects with speed and reliability.
Frequently Asked Questions (FAQs)
- Q: Why use the Raspberry Pi 5 instead of an older model?
- A: The Pi 5’s significantly upgraded quad-core CPU and faster I/O (often bottlenecked on older Pis) provide the stability and speed needed for a reliable server. It handles the Gitea web interface and background operations much smoother than a Pi 4, especially when multiple users are active.
- Q: Is Gitea secure for remote access outside my network?
- A: Gitea is highly secure, but opening any server to the public internet requires careful configuration. You must set up **port forwarding** on your router (port 22 for SSH and port 3000 for HTTPS) and use a **Dynamic DNS (DDNS)** service if your ISP assigns dynamic IP addresses. Always use HTTPS (which requires a certificate) for web access, not just HTTP.
- Q: How do I back up my Gitea data?
- A: You can use Gitea’s built-in command-line backup utility. Log in as the
gituser and run/usr/local/bin/gitea dump. This will create a compressed archive of your entire database and all repositories, which you should regularly copy to an external drive or cloud storage.
0 टिप्पणियाँ