I used to use Apache Subversion as my personal project's version controlling system. But nowadays Git is getting more popular than Subversion with some advanced features in its. Companies like GitHub are now offering code hosting services based on "Git". However, there are some limitation and restrictions, especially if you are an individual or small player. One of the limitations of GitHub is that the free service doesn't allow private hosting of the code. The user has to pay a monthly fee of $7 to host private repositories, and the expenses go up with more repos.

In cases like these or when you want more control or if you want to experience the new features in Git, the best path is to run "Git" on your own server. Not only do you save costs, you also have more control over your server. In most cases a majority of advanced Linux users already have their own servers and pushing "Git" on those servers is like 'free as in beer'.

In this tutorial, I am going to summarize the configuration steps what I have done.

For this tutorial, I use Ubuntu 16.04 LTS server running in native for the git server. As well as same version of Ubuntu virtual box image as the client PC.

Step 1.

First of all, You have to install git-core on the server using following commands. It'd better update your Ubuntu server up to date by using update command before installing the Git.

sudo apt-get update
sudo apt-get install git-core

Step 2.

Then you have to decide and create a root folder path for the Git repositories. You can make a directory or select a place anywhere you wish on your server. However, in this tutorial, I use "~/git-repos" folder as my repositories root. There was no folder on that path so I created a folder.

mkdir -p ~/git-repos
cd ~/git-repos

Step 3.

Inside the repository root, you have to create sub-folders for each repository and this sub-folder name will be your repository name. You can select any name as you wish. But this time, I ask you to create a folder name "test.git" as your first git repository along with this tutorial.

mkdir test.git
cd test.git

Step 4.

Now you have created a folder for your first repository. But repository hasn't been created or initialized. Still, It's empty. So You have to init the repository using "git init" command as follows.

sudo git init --bare

Step 5.

Now you have created a repository but you cannot checkout it from outside the server. The reason is you are not allowed to access your git repositories by the server. So you have to enable HTTP protocol with Git to access it. For that, I'm going to install Apache server and necessary modules. If you haven't installed Apache on your server yet, you can install it on your server using the following command.

sudo apt-get install
apache2 apache2-utils

Step 6.

As well as, You have to enable 'cgi', 'alias', 'env' and 'rewrite' modules using 'a2enmod' command as follows.

a2enmod cgi alias envre write

Step 7.

Next problem is, apache server is not allowed to access folders or files which are not bearing the 'www-data' user's ownership. So you have to give the 'www-data' ownership from the git repository root recursively.

sudo chownwww-data:www-data ~/git-repos/* -R

Step 8.

Then you have to create users list to give access permission to repositories. There are several ways to Git user management.But here I'm going to use 'htpasswd' command to create a users maintain encrypted file.

sudo htpasswd -c /etc/apache2/passwd.git <USERNAME>
<PASSWORD>

Step 9.

Now you have created a user for git access. Next, you have to create an apache configuration file to access git repository allowing only the user has created above step.

 sudo vi/etc/apache2/sites-available/git.conf

Step 10.

Then add the following code to it. (You would have to change the repository root folder path, if you used another path in the Step 2)

	
SetEnv GIT_PROJECT_ROOT ~/git-repos/
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/

RewriteEngine On
RewriteCond %{QUERY_STRING} service=git-receive-pack [OR]
RewriteCond %{REQUEST_URI} /git-receive-pack$
RewriteRule ^/git/ - [E=AUTHREQUIRED]

<Files "git-http-backend">
AuthType Basic
AuthName "Git Access"
AuthUserFile /etc/apache2/passwd.git
Require valid-user
</Files>

Step 11.

You have created an apache configuration file. But, still, apache doesn't know that you have created it a site configuration file. Then, You have to enable that configuration file using 'a2ensite' command.

 sudo a2ensite git.conf

Step 12.

Even though you have created a configuration file, it's not loaded yet. To load it, you have to restart the apache server

sudo service apache2 restart

Step 13.

If it is restarted without any error, Your server side configuration has been almost done!.

Then see the Client Configuration section to see how clone it from client PC.