Skip to content

Instantly share code, notes, and snippets.

@wilcorrea
Forked from karmi/git-hosting-apache.conf
Created November 9, 2016 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilcorrea/d19d693274112b35c72b31ecd80b723d to your computer and use it in GitHub Desktop.
Save wilcorrea/d19d693274112b35c72b31ecd80b723d to your computer and use it in GitHub Desktop.
Configuration for hosting Git repositories with Apache 2.x
# ----------------------------------------------------------
# Configuration for hosting Git repositories with Apache 2.x
# ----------------------------------------------------------
#
# This setup provides "dual URLS", where URL like <http://git.example.com/my_repository.git>
# loads Gitweb in the browser and the same URL can be used in commands like `git clone` and `git remote add`.
# It was compiled from some sources on the internet and further customized/tuned.
#
# Please see documentation for:
#
# 1. `git-http-backend`, <http://www.kernel.org/pub/software/scm/git/docs/git-http-backend.html>
# 2. `gitweb`, <http://repo.or.cz/w/alt-git.git?a=blob_plain;f=gitweb/README>
#
# Also see Scott Chacon's "Smart HTTP Transport", <http://progit.org/2010/03/04/smart-http.html>
<VirtualHost *:80>
# Configure hostname
ServerAdmin you@example.com
ServerName git.example.com
# Configure repository location
DocumentRoot /data/www/example.com/git
# Deny to access to .htaccess etc
<Files ~ "(^\.htaccess|^.*\.conf)">
Order allow,deny
Deny from all
</Files>
# OPTIONAL: Completely deny pushing over HTTP
<LocationMatch "git-receive-pack">
Order Allow,Deny
Deny from all
</LocationMatch>
# OPTIONAL: Require authentication for access
# See http://httpd.apache.org/docs/2.2/mod/mod_auth_basic.html
<Location />
AuthType Basic
AuthName "Git Repositories"
AuthUserFile /path/to/file # For `htpasswd` command
Require valid-user
Order allow,deny
Allow from all
</Location>
# Enable cgi-bin scripts
Options ExecCGI FollowSymLinks
AddHandler cgi-script cgi
# Load configuration for Gitweb <https://git.wiki.kernel.org/index.php/Gitweb>
SetEnv GITWEB_CONFIG /etc/gitweb.conf
# Configure Git HTTP Backend
SetEnv GIT_PROJECT_ROOT /www/example.com/git
SetEnv GIT_HTTP_EXPORT_ALL
# Note: Serve static files directly
AliasMatch ^/(.*/objects/[0-9a-f]{2}/[0-9a-f]{38})$ /var/www/git/$1
AliasMatch ^/(.*/objects/pack/pack-[0-9a-f]{40}.(pack|idx))$ /var/www/git/$1
# Note: Serve repository objects with Git HTTP backend
ScriptAliasMatch \
"(?x)^/(.*/(HEAD | \
info/refs | \
objects/info/[^/]+ | \
git-(upload|receive)-pack))$" \
/usr/libexec/git-core/git-http-backend/$1
# Note: Serve HTML with Gitweb
ScriptAlias / gitweb/gitweb.cgi
# Turning on mod rewrite
RewriteEngine on
# Make the front page an internal rewrite to the gitweb script
RewriteRule ^/$ /gitweb/gitweb.cgi [L]
# Make shortcut for a repostitory, so URL like <http://git.example.com/my_repository.git>
# loads proper repository in Gitweb
RewriteRule ^/(\w+\.git)$ /?p=$1 [L,P]
# Configure location logs
CustomLog /var/log/apache2/git.example.com-access.log multi
ErrorLog /var/log/apache2/git.example.com-error.log
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment