How to install own git server with ssh and http access by using gitolite and gitweb in CentOS

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.

70 thoughts on “How to install own git server with ssh and http access by using gitolite and gitweb in CentOS”

  1. Thank you for your excellent tutorial. I was able to get almost* everything working on my end. But I clueless about an issue I’m facing…

    Please when I create two users user1 & user2
    htpasswd /etc/httpd/conf/git.passwd user1
    htpasswd /etc/httpd/conf/git.passwd user2

    repo newrepo
    RW+ = user2

    repo newallrepo
    RW+ = @all

    When I log into the gitweb as user2, I only see and have access to the repo newallrepo.
    What am I doing wrong ?

    Thanks

    Reply
  2. hello Sharad,
    thanks for the quick reply. i will move on to gitlab, but still wish to fully implement this configuration. kindly help me get through with this. thanks

    Reply
  3. I too followed exactlywhat was procedurized here,but see 404no projectsfound error
    so below arechanges to my configurations

    entries in /etc/gitweb.conf & /var/www/html/gitweb/gitweb.cgi

    our $projectroot = “/var/www/html/gitweb/gitrepos/”;
    our $projects_list = “$projectroot”;

    created 2 empty projects in above path
    mkdir /var/www/html/gitweb/gitrepos/project1
    mkdir /var/www/html/gitweb/gitrepos/project2

    initiated ‘bare’ repositories

    git init –bare /var/www/html/gitweb/gitrepos/project2
    git init –bare /var/www/html/gitweb/gitrepos/project1

    chmod -R g+rx /home/git/repositories

    restarted httpd and I started to see the projects

    PS: I wanted to narrow down the issue, so i removed the gitolite content from apache configs

    Reply
  4. hi, when i try locally , i can clone and make some changes and commit.but i couldn’t push.. any one can help

    i got below error

    git push origin master
    Counting objects: 3, done.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (2/2), 226 bytes | 0 bytes/s, done.
    Total 2 (delta 0), reused 0 (delta 0)
    remote: Empty compile time value given to use lib at hooks/update line 6.
    remote: Use of uninitialized value in require at hooks/update line 7.
    remote: Can’t locate Gitolite/Hooks/Update.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at hooks/update line 7.
    remote: BEGIN failed–compilation aborted at hooks/update line 7.
    remote: error: hook declined to update refs/heads/master
    To file:///home/git/repositories/otglocal.git/
    ! [remote rejected] master -> master (hook declined)
    error: failed to push some refs to ‘file:///home/git/repositories/otglocal.git/’

    Reply
    • i got the solution for the same..
      please do the below..

      Then create file hooks/post-receive:

      #!/bin/sh
      GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f

      And make the file executable:

      chmod +x hooks/post-receive

      sudo -u git ln -sf /home/git/gitlab-shell/hooks/post-receive /home/git/repositories/{group}/{project}.git/hooks/post-receive
      sudo -u git ln -sf /home/git/gitlab-shell/hooks/update /home/git/repositories/{group}/{project}.git/hooks/update
      chown -R git /home/git/repositories
      After that, it’ll probably work

      Reply
  5. i got below error and can’t clone new repo

    linux.git

    git clone http://10.163.14.198/git/linux.git
    Cloning into ‘linux’…
    Username for ‘http://10.163.14.198’: admin
    Password for ‘http://admin@10.163.14.198’:
    fatal: remote error: FATAL: R any linux admin DENIED by fallthru
    (or you mis-spelled the reponame)

    Reply
  6. Solved the issue, was missing semi-colon in gitweb cgi file. But found another (expected) issue, CentOS 7.2 has firewall service enabled by default. So you should include comments about both firewall blocking httpd, as well as SELinux blocking as noted below in other comments from other individuals.

    Reply
  7. Sorry, followed the directions carefully… But getting the following errors:

    Software error:

    syntax error at gitweb.cgi line 84, near “our ”
    Global symbol “$project_maxdepth” requires explicit package name at gitweb.cgi line 84.
    Global symbol “$projects_list” requires explicit package name at gitweb.cgi line 122.
    Global symbol “$projects_list” requires explicit package name at gitweb.cgi line 1200.
    BEGIN not safe after errors–compilation aborted at gitweb.cgi line 1883.

    Reply

Leave a Comment

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