Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Step-by-step installation of cGit with Nginx

How to install cGit on Nginx (Ubuntu server)

Step-by-step installtion of cGit on nginx without funky rewrite rules.

Pre-requisites

This is for

sudo aptitude install build-essential
                      autoconf
                      automake
                      libtool
                      libfcgi-dev
                      spawn-fcgi
                      fcgiwrap
git clone https://github.com/gnosek/fcgiwrap.git
git clone git://hjemli.net/pub/git/cgit

Setting up fcgiwrap

Now lets install fcgiwrap. Alternatively, you can

git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap/
autoreconf -i
./configure
make
sudo make install
cp fcgiwrap /usr/bin/.

Setting up spawn-fcgi

Then, I pasted this perl script into /usr/bin/spawn-fcgi which will create a socket to pass .cgi to it

cat > /usr/bin/spawn-fcgi
#!/usr/bin/perl

use strict;
use warnings FATAL => qw( all );

use IO::Socket::UNIX;

my $bin_path = '/usr/bin/fcgiwrap';
my $socket_path = $ARGV[0] || '/tmp/cgi.sock';
my $num_children = $ARGV[1] || 1;

close STDIN;

unlink $socket_path;
my $socket = IO::Socket::UNIX->new(
    Local => $socket_path,
    Listen => 100,
);

die "Cannot create socket at $socket_path: $!\n" unless $socket;

for (1 .. $num_children) {
    my $pid = fork;
    die "Cannot fork: $!" unless defined $pid;
    next if $pid;

    exec $bin_path;
    die "Failed to exec $bin_path: $!\n";
}

Then make sure to give it executable permissions

chmod +x /usr/bin/spawn-fcgi

The following script will be use to automate the respawning of FastCGI(fcgi) socket

cat > /etc/init.d/spawn-fcgi
#!/bin/bash
C_SCRIPT=/usr/bin/spawn-fcgi
USER=www-data
GROUP=www-data
RETVAL=0
case "$1" in
        start)
                echo "Starting fastcgi"
                sudo -u $USER $C_SCRIPT
                chown $USER:$GROUP /tmp/cgi.sock
                RETVAL=$?
  ;;
        stop)
                echo "Stopping fastcgi"
                killall -9 fcgiwrap
                RETVAL=$?
  ;;
        restart)
                echo "Restarting fastcgi"
                killall -9 fcgiwrap
                $sudo -u $USER $C_SCRIPT
                RETVAL=$?
  ;;
        *)
                echo "Usage: $0 {start|stop|restart}"
                exit 1
  ;;
esac
exit $RETVAL

Again, make that executable and start it up!

chmod +x /etc/init.d/spawn-fcgi
sudo /etc/init.d/spawn-fcgi start

After you start the spawn-fcgi you should see a new file in /tmp folder with the name cgi.sock. That file is our socket to pass .cgi scripts to.

Configuring nginx fcgi_param's

The next step is optional, but I like it because it neatens things up a bit.

cat > /etc/nginx/fastcgi_params
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  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
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;
fastcgi_param  REMOTE_USER        $remote_user;

Setting up cGit

Go into the cgit directory that you cloned in the pre-req's and run the following cmomands

git submodule init
git submodule update
make
sudo make install

This will have installed cgit.cgi cgit.png and cgit.css into /var/www/htdocs/cgit/. That should be fine if you have all the correct permissions set up.

# prepend this string to every url
virtual-root=/

# title, heading, desc, about...
root-title=Intecs git repositories
root-desc=here lies our code!
root-readme=/files/web/example.com/code/about.html


# styling
css=/htdocs/cgit/cgit.css
logo=/htdocs/cgit/cgit.png


#
# "STATIC" REPOSITORIES
#
# Add them one by one, or include a file...
section=Hosted repos

# First repository
repo.url=<project_name>
repo.owner=<user>
repo.path=/path/to/git_repo/.git
repo.desc=A test project.

Now here is where we configure nginx to run cgit

server {
    listen 80;
    server_name localhost;

    # Serve static files
    location ~* ^.+\.(css|png|ico)$ {
        root /var/www/htdocs/cgit;
        expires 30d;
    }

    location / {
        fastcgi_pass   unix:/tmp/cgi.sock;
        fastcgi_param   SCRIPT_FILENAME /var/www/htdocs/cgit/cgit.cgi;
        fastcgi_param   PATH_INFO       $uri;
        fastcgi_param   QUERY_STRING    $args;
    }

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log warn;
}

References

@KubaKaszycki

This comment has been minimized.

Copy link

@KubaKaszycki KubaKaszycki commented Jun 27, 2016

Nice manual, I used it, it works. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment