Setting up cgit + nginx + debian

Updated: Feb 20, 2018 by Pradeep Gowda.
Tagged: git, linux

Goal:

Requirements:

sudo apt-get install build-essential
                  autoconf
                  automake
                  libtool
                  libfcgi-dev
                  spawn-fcgi
                  fcgiwrap

Clone cgit repo:

git clone git://git.zx2c4.com/cgit
cd cgit
git submodule init
git submodule update
make -j4
sudo make install

These files will be installed:

install -m 0755 -d /var/www/htdocs/cgit
install -m 0755 cgit /var/www/htdocs/cgit/cgit.cgi
install -m 0755 -d /var/www/htdocs/cgit
install -m 0644 cgit.css /var/www/htdocs/cgit/cgit.css
install -m 0644 cgit.png /var/www/htdocs/cgit/cgit.png
install -m 0644 favicon.ico /var/www/htdocs/cgit/favicon.ico
install -m 0644 robots.txt /var/www/htdocs/cgit/robots.txt
install -m 0755 -d /usr/local/lib/cgit/filters
cp -r  filters/* /usr/local/lib/cgit/filters

Create the /etc/cgitrc configuration file with the following content:

Create /usr/local/bin/cgit-fcgiwrap with this content:

1
2
3
4
5
6
7
8
9
#!/bin/bash

FASTCGI_USER=www-data
FASTCGI_GROUP=www-data
SOCKET=/var/run/cgit-fastcgi/cgit-fastcgi.socket
PIDFILE=/var/run/cgit-fastcgi/cgit-fastcgi.pid
CGIT=/usr/local/bin/fcgiwrap

/usr/bin/spawn-fcgi -s $SOCKET -P $PIDFILE -u $FASTCGI_USER -g $FASTCGI_GROUP -- $CGIT

Create /etc/init.d/cgit-fastcgi with

#!/bin/bash
### BEGIN INIT INFO
# Provides: cgit-fastcgi
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop cgit-fastcgi
# Description: cgit-fastcgi
### END INIT INFO

CGIT_SCRIPT=/usr/local/bin/cgit-fcgiwrap
FASTCGI_USER=www-data
FASTCGI_GROUP=www-data
PID_DIR=/var/run/cgit-fastcgi
PID_FILE=/var/run/cgit-fastcgi/cgit-fastcgi.pid
RET_VAL=0

case "$1" in
  start)
    if [[ ! -d $PID_DIR ]]
    then
      mkdir $PID_DIR
      chown $FASTCGI_USER:$FASTCGI_GROUP $PID_DIR
      chmod 0770 $PID_DIR
    fi
    if [[ -r $PID_FILE ]]
    then
      echo "cgit-fastcgi already running with PID `cat $PID_FILE`"
      RET_VAL=1
    else
      $CGIT_SCRIPT
      RET_VAL=$?
    fi
  ;;
  stop)
    if [[ -r $PID_FILE ]]
    then
      kill `cat $PID_FILE`
      rm $PID_FILE
      RET_VAL=$?
    else
      echo "Could not find PID file $PID_FILE"
      RET_VAL=1
    fi
  ;;
  restart)
    if [[ -r $PID_FILE ]]
    then
      kill `cat $PID_FILE`
      rm $PID_FILE
      RET_VAL=$?
    else
      echo "Could not find PID file $PID_FILE"
    fi
    $CGIT_SCRIPT
    RET_VAL=$?
  ;;
  status)
    if [[ -r $PID_FILE ]]
    then
      echo "cgit-fastcgi running with PID `cat $PID_FILE`"
      RET_VAL=$?
    else
      echo "Could not find PID file $PID_FILE, cgit-fastcgi does not appear to be running"
    fi
  ;;
  *)
    echo "Usage: cgit-fastcgi {start|stop|restart|status}"
    RET_VAL=1
  ;;
esac

exit $RET_VAL

Make it executable and mark it to start at system boot.

sudo chmod +x /etc/init.d/cgit-fastcgi
sudo update-rc.d cgit-fastcgi defaults

Create the nginx config file: /etc/nginx/sites-available/cgit

server {
  listen      80;
  server_name code.btbytes.com;

  access_log  /var/log/nginx/code.btbytes.com-access.log;
  error_log   /var/log/nginx/code.btbytes.com-error.log;

  location /git {
    gzip off;
    alias /usr/local/cgit/share;

    # We'd like to have static files like cgit.{png,css} in the same
    # (virtual) directory
    try_files $uri @cgit;
  }

  location @cgit {
    fastcgi_pass   unix:/var/run/cgit-fastcgi/cgit-fastcgi.socket;

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    # Tell nginx to consider everything after /git as PATH_INFO. This way
    # we get nice, clean URLs
    fastcgi_split_path_info           ^(/git)(/?.+)$;

    # Unfortunately the version of fcgiwrap currently available in Debian
    # squeeze removes the PATH_INFO variable from the CGI environment and
    # sets a new one based on DOCUMENT_ROOT and SCRIPT_NAME, so the line
    # below won't work
    #fastcgi_param  PATH_INFO         $fastcgi_path_info;
    # Tell fcgiwrap about the binary we'd like to execute and cgit about
    # the path we'd like to access.
    fastcgi_param  SCRIPT_NAME        /cgit.cgi$fastcgi_path_info;
    fastcgi_param  DOCUMENT_ROOT      /usr/local/cgit/bin;
  }
}

To enable highlight filters:

sudo apt-get install highlight

References