Setting up GIT Server on Ubuntu 16.04 over HTTP [SOLVED]

October 16th, 2017

There are so many links to sites with instructions on how to setup GIT servers and 90% of them don’t work. Having followed the instructions that didn’t work I managed to find one that did:-

https://www.howtoforge.com/tutorial/install-http-git-server-with-nginx-on-ubuntu-1604/

I normally use CENTOS but the instructions were for Ubuntu so I created an Ubuntu box for setting up the Git Server. I found a couple of typos in the above link so if you followed their instructions and could not get things to work then you can have a look at my instructions below. I used the below instructions and managed to create a working Git Server.

You may need to improve upon the security of the setup down the road but the following setup works. 100%

(1) Install Packages

Install some useful things, after logging into your Ubuntu box:-

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

sudo mkdir /var/www/html/git

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

Ubuntu uses www-data for the web server user by default – you can change this but don’t bother.

(2) Setup NginX

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

Add this:-

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
}

after these lines:-
   location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
The final config should look like this:-
# 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
}

}

Back to the command line, run this to check that the config is good:-
sudo nginx -t 

You should see this if everything is working:-

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Run this to create an account for “jason” – change to whatever name you want:-

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

It will prompt you to enter password for jason or whatever user you decide to create.

After that, restart the NginX server:-

sudo systemctl restart nginx

Run this to check that everything is ok:-

sudo systemctl status nginx

You should see something like this:-

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 Mon 2017-10-16 22:24:53 HKT; 8s ago
Process: 2582 ExecStop=/sbin/start-stop-daemon –quiet –stop –retry QUIT/5 –pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 2592 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 2586 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 2595 (nginx)
Tasks: 3
Memory: 2.2M
CPU: 32ms
CGroup: /system.slice/nginx.service
├─2595 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
├─2596 nginx: worker process
└─2597 nginx: worker process

(3) Create Git Repository

cd /var/www/html/git
sudo mkdir hitesh.git
cd hitesh.git
sudo git –bare init
sudo git update-server-info
sudo chown -R www-data.www-data .
sudo chmod -R 777 .

The above will create the Git repo in the directory /var/www/html/git/hitesh.git – in the original instructions the tutorial forgot the line “cd hitesh.git” which will cause the repo to be created in /var/www/html/git/ which is wrong – also it should be “–bare init” and not “–bare initt”.

Next, turn the firewall on but make sure the webserver is allowed and also sshd

sudo ufw enable
sudo ufw allow http
sudo ufw allow ssh
sudo ufw status

(4) Test Git on Client Machine

The client machine could be anything from a windows machine, Mac OS X or Linux box. It just needs to have a Git client. With all of the above steps completed the Git Server is actually already setup so the following steps is just for testing your Git Server.

I’m using Mac and had Git already installed.

Go to a sandbox area, using Terminal (Applications -> Utilities)

cd ~/testproject
git init
git remote add origin http://jason@192.168.6.63/hitesh.git

You have to replace the above IP address (192.168.6.63) with your server’s IP address and jason with whatever user you had created.

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

Now to add to the Git repo:-

git add .
git commit -a -m “Add files and directoires”

Now to push the files onto the Master – a moment of truth!

git push origin master

If it works you will get something like this:-

Password for ‘http://jason@192.168.3.63’:
Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (11/11), 748 bytes | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To http://jason@192.168.3.63/hitesh.git
* [new branch]      master -> master