Git is an open-source version control system embraced by developers worldwide to manage code changes efficiently. It allows you to monitor and revert software changes at the source level, making it ideal for collaboration and versioning. With Git, you can track modifications, revert to previous states, and create alternative versions of files and directories.
An HTTP Git Server is a straightforward, open-source solution that uses an Nginx web server to host Git repositories over a Local Area Network (LAN). It’s a simple setup, manageable through the command-line, and perfect for those who wish to serve Git repositories internally.
This tutorial guides you through setting up an HTTP Git repository server with Nginx on Debian 11.
Prerequisites
- A server running Debian 11.
- A valid domain name configured to point to your server’s IP.
- A root password set on your server.
Install Nginx and Other Dependencies
Start by installing the Nginx web server along with additional packages necessary for setting up an HTTP Git server. Execute the following command:
apt-get install nginx git fcgiwrap apache2-utils unzip -y
After installing these packages, proceed to the next step.
Create a Git Repository
Create a directory to store your Git repository. You’ll make a directory named myrepo within the Nginx web root:
mkdir /var/www/html/myrepo
Change to the myrepo directory and create another directory for users:
cd /var/www/html/myrepo mkdir user1.git
Navigate to the user directory and initialize the Git repository:
cd user1.git git --bare init
You should see:
Initialized empty Git repository in /var/www/html/myrepo/user1.git/
Update the Git server information:
git update-server-info
Change ownership of myrepo and set appropriate permissions:
chown -R www-data:www-data /var/www/html/myrepo chmod -R 755 /var/www/html/myrepo
Create a user named user1 and set a password:
htpasswd -c /var/www/html/myrepo/htpasswd user1
Enter and confirm the password when prompted:
New password: Re-type new password: Adding password for user user1
Verify the password setup:
cat /var/www/html/myrepo/htpasswd
Expected output:
user1:$apr1$LoyCEkzA$Fjq5nBbLhBRdaxCQBBUQd1
Configure Nginx to Serve Git Repository
Next, create an Nginx virtual host configuration file to serve your Git repository:
nano /etc/nginx/conf.d/git.conf
Add the following configuration:
server { listen 80; root /var/www/html/myrepo; index index.html index.htm index.nginx-debian.html; server_name git.yourdomain.com; location / { 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. Then verify the Nginx configuration:
nginx -t
You should see:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart the Nginx service to apply the changes:
systemctl restart nginx
Check Nginx status to confirm it’s running:
systemctl status nginx
Expected output:
? nginx.service - A high performance web server and a reverse proxy server ...
Connect to Git Repository From the Client
Your Git HTTP server is now set up with Nginx. It’s time to connect from a client machine and test it.
Install Git on the client machine:
apt-get install git -y
Create a project directory:
mkdir project
Navigate to your project directory and initialize Git:
cd project git init
Configure Git with your email and username:
git config --global user.email "user1@yourdomain.com" git config --global user.name "user1"
Add your Git HTTP server:
git remote add origin http://user1@git.yourdomain.com/user1.git
Create a directory and add a file:
mkdir dev01 echo "This is my first application" > dev01/file1
Add your files to the Git repository:
git add .
Commit the changes:
git commit -a -m "Add files and directories"
Expected output:
[master (root-commit) 0299d83] Add files and directories 1 file changed, 1 insertion(+) create mode 100644 dev01/file1
Push your files to the HTTP Git server:
git push origin master
Enter your password when prompted:
Password for 'http://user1@git.yourdomain.com':
If successful, you’ll see:
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
Clone your repository from the Git server:
git clone http://user1@git.yourdomain.com/user1.git
Expected output:
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
This guide walked you through setting up an HTTP Git server with Nginx on Debian 11. You can now deploy this setup in your local development environment to efficiently manage and track your projects using Git and Nginx.
Frequently Asked Questions (FAQ)
- What is the purpose of using Nginx for a Git server?
Nginx serves as a high-performance web server that can efficiently host and manage Git repositories over a network, providing access via HTTP. - Do I need a public IP and domain to set up an HTTP Git server?
For a LAN setup, a domain isn’t strictly necessary, but having a domain makes accessing the server more convenient. - What does the ‘–bare’ option do when initializing a Git repository?
The ‘–bare’ option creates a repository that is optimized for sharing, without a working directory. It’s suitable for remote repositories like the ones on a server. - Can I manage users and authentication within Nginx?
Yes, Nginx can manage basic authentication using htpasswd files to control user access to the Git repositories.