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

  1. Update your system’s package list:

    sudo apt update && sudo apt upgrade -y

  2. Install necessary dependencies:

    sudo apt install -y git mariadb-server mariadb-client

  3. Create a dedicated user for Gitea:

    sudo adduser --system --group --disabled-login gitea


Step 2: Setting Up the Database

  1. Secure your MariaDB installation:

    sudo mysql_secure_installation

  2. Log into MariaDB:

    sudo mysql -u root -p

  3. 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

  1. Download the latest Gitea release:

    wget -O gitea https://dl.gitea.io/gitea/latest/gitea-$(uname -m)

  2. Move it to a system-wide location:

    sudo mv gitea /usr/local/bin

  3. Make the binary executable:

    sudo chmod +x /usr/local/bin/gitea


Step 4: Configuring Gitea

  1. 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/

  2. Create a systemd service file:

    sudo nano /etc/systemd/system/gitea.service

  3. 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

  4. Reload systemd and start Gitea:

    sudo systemctl daemon-reload

    sudo systemctl enable --now gitea


Step 5: Configuring Nginx as a Reverse Proxy (Optional)

  1. Install Nginx:

    sudo apt install -y nginx

  2. Create an Nginx configuration file:

    sudo nano /etc/nginx/sites-available/gitea

  3. 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;

    }

    }

  4. 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

  1. Open your browser and visit http://yourdomain.com

  2. Complete the web-based setup by entering:

    Database type: MySQL

    Database credentials

    Admin account details

    Repository root directory

  3. 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.