Operating System Debian

How to Set Up an HTTP Git Server With Nginx on Debian 13

Set up Git smart HTTP on Debian 13 with Nginx, fcgiwrap and git-http-backend, protected by a password and TLS, then clone and push over HTTPS.

This guide turns a Debian 13 server into a private Git server that speaks Git's "smart" HTTP protocol. Nginx accepts the requests and checks the password, fcgiwrap runs Git's own git-http-backend CGI program, and you clone and push with ordinary https:// URLs. The same steps work on Debian 12, which only differs in shipping older Git and Nginx packages.

Debian 8 and the other releases older tutorials were written for are end-of-life. The method itself has not changed, but the socket path, the service handling and the need for TLS have.

Requirements

  • A server running Debian 13 (or Debian 12).
  • A user with sudo rights; the examples use deploy.
  • A DNS name pointing to the server. The examples use git.example.com.
  • Ports 80 and 443 open in your firewall.

Step 1: Install the packages

sudo apt update
sudo apt install nginx git fcgiwrap apache2-utils

fcgiwrap is the bridge between Nginx, which cannot run CGI programs itself, and git-http-backend. apache2-utils provides the htpasswd command; it does not install the Apache web server.

On Debian, fcgiwrap is socket-activated by systemd: it listens on /run/fcgiwrap.socket and runs as the www-data user. Confirm that the socket is active:

systemctl status fcgiwrap.socket

Step 2: Create the repository directory

All repositories live under one directory owned by www-data, because that is the account that will read and write them:

sudo mkdir -p /var/www/git
sudo chown www-data:www-data /var/www/git

Create a first bare repository as that user. -b main sets the name of its initial branch:

sudo -u www-data git init --bare -b main /var/www/git/project.git

Always create repositories with sudo -u www-data. A repository created as root cannot be pushed to, and Git refuses to serve repositories owned by a different user than the one running it.

Step 3: Create the password file

Store the password file outside the repository directory so it can never be served. Create it with the first user:

sudo htpasswd -c /etc/nginx/git.htpasswd deploy

Add further users without -c, which would overwrite the file:

sudo htpasswd /etc/nginx/git.htpasswd alice

Restrict the file to root and the Nginx worker user:

sudo chown root:www-data /etc/nginx/git.htpasswd
sudo chmod 640 /etc/nginx/git.htpasswd

Step 4: Configure Nginx

Create a site file:

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

Add the following server block:

server {
    listen 80;
    listen [::]:80;
    server_name git.example.com;

    # Pushes can be large; do not let Nginx reject them.
    client_max_body_size 0;

    location ~ ^/git(/.*)$ {
        auth_basic "Git";
        auth_basic_user_file /etc/nginx/git.htpasswd;

        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
        fastcgi_param GIT_PROJECT_ROOT /var/www/git;
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param PATH_INFO $1;
        fastcgi_param REMOTE_USER $remote_user;
        fastcgi_pass unix:/run/fcgiwrap.socket;
    }
}

What the important lines do:

  • location ~ ^/git(/.*)$ sends every URL below /git/ to Git and captures the rest of the path.
  • SCRIPT_FILENAME is the program fcgiwrap executes. Debian installs it at /usr/lib/git-core/git-http-backend.
  • GIT_PROJECT_ROOT and PATH_INFO are joined by git-http-backend to find the repository on disk, so /git/project.git maps to /var/www/git/project.git.
  • GIT_HTTP_EXPORT_ALL allows every repository under the root to be served. Without it, only repositories containing a file named git-daemon-export-ok are exported.
  • REMOTE_USER tells Git that the web server authenticated the user. Git only accepts pushes over HTTP from authenticated users, and it records this name in the repository's reflog.

Enable the site, test the configuration and reload Nginx:

sudo ln -s /etc/nginx/sites-available/git /etc/nginx/sites-enabled/git
sudo nginx -t
sudo systemctl reload nginx

Step 5: Add HTTPS

Basic authentication sends the password with every request, so do not use this server over plain HTTP. If the DNS name already points to the server, Certbot can obtain a Let's Encrypt certificate and adjust the server block for you:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d git.example.com

Choose the redirect option when asked so that HTTP requests are sent to HTTPS.

Step 6: Clone and push

On your workstation, clone the empty repository. Git asks for the user name and password from Step 3:

git clone https://git.example.com/git/project.git
cd project

Make a first commit and push it:

echo "# Project" > README.md
git add README.md
git commit -m "Initial commit"
git push -u origin main

To push an existing local repository instead, add the server as a remote:

git remote add origin https://git.example.com/git/project.git
git push -u origin main

To avoid typing the password on every operation, enable a credential helper on the workstation, for example git config --global credential.helper cache, which keeps it in memory for a short time.

Adding repositories

Every new repository is one command on the server; no Nginx change is needed:

sudo -u www-data git init --bare -b main /var/www/git/another.git

All users in the password file can read and write all repositories. If you need per-repository permissions, add separate location blocks with different password files, or use a Git hosting application instead.

Troubleshooting

  • 502 Bad Gateway: Nginx cannot reach fcgiwrap. Check systemctl status fcgiwrap.socket fcgiwrap.service and that the socket path in the configuration is /run/fcgiwrap.socket.
  • 403 on push, clone works: REMOTE_USER is not being passed, so Git treats the request as anonymous and refuses the push.
  • "repository not found" or 404: the path after /git/ does not match a directory under /var/www/git, or the repository is not owned by www-data. Fix ownership with sudo chown -R www-data:www-data /var/www/git/project.git.
  • Large pushes fail with 413: client_max_body_size 0; is missing from the server block.
  • Nginx errors are in /var/log/nginx/error.log; messages from fcgiwrap and Git appear in journalctl -u fcgiwrap.

Frequently asked questions

What is the difference between smart and dumb HTTP?

The dumb protocol serves repository files as static downloads and is read-only and slow. Smart HTTP, provided by git-http-backend, negotiates with the client like the SSH transport does, transfers only what is needed and supports pushing. This setup uses smart HTTP only.

Can I allow anonymous clones but require a password for pushing?

Yes. Authentication has to be requested only for push requests, which are those for git-receive-pack. The git-http-backend manual shows the URL patterns to match; in Nginx that means a second location without auth_basic for the read-only requests.

Should I use HTTP or SSH for a private Git server?

SSH needs no web server and uses keys, which suits a few administrators who already have shell accounts. HTTPS passes through firewalls and proxies that block SSH and lets you give people Git access without giving them a system account.

Does this give me a web interface?

No. git-http-backend only serves Git clients. For browsing repositories in a browser, add gitweb or cgit, or install a full hosting application such as Forgejo or Gitea.

Share story:

Continue reading other guides on How VPS.