Skip to content

Instantly share code, notes, and snippets.

@LastDragon-ru
Created January 12, 2014 15:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LastDragon-ru/8385818 to your computer and use it in GitHub Desktop.
Save LastDragon-ru/8385818 to your computer and use it in GitHub Desktop.
git-http-backend + nginx (RHEL/Centos)

Requirements:

  • fcgiwrap
  • spawn-fcgi
  • nginx
  • git

Repository setup (without anonymous access):

# cd /var/www/git.example.com
# git init --bare testrepo.git
# cd testrepo.git
# git config http.getanyfile false
#!/bin/sh
#
# git-fcgi The Git HTTP/FastCGI server
#
# chkconfig: - 80 20
# processname: git-fcgi
# description: Git HTTP/FastCGI server
# pidfile: /var/run/git-fcgi.pid
### BEGIN INIT INFO
# Provides: git-fcgi
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop Git HTTP/FastCGI server
### END INIT INFO
# Source function library.
. /etc/init.d/functions
# Config & Vars
prog=git-fcgi
childs=1
pidfile=/var/run/git-fcgi.pid
lockfile=/var/lock/subsys/git-fcgi
sockfile=/var/run/git-fcgi.sock
sockmode=0700;
sockuser=nginx
sockgroup=nginx
proguser=git
proggroup=git
gitexec=/usr/libexec/git-core/git-http-backend
fcgiexec=/usr/sbin/fcgiwrap
spawnexec=/usr/bin/spawn-fcgi
progexec="${spawnexec} -u ${proguser} -g ${proggroup} -U ${sockuser} -G ${sockgroup} -P ${pidfile} -s ${sockfile} -M ${sockmode} -- ${fcgiexec} -f -c ${childs} -p ${gitexec}"
RETVAL=0
# Functions
start() {
echo -n $"Starting ${prog}: "
[ -n "${sockfile}" -a -S "${sockfile}" ] && rm -f ${sockfile}
daemon "${progexec} > /dev/null"
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping ${prog}: "
[ -n "${sockfile}" -a -S "${sockfile}" ] && rm -f ${sockfile}
killproc -p ${pidfile} ${prog}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
return $RETVAL
}
restart() {
stop
start
}
reload() {
restart
}
force_reload() {
restart
}
rh_status() {
status -p ${pidfile} ${prog}
}
# Main
case "$1" in
start)
rh_status > /dev/null 2>&1 && exit 0
start
;;
stop)
stop
;;
status)
rh_status
RETVAL=$?
;;
restart)
restart
;;
reload)
reload
;;
force-reload)
force_reload
;;
condrestart|try-restart)
if rh_status > /dev/null 2>&1; then
restart
fi
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|force_reload|condrestart|try-restart|status|help}"
RETVAL=2
esac
exit $RETVAL
# Git
server {
listen 80;
server_name git.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name git.example.com;
root /var/www/git.example.com;
error_log /var/log/nginx/git.example.com.error.log warn;
access_log /var/log/nginx/git.example.com.access.log main;
add_header Strict-Transport-Security "max-age=86400";
ssl_certificate /path/to/git.example.com.ssl.crt;
ssl_certificate_key /path/to/git.example.com.ssl.key;
auth_basic "The Private Git Server;"
auth_basic_user_file /path/to/htpasswd;
location ~ ^/([^/]+\.git)(/.*|$) {
include fastcgi_params;
fastcgi_param PATH_INFO $uri;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
fastcgi_param GIT_PROJECT_ROOT $document_root;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_pass unix:/var/run/git-fcgi.sock;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment