Gitea Self-Hosted Git Server on Linux with HTTPS and Backups
Platform

Gitea Self-Hosted Git Server on Linux with HTTPS and Backups

  • AuthorLiam K.
  • Date
  • Time18 minutes

Gitea is a lightweight, self-hosted Git service that feels familiar if you already use GitHub or GitLab. It runs well on a single VPS, supports issues and pull requests, and stays lean enough for teams that do not need a full DevOps platform. This guide installs Gitea behind Nginx with PostgreSQL, HTTPS, and backups you can actually restore.

Prerequisites

  • Ubuntu 22.04+ or Debian 12 with at least 2 CPU and 2 GB RAM
  • Domain DNS A/AAAA records for git.example.com
  • Ports 22, 80, and 443 open; Gitea SSH can use a dedicated port if needed
  • Root or sudo access

Step 1: Create the Gitea System User

bash
sudo adduser --system --shell /bin/bash --gecos 'Git Version Control' --group --disabled-password --home /home/git git
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 -p /etc/gitea
sudo chown root:git /etc/gitea
sudo chmod 770 /etc/gitea

Step 2: Install PostgreSQL and Create the Database

bash
sudo apt update
sudo apt install -y postgresql postgresql-contrib
sudo -u postgres psql <<'EOF'
CREATE USER gitea WITH PASSWORD 'change-me-strong';
CREATE DATABASE giteadb OWNER gitea;
\\q
EOF

Step 3: Download and Install Gitea

bash
GITEA_VERSION=1.22.6
sudo wget -O /usr/local/bin/gitea https://dl.gitea.com/gitea/\${GITEA_VERSION}/gitea-\${GITEA_VERSION}-linux-amd64
sudo chmod +x /usr/local/bin/gitea
gitea --version

Step 4: Create the systemd Service

bash
sudo tee /etc/systemd/system/gitea.service >/dev/null <<'EOF'
[Unit]
Description=Gitea
After=network.target postgresql.service
Wants=postgresql.service

[Service]
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_WORK_DIR=/var/lib/gitea

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable --now gitea
sudo systemctl status gitea --no-pager

Step 5: Initial Web Setup

Open http://YOUR_SERVER_IP:3000 and complete the installer. Choose PostgreSQL, set the database user and password from Step 2, set the domain to git.example.com, and keep HTTP listen on 127.0.0.1:3000 after the reverse proxy is in place.

bash
sudo sed -i 's/^HTTP_ADDR.*/HTTP_ADDR = 127.0.0.1/' /etc/gitea/app.ini
sudo systemctl restart gitea

Step 6: Nginx Reverse Proxy with TLS

bash
sudo apt install -y nginx certbot python3-certbot-nginx
sudo tee /etc/nginx/sites-available/gitea >/dev/null <<'EOF'
server {
    listen 80;
    server_name git.example.com;

    location / {
        proxy_pass http://127.0.0.1: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;
        proxy_set_header X-Forwarded-Proto $scheme;
        client_max_body_size 512M;
    }
}
EOF
sudo ln -sf /etc/nginx/sites-available/gitea /etc/nginx/sites-enabled/gitea
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d git.example.com --non-interactive --agree-tos -m admin@example.com --redirect

Step 7: Backup and Restore

bash
# Daily dump of database + repositories
sudo -u postgres pg_dump giteadb | gzip > /var/backups/giteadb-$(date +%F).sql.gz
sudo tar -czf /var/backups/gitea-data-$(date +%F).tar.gz -C /var/lib/gitea data

# Restore example
gunzip -c /var/backups/giteadb-2026-08-03.sql.gz | sudo -u postgres psql giteadb
sudo tar -xzf /var/backups/gitea-data-2026-08-03.tar.gz -C /var/lib/gitea
sudo chown -R git:git /var/lib/gitea/data
sudo systemctl restart gitea

Production Checklist

  • Bind Gitea to localhost and expose only Nginx and SSH.
  • Rotate the PostgreSQL password and store it in a secrets manager or sealed config.
  • Enable 2FA for admin accounts and restrict registration if the instance is private.
  • Test a restore at least once per quarter before you need it.
  • Monitor disk growth under /var/lib/gitea/data and PostgreSQL.

"A Git server is only trustworthy when the database, repositories, and HTTPS edge are backed up and restorable as one unit."

Technical Author

Liam K.

Liam K.

Founder of Trivox. Writes about how the platform, the network, and the Amsterdam sites fit together.