No GitHub Required! | How To Create Your Own Git Server | With Gitweb on Nginx

In this article, we’re going to take a look at how to set up your own Git server.

If you’d prefer something visual, you can check out the video demonstration:


Why Do This Anyway?

GitHub is a nice website. And you can host your own Git projects there for free. But, when you start adding features and functionality, free becomes paid. And pretty soon you are relying on this website to service your needs. Why not do it yourself? And enjoy unlimited customization?

Your Server Is Your Git Hub

If you are familiar with how Git works in theory, you understand how your remote repository acts as a central hub for managing your project. It is the central source from which (and to which) you can manage the day-to-day history of your project. And with the Gitweb front end, this is what you get:

Managing SSH Keys

This is not a full course in SSH keys. I’ll assume you know understand the basics:

  1. You have a public SSH key and a private one
  2. You know which one is which

You will need to grab your public key later on this tutorial, so have it ready.

Provision a Server

In order to have a good server you will need to start with a bare Linux server. Many hosting companies are now providing affordable Linux cloud servers for a few bucks a month. (This article will assume you’re using the Debian operating system.)

Once you’ve got yourself a server you will need to log into the root user via SSH.

ssh root@example.com

Install Git

We’ll be installing a few packages in this article. We’ll start by installing Git. But before you do anything, go ahead and run an apt update command.

apt update

Then you can install Git:

apt install git

Add SSH Key To Git User

You will need to create a Linux user to handle the Git connections. You will not need to log into your user often, unless you want to create a new repository or do some other server-related tasks.

adduser git

Follow the prompts. The password is not very important, because you’ll be using SSH keys to log in. But be sure to use a secure password just in case.

Assume the git user:

su git

Make an .ssh director:

mkdir .ssh

Secure that directory by changing the privilege level:

chmod 700 .ssh

Create an “authorized_keys” file:

touch .ssh/authorized_keys

Secure the “authorized_keys” file:

chmod 600 .ssh/authorized_keys

Add your public SSH key to the “authorized_keys” file. Use your favorite text editor. The SSH public key is basically a line of text.

Create Your Repository Directory (*.git)

The Git user will need privileged access to the directory in which all the repositories will be created. I use the /var/lib/git directory, but you can use a different one, if you like. (This is the default directory used by the Gitweb program, so it saves some time to use it by default.)

Go back to root:

exit

Changed ownership of this directory to the Git user and group:

chown git:git /var/lib/git

Go back to the Git user:

su git

Make the repository directory. This directory name must end with .git.

mkdir /var/lib/git/<repo name>.git

You can otherwise name the directory whatever you want.

Initialize Your New Repo

Now that you have set up a directory to house your repository, you just need to initialize that directory as a Git repository.

Change directory into your new desired working directory and run the Git initialization command, with the --bare option:

git init --bare

This will effectively create a “bare” Git repository in this directory. It’s now ready to be used as a fully functioning Git repository. You can clone it, push and pull to it.

How To Clone Your Repo And Use It

You are now to clone your remote repo locally. (Note: Cloning is not required. You can also take an existing project and add your new remote repository as a destination.)

Here is the syntax for an SSH clone:

git clone ssh://git@<hostname>:/var/lib/git/<repo>.git

The project you just cloned will have the remote information saved in the local configuration as “origin”.

Install Gitweb w/ NGINX Configuration

For this portion, you will need to log back into your server as the root user.

Install Gitweb:

apt install gitweb

Install fcgiwrap:

apt install fcgiwrap

Install NGINX:

apt install nginx

Now you will just need to create a config file for your NGINX web server. There are many different ways to do this, and many different recipes. The simplest way I’ve found that works is to create a config file in the /etc/nginx/sites-enabled directory and add your config content there (below):

So, first delete (or copy) the default the config file.

cd /etc/nginx/sites-enabled
rm default

Create a new config file:

touch <name of file>

Edit the file and add the following configuration, being sure to replace example.com with your actual domain name.

server {
  listen 80;
  #replace "example.com" below with your domain (or subdomain)
  server_name example.com;

location /index.cgi {
  root /usr/share/gitweb/;
  include fastcgi_params;
  gzip off;
  fastcgi_param SCRIPT_NAME $uri;
  fastcgi_param GITWEB_CONFIG /etc/gitweb.conf;
  fastcgi_pass  unix:/var/run/fcgiwrap.socket;
}

location / {
  root /usr/share/gitweb/;
  index index.cgi;
}

}

Restart NGINX:

service nginx restart

If you get any errors, you may need to restart the server. It happens.

reboot

When you visit your domain, you should see an active Git project and the Gitweb interface.

Be the first to comment

Leave a Reply

Your email address will not be published.


*