Skip to content

Instantly share code, notes, and snippets.

@mundry
Last active April 8, 2018 17:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mundry/6bd24edac461fe869289 to your computer and use it in GitHub Desktop.
Save mundry/6bd24edac461fe869289 to your computer and use it in GitHub Desktop.
Temporary installation of cgit with lighttpd for testing.

Install lighttpd in a temporary location.

curl -o {/tmp,http://download.lighttpd.net/lighttpd/releases-1.4.x}/lighttpd-1.4.45.tar.xz
tar xf /tmp/lighttpd-1.4.45.tar.xz -C /tmp
cd /tmp/lighttpd-1.4.45
./configure --prefix=/tmp/lighttpd
make -j8
make install

Basic configuration for lighttpd in general and cgit specifically.

mkdir -p /tmp/lighttpd/conf{,.d}
cat <<'EOF' > /tmp/lighttpd/conf/lighttpd.conf
server.document-root = "/tmp/lighttpd"

server.port = 8000

mimetype.assign = (
  ".html" => "text/html",
  ".txt" => "text/plain",
  ".jpg" => "image/jpeg",
  ".png" => "image/png",
  ".css" => "text/css"
)

include "/tmp/lighttpd/conf.d/cgit.conf"
EOF
cat <<'EOF' > /tmp/lighttpd/conf.d/cgit.conf
server.modules += ("mod_redirect",
                   "mod_alias",
                   "mod_cgi",
                   "mod_fastcgi",
                   "mod_rewrite")

$HTTP["url"] =~ "^/" {
    server.document-root = "/tmp/cgit"
    server.indexfiles = ("cgit.cgi")
    cgi.assign = ("cgit.cgi" => "")
    # Rewrite any other url but the asset urls to be handled by the cgit
    # executable. Use with virtual-root=/ cgitrc setting.
    url.rewrite-once = ("^/((?!cgit\.(css|png)$).*)$" => "/cgit.cgi/$1")
}
EOF

Setup the cgit source.

git clone -o zx2c4 git://git.zx2c4.com/cgit /tmp/cgit-src
cd /tmp/cgit-src
git submodule init && git submodule update

Change the default installation paths to the temporary ones.

cat <<'EOF' > /tmp/cgit-src/cgit.conf
CGIT_SCRIPT_PATH = /tmp/cgit
CGIT_DATA_PATH = $(CGIT_SCRIPT_PATH)
CGIT_CONFIG = $(CGIT_DATA_PATH)/etc/cgitrc
CACHE_ROOT = $(CGIT_DATA_PATH)/cache
prefix = $(CGIT_DATA_PATH)
EOF
make -j8
make install
mkdir -p /tmp/cgit/{cache,etc}
cat <<'EOF' > /tmp/cgit/etc/cgitrc
virtual-root=/
scan-path=/tmp/cgit/cache
auth-filter=/tmp/cgit/lib/cgit/filters/simple-authentication.py
EOF

Start the lighttpd server with

/tmp/lighttpd/sbin/lighttpd -D -f /tmp/lighttpd/conf/lighttpd.conf

FastCGI

Modern web servers don't support CGI anymore, but instead FastCGI. So an extra layer is needed to bridge from FastCGI to CGI. The following section will describe how to set up cgit with both Nginx and H2O. H2O ships its own utility script to support CGI applications with its FastCGI interface. After the installation the utility script is located at $PREFIX/share/h2o/fastcgi-cgi (source). The script can be given the option --listen=<path> to define the path of the socket file used to communicate with the CGI application.

Nginx can be used with this script too.

fcgiwrap

Alternatively to H2O's script fcgiwrap is another option.

Fcgiwrap can be installed from source. To compile fcgiwrap libfcgi is required to be installed (fcgi-devel on RedHat, libfcgi-dev on Debian)

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

Additionally a simple script is needed to spawn a new FastCGI process whenever a new request is received. One example of such a script might look as follows:

#!/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";
}

Alternatively spawn-fcgi provides a C implementation of the same functionality.

H2O's utility script combines both fcgiwrap and the spawn script.

Web Server Configurations

The following are rudimentary, example configurations. They both work with either of the FCGI methods described above.

Nginx

location ~* ^.+\.(css|png|ico)$ {
    root /tmp/cgit;
}

location / {
    fastcgi_pass   unix:/tmp/fcgi.sock;
    fastcgi_param  SCRIPT_FILENAME /tmp/cgit/cgit.cgi;
    fastcgi_param  PATH_INFO       $uri;
    include        fastcgi_params;
}

H2O

hosts:
    "localhost:8000":
        listen:
            port: 8000
        paths:
            "/cgit.css":
                file.file: /tmp/cgit/cgit.css
            "/cgit.png":
                file.file: /tmp/cgit/cgit.png
            "/":
                fastcgi.connect:
                    port: /tmp/fcgi.sock
                    type: unix
                setenv:
                    SCRIPT_FILENAME: /tmp/cgit/cgit.cgi
                    GATEWAY_INTERFACE: CGI/1.1

access-log: /tmp/access.log
error-log: /tmp/error.log
pid-file: /tmp/h2o.pid

Launch

First the FastCGI layer needs to be started, e.g:

$ $PREFIX/share/h2o/fastcgi-cgi

or

$ spawn.pl

Next the web server needs to be started. Either

$ nginx

or

$ h2o -m daemon -c h2o.yml

Resources

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