How to Install HTTP Git Server with Nginx on Debian 11

Git is an open-source version control system used by thousands of developers around the world. It is used to keep track of your software changes at the source level. It allows you to track changes, revert it back to previous stages and create an alternate version of files and directories.

HTTP Git Server is an open-source project that uses an Nginx webserver to serve Git repositories over your Local Area Network (LAN). It is very simple and easy to set up. Anyone can manage it from the command-line interface.

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

Prerequisites

  • A server running Debian 11.
  • A valid domain name pointed with your server IP.
  • A root password is configured on your server.

Install Nginx and Other Dependencies

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

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

Once all the packages are installed, you can proceed to the next step.

Create a Git Repository

Next, you will need to create a directory to store the Git repository. Let's create a directory called myrepo inside the Nginx web root directory:

mkdir /var/www/html/myrepo

Next, change the directory to myrepo and create another directory for users:

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

Next, navigate to the user directory and initialize the Git repository using the following command:

cd user1.git
git --bare init

You will get the following output:

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

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

git update-server-info

Next, change the ownership of myrepo and set proper permission with the following command:

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

Next, create a user called user1 and set a password:

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

You can set the password as shown below:

New password: 
Re-type new password: 
Adding password for user user1

You can check your password using the following command:

cat /var/www/html/myrepo/htpasswd

Sample output:

user1:$apr1$LoyCEkzA$Fjq5nBbLhBRdaxCQBBUQd1

Configure Nginx to Serve Git Repository

Next, you will need to create an Nginx virtual host configuration file to serve Git repository.

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

Add the following lines:

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;
}

}

Save and close the file when you are finished then verify the Nginx for any syntax error:

nginx -t

You will get the following output:

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:

systemctl restart nginx

You can also check the Nginx status using the following command:

systemctl status nginx

You will get the following output:

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

Connect to Git Repository From the Client

At this point, the Git HTTP server is set up with Nginx. Now, it's time to connect it from the client machine and test it.

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

apt-get install git -y

Next, create a directory for your project with the following command:

mkdir project

Next, navigate to your project directory and initialize the Git using the command below:

cd project
git init

Next, configure Git using your email and username:

git config --global user.email "[email protected]"
git config --global user.name "user1"

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

git remote add origin http://[email protected]/user1.git

Next, create a directory called dev01 and add a file inside it:

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

Next, add your created directory and file to the Git repository:

git add .

Next, commit the changes with the following command:

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

You will get the following output:

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

Next, upload your file and directory to the HTTP Git server using the following command:

git push origin master

You will be asked to provide your password to access the Git server:

Password for 'http://[email protected]': 

Once you are connected, you will get the following output:

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 from the Git server directly using the following command:

git clone http://[email protected]/user1.git

You will get the following output:

Cloning into 'user1'...
Password for 'http://[email protected]': 
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 with Nginx on Debian 11. You can now implement this setup to your local development environment and manage and track your project using the command line.

Share this page:

0 Comment(s)