Building a Self-Hosted Git Server with Gitea
Introduction
Gitea is a lightweight and self-hosted Git service that allows you to run your own version control server. It is a great alternative to GitHub and GitLab for those who want full control over their repositories. This guide will walk you through setting up Gitea on a VPS.
Step 1: Preparing Your VPS
-
Update your system’s package list:
sudo apt update && sudo apt upgrade -y
-
Install necessary dependencies:
sudo apt install -y git mariadb-server mariadb-client
-
Create a dedicated user for Gitea:
sudo adduser --system --group --disabled-login gitea
Step 2: Setting Up the Database
-
Secure your MariaDB installation:
sudo mysql_secure_installation
-
Log into MariaDB:
sudo mysql -u root -p
-
Create a database and user for Gitea:
CREATE DATABASE gitea;
CREATE USER 'giteauser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON gitea.* TO 'giteauser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 3: Installing Gitea
-
Download the latest Gitea release:
wget -O gitea https://dl.gitea.io/gitea/latest/gitea-$(uname -m)
-
Move it to a system-wide location:
sudo mv gitea /usr/local/bin
-
Make the binary executable:
sudo chmod +x /usr/local/bin/gitea
Step 4: Configuring Gitea
-
Create necessary directories:
`sudo mkdir -p /var/lib/gitea/{custom,data,log}
sudo chown -R gitea:gitea /var/lib/gitea/
sudo chmod -R 750 /var/lib/gitea/
-
Create a systemd service file:
sudo nano /etc/systemd/system/gitea.service
-
Add the following content:
[Unit]
Description=Gitea
After=network.target
[Service]
RestartSec=2s
Type=simple
User=gitea
Group=gitea
WorkingDirectory=/var/lib/gitea
ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini
Restart=always
Environment=USER=gitea HOME=/var/lib/gitea
[Install]
WantedBy=multi-user.target
-
Reload systemd and start Gitea:
sudo systemctl daemon-reload
sudo systemctl enable --now gitea
Step 5: Configuring Nginx as a Reverse Proxy (Optional)
-
Install Nginx:
sudo apt install -y nginx
-
Create an Nginx configuration file:
sudo nano /etc/nginx/sites-available/gitea
-
Add the following configuration:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
-
Enable the configuration and restart Nginx:
sudo ln -s /etc/nginx/sites-available/gitea /etc/nginx/sites-enabled/
sudo systemctl restart nginx
Step 6: Completing the Gitea Setup
-
Open your browser and visit
http://yourdomain.com
-
Complete the web-based setup by entering:
Database type: MySQL
Database credentials
Admin account details
Repository root directory
-
Click “Install Gitea”
Conclusion
You have successfully set up a self-hosted Git server using Gitea. This gives you full control over your repositories, offering a lightweight alternative to hosted solutions. For enhanced security, consider enabling HTTPS with Let’s Encrypt.