How to Install HTTP Git Server with Nginx on Ubuntu 18.04 LTS
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 version control system that can be used to track changes of code. Git allows you to create many repositories for the same application and coordinating work on those files among multiple people. It is primarily used for source code management in software development.
In this article, we will learn how to install an HTTP Git Server With Nginx on Ubuntu 18.04 LTS (Bionic Beaver).
Requirements
- Fresh Ubuntu 18.04 server installed on your system.
- Sudo user with root privileges.
- A static IP address is configured on your server, I use the IP 192.168.1.100 in this tutorial.
1 Getting Started
Before starting, you will need to update your system with the latest stable version.
You can do this by running the following command:
sudo apt-get update -y
sudo apt-get upgrade -y
Once your system is updated, restart your system and log in with sudo user.
2 Install Required Packages
First, you will need to install some required packages including nginx, git, nano, and fcgiwrap to your system. You can install all of them by running the following command:
sudo apt-get install nginx git nano fcgiwrap apache2-utils -y
Once all the required packages are installed, you will need to create a directory for Git repository. You can do this by running the following command:
sudo mkdir /var/www/html/git
Next, give proper permission to the Git directory:
sudo chown -R www-data:www-data /var/www/html/git
Once you are done, you can proceed to configure Nginx web server.
3 Configure Nginx
First, you will need to configure Nginx to pass on Git traffic to Git. You can do this by editing Nginx default configuration file:
sudo nano /etc/nginx/sites-available/default
Change the file as shown below:
# Default server configuration # server { listen 80 default_server; listen [::]:80 default_server; 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 _; 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; # Git pushes can be massive, just to make sure nginx doesn't suddenly cut the connection add this. auth_basic "Git Login"; # Whatever text will do. auth_basic_user_file "/var/www/html/git/htpasswd"; include /etc/nginx/fastcgi_params; # Include the default fastcgi configs fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; # Tells fastcgi to pass the request to the git http backend executable fastcgi_param GIT_HTTP_EXPORT_ALL ""; fastcgi_param GIT_PROJECT_ROOT /var/www/html/git; # /var/www/git is the location of all of your git repositories. fastcgi_param REMOTE_USER $remote_user; fastcgi_param PATH_INFO $1; # Takes the capture group from our location directive and gives git that. fastcgi_pass unix:/var/run/fcgiwrap.socket; # Pass the request to fastcgi } }
Save and close the file when you are finished. Then test Nginx for any configuration error with the following command:
sudo nginx -t
If everything is fine, you should see the following output:
Next, you will need to create a user account of which you will need to use to browse of commit to the repository. You can create a user with the name hitesh by using the htpasswd utility:
sudo htpasswd -c /var/www/html/git/htpasswd hitesh
Finally, restart Nginx to apply all the changes with the following command:
sudo systemctl restart nginx
You can check the status of the Nginx server with the following command:
sudo systemctl status nginx
You should see the following output:
4 Create a Git Repository
Once everything is configured properly, it's time to create Git repository.
You can create a repository with name hitesh.git with the following command:
cd /var/www/html/git
sudo mkdir hitesh.git
sudo cd hitesh.git
sudo git --bare init
sudo git update-server-info
sudo chown -R www-data.www-data .
sudo chmod -R 755 .
Next, you will need to allow HTTP service through UFW firewall. By default UFW is disabled on your system, so you need to enable it first. You can enable it with the following command:
sudo ufw enable
Once UFW firewall is enabled, you can allow HTTP and SSH service by running the following command:
sudo ufw allow http
sudo ufw allow ssh
SSH is not needed for GIT, but you should enable it to be able to manage your server through SSH.
You can now check the status of the UFW firewall by running the following command:
sudo ufw status
Ok that's it for the server-side configuration. You can now move on to the client-side to test Git.
5 Test Git on the Client Machine
Before starting, you will need to install git on the client system. You can install it with the following command:
sudo apt-get install git -y
First, create a local repository with the following command:
sudo mkdir ~/testproject
Next, change the directory to 'testproject' and initiate the new remote repository with the following command:
cd ~/testproject
git init
git remote add origin http://[email protected]/hitesh.git
Next, create some files and directory with the following command:
mkdir test1 test2 test3
echo "This is my first repository" > test1/repo1
echo "This is my second repository" > test2/repo2
echo "This is my third repository" > test3/repo3
Next, run the following command to add all the files and directories to the repository:
git add .
git commit -a -m "Add files and directories"
You should see the following output:
[master 002fac9] Add files and directories 3 files changed, 3 insertions(+) create mode 100644 repo1 create mode 100644 repo2 create mode 100644 repo3
Next, push all the files and directories to the Git server with the following command:
git push origin master
You should see the following output:
Password for 'http://[email protected]': Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (5/5), 422 bytes | 0 bytes/s, done. Total 5 (delta 0), reused 0 (delta 0) To http://[email protected]/hitesh.git 68f1270..002fac9 master -> master
Now, your all files and directories have been committed to your Git server.
Your Git repository creation process is now completed. You can now easily clone your repository in future. You can clone your repository using the following command on the remote system:
git clone [email protected]:/var/www/html/git/hitesh.git
You should see the following output:
Cloning into 'hitesh'... [email protected]'s password: remote: Counting objects: 8, done. remote: Compressing objects: 100% (3/3), done. Receiving objects: 100% (8/8), 598 bytes | 0 bytes/s, done. remote: Total 8 (delta 0), reused 0 (delta 0) Checking connectivity... done.
Now, change the directory to the cloned repository with the following command:
cd hitesh
tree
You should see the following output:
. |-- test1 | `-- repo1 |-- test2 | `-- repo2 `-- test3 `-- repo3 3 directories, 3 files
Download as VM
This tutorial is available as ready to use virtual machine image in ovf/ova format that is compatible with VMWare and Virtualbox. The virtual machine image uses the following login details:
SSH / Shell Login
Username: administrator
Password: howtoforge
This user has sudo rights.
GIT Login
Username: hitesh
Password: howtoforge
The IP of the VM is 192.168.1.100, it can be changed in the file /etc/netplan/01-netcfg.yaml. Please change all the above passwords to secure the virtual machine.
Conclusion
I hope you can now easily push, pull, clone and commit source code using Git server. Feel free to comments me if you have any doubt.