Git is an open source version control system used by thousands of developers around the world to track your software changes at the source code level. It allows you to track changes, revert them to a previous stage and create alternate versions of files and directories.

HTTP Git Server is an open source project that uses an Nginx web server to serve Git repositories over a local area network (LAN). It’s simple and easy to set up, and anyone can manage it from a command-line interface.

In this tutorial, I’ll explain how to set up an HTTP Git repository server on Debian 11 using Nginx.

Prerequisites

  • A server running Debian 11.
  • A valid domain name pointing to your server’s IP.
  • The root password configured on your server.

Installing Nginx and Other Dependencies

First, you need to install the Nginx web server and other required packages to set up your HTTP Git server. You can install all of these using the following command.

1
apt-get install nginx git fcgiwrap apache2-utils unzip -y

After installing all the packages, you can continue to the next step.

Creating a Git Repository

Next, you need to create a directory to store your Git repository. Let’s create a directory called myrepo in the Nginx web root.

1
mkdir /var/www/html/myrepo

Next, change the directory to myrepo and create another directory for the user.

1
2
cd /var/www/html/myrepo 
mkdir user1.git

Next, navigate to the user directory and initialize the Git repository with the following command.

1
2
cd user1.git 
git --bare init

You will get the following outputs.

1
Initialized empty Git repository in /var/www/html/myrepo/user1.git/

Next, update the Git server information with the following command.

1
git update-server-info

Next, change the ownership of myrepo and set the appropriate permissions using the following command.

1
2
chown -R www-data:www-data /var/www/html/myrepo 
chmod -R 755 /var/www/html/myrepo

Next, create a user named user1 and set the password.

1
htpasswd -c /var/www/html/myrepo/htpasswd user1

You can set a password as follows.

1
2
3
New password: 
Re-type new password: 
Adding password for user user1

You can check your password using the following command.

1
cat /var/www/html/myrepo/htpasswd

Example output.

1
user1:$apr1$LoyCEkzA$Fjq5nBbLhBRdaxCQBBUQd1

Configuring Nginx to Serve a Git Repository

Next, you need to create an Nginx virtual host profile to serve the Git repository.

1
nano /etc/nginx/conf.d/git.conf

Add the following lines.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
server {
        listen 80;

        root /var/www/html/myrepo;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name git.yourdomain.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

location ~ (/.*) {
    client_max_body_size 0; 
    auth_basic "Git Login"; 
    auth_basic_user_file "/var/www/html/myrepo/htpasswd";
    include /etc/nginx/fastcgi_params; 
    fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; 
    fastcgi_param GIT_HTTP_EXPORT_ALL "";
    fastcgi_param GIT_PROJECT_ROOT /var/www/html/myrepo;
    fastcgi_param REMOTE_USER $remote_user;
    fastcgi_param PATH_INFO $1; 
    fastcgi_pass  unix:/var/run/fcgiwrap.socket;
}

}

When you are done, save and close the file, then verify that Nginx does not have any syntax errors.

1
nginx -t

You will get the following outputs.

1
2
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart the Nginx service to apply the changes.

1
systemctl restart nginx

You can also check Nginx status using the following command.

1
systemctl status nginx

You will get the following outputs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-12-11 08:00:04 UTC; 2s ago
       Docs: man:nginx(8)
    Process: 144985 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 144986 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 144987 (nginx)
      Tasks: 2 (limit: 2341)
     Memory: 2.5M
        CPU: 42ms
     CGroup: /system.slice/nginx.service
             ??144987 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??144988 nginx: worker process

Dec 11 08:00:04 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Dec 11 08:00:04 debian11 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Dec 11 08:00:04 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.

Connecting to a Git Repository from a Client

This completes the Git HTTP server and Nginx build. Now, it’s time to connect to it from the client machine and test it.

First, install the Git package on the client machine using the following command.

1
apt-get install git -y

Next, create a directory for your project using the following command.

1
mkdir project

Next, navigate to your project directory and initialize Git with the following command.

1
2
cd project
git init

Next, configure Git with your email and user name:

1
2
git config --global user.email " user1@yourdomain.com " 
git config --global user.name "user1"

Next, add your Git HTTP server using the following command.

1
git remote add origin http: //user1@git.yourdomain.com /user1.git

Next, create a directory named dev01 and add a file to it.

1
2
mkdir dev01
echo "This is my first application" > dev01/file1

Next, add the directories and files you created to your Git repository.

1
git add .

Next, commit the changes using the following command.

1
git commit -a -m "Add files and directories"

You will get the following outputs.

1
2
3
[master (root-commit) 0299d83] Add files and directories
 1 file changed, 1 insertion(+)
 create mode 100644 dev01/file1

Next, upload your files and directories to the HTTP Git server using the following command.

1
git push origin master

You will be asked to provide a password to access the Git server.

1
Password for 'http://user1@git.yourdomain.com': 

After connecting, you will get the following output.

1
2
3
4
5
Counting objects: 4, done.
Writing objects: 100% (4/4), 281 bytes | 281.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To http://git.yourdomain.com/user1.git
 * [new branch]      master -> master

You can also download your repository directly from the Git server using the following command.

1
git clone http: //user1@git.yourdomain.com /user1.git

You will get the following outputs.

1
2
3
4
5
6
Cloning into 'user1'...
Password for 'http://user1@git.yourdomain.com': 
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.

Conclusion

In the above guide, you learned how to set up an HTTP Git server on Debian 11 using Nginx, and you can now implement this setup in your local development environment and use the command line to manage and track your projects.