How to Install HTTP Git Server with Nginx on Ubuntu 20.04
This tutorial exists for these OS versions
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 18.04 (Bionic Beaver)
- Ubuntu 16.04 (Xenial Xerus)
On this page
Git is a free and open-source versioning system developed by Linus Torvalds. It is used by millions of developers around the world. GitHub also offers free code hosting service. However, the free service doesn’t allow private hosting of the code. In this case, you can host your own code hosting server with Git HTTP server. This will give you full control on the server.
In this tutorial, we will show you how to install and configure Git HTTP server with Nginx on Ubuntu 20.04.
Prerequisites
- A server running Ubuntu 20.04.
- Valid domain name pointed with your server IP.
- A root password is configured the server.
Getting Started
Before starting, it is recommended to update your server packages to the latest version. You can update them with the following command:
apt-get update -y
Once all the packages are updated, you can proceed to the next step.
Install Nginx and Git
Next, you will need to install the Nginx web server, Git and other required packages to your system. You can install them with 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 Git repository inside the Nginx web root directory. First, create a directory named git with the following command:
mkdir /var/www/html/git
Next, change the directory to git and create a new directory for Git repository:
cd /var/www/html/git
mkdir gituser.git
Next, Change into this new directory and initialize the Git repository with the command:
git --bare init
Next, update the Git Server with the command:
git update-server-info
Next, set the ownership and permission on the git directory with the following command:
chown -R www-data:www-data /var/www/html/git
chmod -R 755 /var/www/html/git
Next, create a new gituser for authentication with the following command;
htpasswd -c /var/www/html/git/htpasswd gituser
You will be asked to set a password as shown below:
New password: Re-type new password: Adding password for user gituser
You can now verify your password using the following command:
cat /var/www/html/git/htpasswd
You should get the following output:
gituser:$apr1$iPKZDbFB$ziRRbGXzVMMHaPYOtL05m/
Configure Nginx for Git
Next, you will need to configure Nginx to serve the Git repository. You can create a new virtual host configuration file for Git with the following command:
nano /etc/nginx/conf.d/git.conf
Add the following lines:
server { listen 80; root /var/www/html/git; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name git.example.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/git/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/git; 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 with the following command:
nginx -t
You should 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
Next, restart the Nginx service to apply the changes:
systemctl restart nginx
You can also verify the status of the Nginx service using the following command:
systemctl status nginx
You should 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 Tue 2020-11-17 07:43:46 UTC; 4s ago Docs: man:nginx(8) Process: 3240 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 3256 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 3257 (nginx) Tasks: 3 (limit: 4691) Memory: 3.5M CGroup: /system.slice/nginx.service ??3257 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ??3258 nginx: worker process ??3259 nginx: worker process Nov 17 07:43:46 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server... Nov 17 07:43:46 ubuntu2004 systemd[1]: Started A high performance web server and a reverse proxy server.
Once you are finished, you can proceed to the next step.
Test HTTP Git Server
At this point, Git server is installed and configured. Now, its time to test it.
On the client machine, install the Git package with the following command:
apt-get install git -y
Once installed, create a directory named myapp with the following command:
mkdir myapp
Next, change to the new directory and initialize the Git with the following command:
cd myapp
git init
Next, add your remote Git repository with the following command:
git remote add origin http://[email protected]/gituser.git
Next, create app1 and app2 directory and also create app1 and app2 files with some contents inside those directories:
mkdir app1 app2
echo "This is my first application" > app1/app1
echo "This is my first application" > app2/app2
Next, add all directories and files to the repository with the following command:
git add .
Next, commit the changes with the following command:
git commit -a -m "Add files and directories"
You should get the following output:
[master (root-commit) 4e90372] Add files and directories 2 files changed, 2 insertions(+) create mode 100644 app1/app1 create mode 100644 app2/app2
Next, push these changes to the remote Git server with the following command:
git push origin master
You will be asked to provide a password for remote Git user as shown below:
Password for 'http://[email protected]':
Provide your password and hit Enter. You should get the following output:
Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (5/5), 354 bytes | 0 bytes/s, done. Total 5 (delta 0), reused 0 (delta 0) To http://[email protected]/gituser.git * [new branch] master -> master
The above output indicate that your files and directories are added to the remote Git repository. If you want to clone this repository to your local system, run the following command:
git clone http://[email protected]/gituser.git
You should get the following output:
Cloning into 'gituser'... Password for 'http://[email protected]': remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 5 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (5/5), done. Checking connectivity... done.
Conclusion
Congratulations! you have successfully installed and setup the Git HTTP server on Ubuntu 20.04 server. You can now implement theGit server in your development environment that is accessible from within your LAN.