In this tutorial we will learn about,how to install own git server with ssh and http access by using gitolite and gitweb in CentOS.For user management in Git Server,we will use the Gitolite.For showing the repository list on web interface,we will use Gitweb. And Smart HTTP is configured in Apache, to access the Git server via HTTP.

Git Project URL : http://git-scm.com/

Description of our server
Arch: i686 / x86_64
Packages : Gitolite and Gitweb
Git server ip address : 192.168.169.95

Note:You can also use git server FQDN or hostname resolvable from your internal network or DNS.

Summary : Creating a private git server. And can be accesible through ssh and http both.Here Gitweb will be used for viewing repositories detail. Gitolite is used for Git server with user/group management.

Note: If you see # sign it means root is log in. If it is $ means it is user called git log in.

Follow the given below steps as it is :

First start working from your pc ,not from git server.

First start with creating RSA key in your system means your client machine (not git server).We will start working in Git Server after finishing steps in client PC.

Step A: login into your linux pc from where you want to manage git server remotely .(means here I am talking about your client system)

Now create the RSA key . It will create two files id_rsa and id_rsa.pub in your system’s $HOME/.ssh directory. Do not give passphrase password. Just hit enter when it ask for setting passphrase password

$ ssh-keygen -t rsa -C "Git-Admin"

Step B: Now scp the id_rsa.pub file into Git Server (Its our IP Address : 192.168.169.95)

$ scp ~/.ssh/id_rsa.pub root@ip-address-of-git-server:~ 

Login into the Git Server with user root.

Login into Git Server as a root user.

Step 1: Install git,http and perl dependency.

# yum -y install git httpd perl-Time-HiRes perl-Data-Dumper

Step 2: Create user git and change its gid and uid

 # useradd git
 # usermod -u 600 git
 # groupmod -g 600 git

Step3: Rename the id_rsa.pub file which you did scp from your system.And move the rename files to /home/git and change its owner & group with git

# mv /root/id_rsa.pub /home/git/Git-Admin.pub ; chown git:git /home/git/Git-Admin.pub

Step 4: Login with user git directly from terminal.

Do not set password for user git. And git clone the gitolite from github.com

#su -l git

$whoami          (The command will show you log in with which user)
$echo $HOME       (The command will show what is your home directory)

$ git clone git://github.com/sitaramc/gitolite

Step 4: Create bin directory in /home/git and setup the Git-Admin account.

Note: We renamed the id_rsa.pub file to Git-Admin.pub ,the reason is the Gitolite will provide same name of user in gitolite.conf file as the name of .pub file.
for eg. if I use only id_rsa.pub then “id_rsa” user will be created.Hence if you are adding any user in Git Server by using its ssh key, rename the user’s id_rsa.pub key with user-name.pub .
like joe’s rsa file should be change into joe.pub (id_rsa.pub –rename–> joe.pub)

$ mkdir -p /home/git/bin
$ gitolite/install -ln
$ gitolite setup -pk Git-Admin.pub

Step 5: Exit from user git login and again use the user root. Now check what are values defined for suexec bydefault. Use the command “suexec -V”

$exit   (logout from git user)
you should be now root if you are following the steps as it is.If no then no-problem login with root again :)

# suexec -v 

Given below is my server’s suexec details.

[root@gitserver ~]# suexec -V
 -D AP_DOC_ROOT="/var/www"
 -D AP_GID_MIN=100
 -D AP_HTTPD_USER="apache"
 -D AP_LOG_EXEC="/var/log/httpd/suexec.log"
 -D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
 -D AP_UID_MIN=500
 -D AP_USERDIR_SUFFIX="public_html"

Step 6: Create a directory bin in /var/www (Why /var/www ? because I got the detail from suexec -V,see parameter AP_DOC_ROOT)
Below given command will create directory bin in /var/www with permission 0755 and owner &group is git

#install -d -m 0755 -o git -g git /var/www/bin

Step 7: Create a bash script with name gitolite-suexec-wrapper.sh in /var/www/bin/. Then save and exit.(I am using vi editor,you can choose your any other editor)

vi /var/www/bin/gitolite-suexec-wrapper.sh

#!/bin/bash
#
# Suexec wrapper for gitolite-shell
#

export GIT_PROJECT_ROOT="/home/git/repositories"
export GITOLITE_HTTP_HOME="/home/git"

exec ${GITOLITE_HTTP_HOME}/gitolite/src/gitolite-shell

Step 8: Set ownership and permission of /var/www/bin and gitolite-suexec-wrapper.sh

# chown -R git:git /var/www/bin
# chmod 750 /var/www/bin/gitolite-suexec-wrapper.sh
#chmod 755 /var/www/bin

Step 9: Now change the value of “UMASK 0077,” into “UMASK => 0027,”in file /home/git/.gitolite.rc

vi /home/git/.gitolite.rc

UMASK => 0027,

Step 10: Install GitWeb

yum install gitweb

Step 11: By-Default the gitweb will be installed in /var/www/git directory.(It has gitweb.cgi file)
We will rename git directory as gitweb. And move the renamed directory inside /var/www/html

# mv /var/www/git /var/www/html/gitweb

Step 12: Change the ownership of /var/www/html/gitweb


# chown -R git:git /var/www/html/gitweb

Given below are the details of my server

[root@gitserver html]# chown -R git:git gitweb/
[root@gitserver html]# ls -ld gitweb/
drwxr-xr-x 2 git git 4096 Jun  1 12:36 gitweb/
[root@gitserver html]# ls -la gitweb/
total 252
drwxr-xr-x 2 git  git    4096 Jun  1 12:36 .
drwxr-xr-x 3 root root   4096 Jun  1 12:34 ..
-rw-r--r-- 1 git  git     115 Apr 24  2010 git-favicon.png
-rw-r--r-- 1 git  git     207 Apr 24  2010 git-logo.png
-rwxr-xr-x 1 git  git  204754 Jun  1 12:36 gitweb.cgi
-rw-r--r-- 1 git  git    8379 Apr 24  2010 gitweb.css
-rw-r--r-- 1 git  git   24142 Apr 24  2010 gitweb.js
[root@gitserver html]#

Step 13: Edit the file /etc/gitweb.conf. and change the value of two variables
$projectroot and $projects_list as given below

vi /etc/gitweb.conf
     our $projectroot = "/home/git/repositories/";
     our $projects_list = "/home/git/projects.list";

Step 14: Edit the file /var/www/html/gitweb/gitweb.cgi and change the value of two variables
$projectroot and $projects_list as given below

vi /var/www/html/gitweb/gitweb.cgi

    our $projectroot = "/home/git/repositories";
    our $projects_list = "/home/git/projects.list";

Step 15: Create dummy folder git . And be carefull with permissions,owner and group

 #install -d -m 0755 -o apache -g apache /var/www/git  (This is dummy one)

Step 16: Open the /etc/httpd/conf/httpd.conf file and in last line paste the given below VirtualHost Configuration.

Note: If you have set the hostname and FQDN of git server then you can uncomment the ServerName,ServerAlias related lines and write the information which you have. If you do not have these info ,no problem paste the content as it is.



  # You can comment out the below 3 lines and put correct value as per your server information
  #  ServerName        gitserver.example.com
  #  ServerAlias       gitserver
    ServerAdmin       youremailid@example.com

    DocumentRoot /var/www/git
    
        Options       None
        AllowOverride none
        Order         allow,deny
        Allow         from all
        
    

    SuexecUserGroup git git
    ScriptAlias /git/ /var/www/bin/gitolite-suexec-wrapper.sh/
    ScriptAlias /gitmob/ /var/www/bin/gitolite-suexec-wrapper.sh/

    
        AuthType Basic
        AuthName "Git Access"
        Require valid-user
        AuthUserFile /etc/httpd/conf/git.passwd
    

Step 17: Edit the /etc/httpd/conf.d/git.conf file. After installation of GitWeb this git.conf file created itself. Here we are doing some changes as per our Git Server. Carefully see each line which I edited. Do not skip it is important part also.

vi /etc/httpd/conf.d/git.conf 

Alias /gitweb /var/www/html/gitweb


  Options +ExecCGI
  AddHandler cgi-script .cgi
  DirectoryIndex gitweb.cgi


   AuthType Basic
   AuthName "Git Access"
   Require valid-user
   AuthUserFile /etc/httpd/conf/git.passwd

Step 18: Now we will create Basic apache auth username and password.
When you create a first time a user then only we use “-c” flag. -c means create new file. see man page of htpasswd.

# htpasswd -c /etc/httpd/conf/git.passwd admin

For new user addition or updating existing user’s password do not use -c flag

  # htpasswd /etc/httpd/conf/git.passwd  user1
  # htpasswd /etc/httpd/conf/git.passwd  testuser

Restart/Reload the apache whenever you setup a htpasswd user or passwd
chkconfig command to set apache service to run in runelevel 3 and 5 whenever system start.

#### On CentOS 6.x / RHEL 6.x
/etc/init.d/httpd restart;chkconfig httpd on

#### On CentOS 7.x / RHEL 7.x
systemctl restart httpd ; systemctl enable httpd 

All configuration is done. Now Git Server is ready to use.

How to see GitWeb webpage

Open the web browser and in URL type
(give username and password which you set at htpasswd command)

http://ip-address-of-git-server/gitweb/

How to clone the repository through HTTP in client machine

Use the command.
(give username and password which you set at htpasswd command)


$git clone http://ip-address-of-git-server-OR-FQDN/git/repo-name.git

See difference, when I am cloning the repo from Git Server I am using word git in between of URL.
But for git web ,I am using word “gitweb”

Note: If you want to learn why git and gitweb,open the git.conf and httpd.conf file.
In httpd.conf , “ScriptAlias /git/ /var/www/bin/gitolite-suexec-wrapper.sh/ ”
In git.conf “Alias /gitweb /var/www/html/gitweb ”

$ cd ~/Desktop
$git clone http://ip-address-of-git-server-OR-FQDN/git/testing.git

Now it will clone the testing repo in your Desktop or wherever directory from you run the git clone command

How to Manage user and group of Git Server

For managing Users/Groups in Git Server from your remote machine, you have to clone the gitolite-admin repo in your System. But it is the same system from which you scp the RSA key file.(Read Step A and Step B ,start of this post)

Here I will clone gitolite-admin in Desktop


$cd ~/Desktop
$ git config --global user.name "Git-Admin"
$ git config --global user.email "youremailid@example.com"
$ git clone git@GitServerIP-or-FQDN:gitolite-admin.git

With the help of gitolite.conf file you can manage the Users and Group from your system.
But whenever you do any changes you have to do “git push”

Given below is reference from my system

sharad@mypc:~/Desktop/gitolite-admin/conf$ pwd
/home/sharad/Desktop/gitolite-admin/conf
sharad@sharad-sapplica:~/Desktop/gitolite-admin/conf$ cat gitolite.conf 
repo gitolite-admin
    RW+     =   Git-Admin

repo testing
    RW+     =   @all
    R       =   git daemon

sharad@mypc:~/Desktop/gitolite-admin/conf$

Here, meaning of –
R = Read
W = Write

Now push new changes of gitolite.conf into git server from your system

Use the given below command to push the changes into Git Server.

Go to gitolite-admin cloned directory and change to conf directory also. Edit the changes in gitolite.conf and push the code with given below command.

$ cd ~/Desktop/gitolite-admin/conf
$ls -l gitolite.conf

$git add gitolite.conf
$git commit -m "first commit"
$git push origin master

Note: git pull command is for pull the Version files/directory from Git Server.

How to create repository in Git Server

In this eg. we are creating repo called “test-repo”

Login in Git Server as root.And then change to git user.


# su -l git
$ cd repositories
$ mkdir linux.git
$ cd linux.git
$ git --bare init
$ git update-server-info

Update the projects.list file

Now update the file projects.list file with new git repo name which you have just created as described in above section.

vi /home/git/projects.list
testing.git
linux.git

Once you updated the /home/git/projects.list file, it will help you to show repository on Gitweb.
See the below given screenshot –
gitweb

(This post is last updated on 07-July -2015 )
* Added CentOS 7 in practical scenario .
* Added perl-Data-Dumper installation as dependency for gitolite.

Read Some More Articles

70 Comments

  1. Thanks a lot for instructions. These are best.

    I am having issues on STEP 12:

    I am not seeing git-favicon.png, git-logo.png, gitweb.css, gitweb.js

    Please advise.

    Thanks
    Jas

      1. Thanks I got the GITWeb up now. But I am not seeing the testing.git .

        Getting error 404 – No projects found

        Please advise

        Thanks alot

          1. hello, wonderful tutorial, I followed everything and all is working correctly.

            I am not able to clone new repository using http and am only able to clone testing.git directory.
            I added R = daemon in the gitolite.conf file and pushed it as well.

            curl http://localhost/git/info shows only testing.git directory

            please suggest a solution. thanks.

          2. Hello Vedant,

            Thank you for commenting. This post was written long back and currently I am not curating it.
            I would like to encourage to use Gitlab which is fantastic open source tool and my experience with Gitlab is awesome till now.

            If you are still interested to work on this method which I have written, surely I will recheck the steps. Anyway it is basic technique and give immense understanding how git works in backend.

            Regards
            Sharad

  2. Hello All,

    we have also followed all above mentioned steps.
    Here am also facing same problem even after disabling SELinux on my CentOS 7.1
    error: 404 – No projects found

  3. Hi I followed your step by step and by the way thanks it is very helpful and the only clear steps I’ve seen so far. However, in the web interface I cannot see any project. Even the testing.git is not displaying and 404 – No projects found. Hope you can help. I am using Centos v. 7

    1. Hello Mark,

      I re validated this practical just short while ago. It looks all good for me.
      Please do check all the steps once again. Hope , you will find missing step. Also recommend to read the post carefully.

      404 when page is not found. Seems problem with Gitolite configuration.

      Revert , if still has issue.

      Regards
      Sharad

      1. Thanks. I found the issue. I was on RHEL and somehow SElinux is enforcing. So I disabled it for now. I have to find a way to make it work without permanently disabling.

        Cheers,
        Mark

        1. Hey Mark,

          This is really awesome spree of conversation on our comment section. I appreciate for valuable feedback which helps many reader.
          If you are thinking to not to disable selinux, I will suggest you to workaround on selinux for Apache.

          Cheers !

          Sharad

        1. Hello Andrew,

          Just now I completed the practical on CentOS 7.
          I found the same issue that repolist is not updated. I will workaround on it and share the update soon.

          Thank you for reporting. Other than that I have not found any such issue and all is working fine.

          Regards
          Sharad

        2. Shame on me, I forgot this step.
          You have to update this file /home/git/projects.list . Then reload the web interface , you will see the repo name listed there.
          Add the repo name in this file.

          vi /home/git/projects.list
          
          testing.git
          linux.git
          your-git-repo-name.git
          

          Given below is screenshot from my server

          gitweb

  4. First off, this is the best tutorial I’ve found for setting this up, so thank you!
    Everything works with the testing.git repo, but when I try to clone a new repo (after following your instructions on creating a new repo on the server), it looks like it can’t find it. The only difference in my setup is that my SSH is on a different port, so my clone looks like: git clone ssh://git@myserver:portnum/reponame.git

    Any ideas? The error looks like this:
    Cloning into ‘myreponame’…
    Warning: Permanently added ‘[servername]:pornum,[xxx.xxx.xxx.xxx]:portnum’ (RSA) to the list of known hosts.
    FATAL: R any myreponame Git-Admin DENIED by fallthru
    (or you mis-spelled the reponame)
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.

    1. Hi Fenix,

      I had the same issue when created new repositories. I went into my /home/git/repositories/myrepo.git folder and did just ran “touch git-daemon-export-ok”

      That seemed to fix it.

  5. Thanks for this tutorial. We were able to setup an in-house git server with gitolite for a WordPress development. Since the git version in “yum install git” is at 1.x versions, we installed git from source so we could harness the improvements in 2.x versions. With this guide and tweaks made in gitweb, we managed to have all things running.

    However, we are also doing developments in in WordPress running atop of nginx instead of Apache. Currently tweaking settings and configs. It would be great to see a tutorial like this with nginx + php-fpm used instead of Apache.

    Thanks much for your efforts. More power.

    1. Hello Denmar,

      I recognized your comment as one of the best detailed feedback . I also appreciate that you tweaked and played around the setup. Your suggestion on Nginx+PHP-fpm is good, I will setup something same for my new project.

      Regards
      Sharad

      1. Hello Sharad,

        Appreciate the response and looking forward to the guide you will be posting. I am still exploring my way with nginx though and solving errors one after another that were listed in the error logs.

        This is what we did in /etc/gitweb.conf so that gitweb will be able to identify the compiled-from-source git 2.4

        $GIT = ”;

        We managed to install gitweb without git as its dependency from yum.

        More like figuring things out based from the errors, clues and the general idea.

        Again, thanks.

        -Denmar

  6. It’s very nice tutorial and detailed explanation. After went this tutorial, I came up with few questions which will be clarified hopefully.

    1) If the gitolite config repo can be cloned in every client hosts, the admin members(here respective team managers) can change to “W” to others projects that lead security issues probably. Every team data should be confidential.

    2) How to add existing files into GIT repo? Because we maitain SFTP accounts which are stored in default location /etc/{passwd, shadow, group and gshadow}.

    3) At the same time we have other files in /etc/ which will be managed through git.

    4) How the internal GIT will be maintained via GUI like Github.

    Thanks in advance.

  7. Dear Seniors,

    Am new to Git server..am trying to build the git server since last year..Please give a document or how to setup a git server..

    Thanks,
    Prashanth.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.