LinuxTutorialsUbuntu

How To Install HTTP Git Server on Ubuntu 20.04 LTS

Install HTTP Git Server on Ubuntu 20.04

In this tutorial, we will show you how to install HTTP Git Server on Ubuntu 20.04 LTS. For those of you who didn’t know, HTTP Git Server is an open-source project that uses an Nginx webserver to serve Git repositories over your Local Area Network (LAN). HTTP Git Server is surprisingly easy to set up and manage.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you through the step-by-step installation of HTTP Git Server on Ubuntu 20.04 (Focal Fossa). You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 20.04, 18.04, 16.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install HTTP Git Server on Ubuntu 20.04 LTS Focal Fossa

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade
sudo apt install fcgiwrap apache2-utils unzip

Step 2. Installing Nginx on Ubuntu 20.04.

Nginx is available in the default Ubuntu repositories. To install it run the following command:

sudo apt install nginx

Once the installation is completed, run the commands to enable Nginx to automatically startup when your server starts:

sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl enable nginx

Configuring firewall.

Meanwhile, you need to make sure that your firewall is configured to allow traffic on HTTP (80) and HTTPS (443) ports. Nginx registers itself as a service with ufw :

sudo ufw allow in "Nginx Full"

Step 3. Installing Git on Ubuntu 20.04.

Run the following command below to install Git on your Ubuntu system:

sudo apt install git

Confirm Git the installation:

git --version

Step 4. Create a Git Repository.

Now we create a directory to store the Git repository:

mkdir /var/www/html/idroot-repo

Next, change the directory to idroot-repo and create another directory for users:

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

Now we’ll initialize the repository with the command:

cd user.git
git --bare init

Next, update the Git Server with the command:

git update-server-info

Give the repository the correct ownership with the command:

chown -R www-data:www-data /var/www/html/idroot-repo
chmod -R 755 /var/www/html/idroot-repo

After that, create a user called user and set a password to restrict access to the git repository using HTTP basic authentication:

htpasswd -c /var/www/html/idroot-repo/htpasswd user

Step 5. Configure Nginx to Serve Git Repository.

Now we create an Nginx virtual host configuration file to serve the Git repository:

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

Add the following lines:

server {
        listen 80;

        root /var/www/html/idroot-repo;

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

        server_name git.your-domain.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/idroot-repo/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/idroot-repo;
    fastcgi_param REMOTE_USER $remote_user;
    fastcgi_param PATH_INFO $1; 
    fastcgi_pass  unix:/var/run/fcgiwrap.socket;
}

}

Save and close the file, then restart the Nginx service to apply the changes:

nginx -t
sudo systemctl restart nginx

Step 6. Connect to Git Repository From the Client.

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

mkdir my-project

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

cd my-project
git init

We recommended setting your Git to commit email and username. To do that run the following commands:

git config --global user.email "user@your-domain.com"
git config --global user.name "user"

After that, add your remote Git repository with the following command:

git remote add origin http://user@git.your-domain.com/user.git

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

mkdir dev
echo "This is my first my application" > dev/file

We can now add those files to git with the command:

git add .

Commit the changes with the command:

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

Then, push all of our newly created directories and files to the server with the command:

git push origin master

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

Counting objects: 8, done.
Writing objects: 100% (4/4), 512 bytes | 346.00 KiB/s, done.
Total 8 (delta 0), reused 0 (delta 0)
To http://git.your-domain.com/user.git
 * [new branch]      master -> master

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

git clone http://user@git.your-domain.com/user.git

Congratulations! You have successfully installed HTTP Git Server. Thanks for using this tutorial for installing the HTTP Git Server on your Ubuntu 20.04 LTS Focal Fossa system. For additional help or useful information, we recommend you check the official Git website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button