tutorial, git,

Enable git over http

Jing Chi Jing Chi Follow Aug 07, 2019 · 2 mins read
Enable git over http
Share this

Usually, you push your changes to remote repository by git or ssh protocol, but in case, e.g. in my company office, these protocols are blocked for security consideration. In the following sections, I’ll show you how to enable git over http.

The following tutorial is based on the assumption that you are using Linux VPS for your repository/http server. Check linode.com to get one if you have not got one.

Install Nginx http server

Run command which nginx to check if Nginx http server is ready, install it if it is not available by running sudo apt install nginx.

Enable secure https

You need SSL certificate to enable secure https server, usually you can buy it from website hosting. Fortunately, you can get it free from Let’s Encrypt, furthermore certbo provides a convenient tool to apply or renew it automatically. Check certbo official website and follow the guide to install and enable your https website. e.g. if you’re ruing Nginx on UbuntuLTS 18.04 you should follow this guide.

Install fcgiwrap and apache2-utils

Run sudo apt install fcgiwrap apache2-utils -y to install them.

Configure Nginx http server

Add the following to Nginx http server configurations file /etc/nginx/sites-enabled/default before the line listen [::]:443 ssl ipv6only=on; # managed by Certbot, which enclosed by server {}.

location ~ (/git/.*) {
    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/; # /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

Reload Nginx after configured sudo nginx -s reload

Add a user and password

# create dedicated repository directory
sudo mkdir /var/www/html/git

# create a new user with password
sudo htpasswd -c /var/www/html/git/.htpasswd <user name>

Create git repository

sudo mkdir /var/www/html/git/repo.git
cd /var/www/html/git/repo.git
sudo git init --bare

# give http server access permision
chown -R www-data:www-data /var/www/html/git

Reload Nginx http server configurations

sudo nginx -s reload

Try your git repository now

# on your local PC
git clone https://<user name>@your-domain-name.com/git/repo.git
cd repo
echo "hello world" > README
git status
git add *
git commit -a -m "hello world"
git status
git push