Networking

How to install HTTP Git Server on Ubuntu 18.04

Jack Wallen shows you how to create an HTTP Git Server in less than 10 minutes.

nginxhero.jpg
Image: NGINX

If you're doing internal development, and would rather not have your proprietary code housed on an external server, you might be wondering what you're options are? Fortunately, there are plenty. Even more fortunately, some of those options aren't difficult to get up and running. One such possibility is an HTTP Git Server, which uses the NGINX web server to help serve up Git repositories on your local LAN.

I'm going to walk you through the process of getting this easy-to-use Git Server up and running on the Ubuntu Server 18.04 platform. All you need is a running instance of Ubuntu Server 18.04 and a user account with sudo privileges. With those two things in check, let's install.

SEE: Wireless networking policy (Tech Pro Research)

Updates and dependencies

The first thing to do is update and upgrade Ubuntu Server. Since doing so could cause the kernel to be upgraded, you'll want to do this at a time when a reboot is possible. To run the update/upgrade, issue the commands:

sudo apt-get update
sudo apt-get upgrade

Once those two commands complete, install the necessary dependencies with the command:

sudo apt-get install nginx git nano fcgiwrap apache2-utils -y

Create a working directory

Now we're going to create a directory that our HTTP Git Server will serve files from. Do that with the command:

sudo mkdir /var/www/html/git

Give the newly created directory the necessary permissions with the command:

sudo chown -R www-data:www-data /var/www/html/git

Configure NGINX

Next we need to configure NGINX. We'll do that directly from the default sites-available configuration file. Before we make any changes, let's back that file up with the command:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default-backup

Open the file for editing with the command:

sudo nano /etc/nginx/sites-available/default

Under the server directive (Figure A), add the following:

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
}

Figure A

Figure A

The default NGINX sites-available configuration file.


Save and close that file.

Run the NGINX configuration file test command:

sudo nginx -t

You should see no errors or warnings. If so, go back and make sure the copied text is formatted correctly.

Create a user account

Now let's create a user that will have access to the HTTP Git Server. We can do this with the help of the htpasswd command. I'll create the user jack, but you create whatever user you need. The command to do so is:

sudo htpasswd -c /var/www/html/git/htpasswd jack

Restart NGINX to apply all of our work so far:

sudo systemctl restart nginx

Create a Git repository

Time to create a repository. We'll be sticking with the user account we created above. Issue the command cd /var/www/html/git and create a new directory with the command:

sudo mkdir jack.git

Change into this new directory with the command sudo cd jack.git. Initialize the Git repository with the command:

sudo git --bare init 

Update the Git Server with the command:

sudo git update-server-info

Give the repository the correct ownership with the command:

sudo chown -R www-data.www-data .

Change the permissions of the repository with the command:

sudo chmod -R 755 .

Testing the server

Let's test our server. Move to another machine on your network and install Git with the command:

sudo apt-get install git

Create a local repository within a user's home directory with the command:

mkdir ~/testproject

Change into that newly created directory with the command cd ~/testproject. Initialize the repository with the command:

git init

Now let's add the origin with the command:

git remote add origin http://jack@SERVER_IP/jack.git

where SERVER_IP is the IP address of your server.

Now create some test files with the following commands:

mkdir test1 test2 test3
touch test1/testing1 test2/testing2 test3/testing3

Add those new files to Git, and commit the changes with the following commands:

git add .
git commit -a -m "Added test directories and files"


Push all of our newly created directories and files to the server with the command:

git push origin master

You have committed the test directories and files to the Git Server. Go to another client machine, install Git, and clone that repository with the command:

git clone jack@SERVER_IP:/var/www/html/git/jack.git

Once the clone finishes, you should see the directory ~/jack on your client. Change into that directory, and you'll see the directories listed from the Git Server (Figure B).

Figure B

Figure B

Our cloned directories from the Git server.


Happy Git'ting

And that's all there is to setting up an HTTP Git Server on Ubuntu Server 18.04. This is one of the easiest ways to create a Git repository that is accessible from within your LAN. Happy Git'ting!

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox