Set-up cgit

The purpose of this article is to show you how to build a git server like Github (with less features) with Apache (2.4), cgit and Linux.

The configuration will be tweaked to allow a private (protected by HTTP authentication) and a public server using Linux's permissions and using the same frontend.

For the configuration example, we'll be using the URL http://example.com/mygit and http://example.com/mygit2.

git

The git-scm must be installed. It's available on most of the distributions. For Debian a simple apt-get is sufficient.

To initialize repositories, use

git init --bare

You should be able to clone/connect with

git clone user@server:/path/to/git/repo

cgit

Now for the frontend.

First, clone the latest cgit or use one of the tar release.

Use make and grab the cgit.cgi and move it along with res ang filters into your usual folder for CGI.

And now for the cgit configuration:

logo=/mygit/res/cgit.png
css=/mygit/res/cgit.css
favicon=/mygit/res/favicon.ico
scan-path=/path/to/git/repo/
enable-index-links=1
virtual-root=/mygit/
EOF

Add a .htaccess

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/mygit/cgit.cgi
RewriteCond %{REQUEST_URI} !^/mygit/res
RewriteCond %{REQUEST_URI} !^/mygit2/cgit.cgi
RewriteCond %{REQUEST_URI} !^/mygit2/res
RewriteRule ^(.*)$ /%{ENV:GIT_URL}/cgit.cgi/$1 [L]

Add as much RewriteCond as needed.
If you want to provide access to static files (robots.txt, etc.), add lines in this file.
The environment variable GIT_URL will be defined in the next section.

Apache

With Apache you can set environment variables. In particular, the one used by cgit.cgi to get its configuration file.

You'll have to create the <Directory> sections corresponding to your repositories.

Alias /mygit /path/to/cgit/cgi/
<Directory "/path/to/cgit/cgi/">
    SetEnv CGIT_CONFIG /path/to/cgit/configuration
    SetEnvIf Request_URI ".*$" GIT_URL=mygit
    AllowOverride all
    Options +ExecCGI +FollowSymLinks
    DirectoryIndex cgit.cgi
    AddHandler cgi-script .cgi

    AuthType Basic
    AuthName "Protection"
    AuthUserFile /path/to/.htpasswd
    Require valid-user
</Directory>

Do the same for the others directories (mygit2).

If you want to allow unauthenticated people to clone the repository, you have to provide an URL for git to access its folders.

Alias /mygit_http /path/to/git/repo/
<Directory "/path/to/git/repo/">
    Options +FollowSymLinks +Indexes
    Require all granted
</Directory>

The following command should work

git clone http://example.com/mygit_http