Skip to content
4pr0n edited this page Mar 13, 2014 · 3 revisions

Overview

Checks out the site, installs nginx, fcgiwrap, and python, and sets up nginx to run the site through fcgiwrap on /var/www.

Pre-requisites

Setup was done as root, your mileage may vary

  • Sudo access to a Debian/Ubuntu linux machine with internet access

Install dependencies

# Install git - To checkout the code in this repo
apt-get install git

# Install nginx - The webserver
apt-get install nginx

# Install FCGIWrap - Needed to allow nginx to run Python (cgi) scripts
apt-get install fcgiwrap

# Install python & PIL (Python Imaging Library)
# These are needed to run scripts and create thumbnails
apt-get install python2.7
apt-get install python-imaging

Check out the code

Create a directory in /var/www and set the owner to be www-data (user nginx is run under). Then puts the site's code in the directory.

cd /var/
mkdir www && chown www-data www
git clone https://github.com/4pr0n/rip.git .

Configure fcgiwrap (FCGI)

Set up fastcgiwrap to be enabled on load, use 4 preforks (workers), and the location of the sock file.

echo 'fcgiwrap_enable="YES"' >> /etc/rc.conf
echo 'fcgiwrap_flags="-c 4"' >> /etc/rc.conf
echo 'fcgiwrap_profiles="nginx"' >> /etc/rc.conf
echo 'fcgiwrap_nginx_socket="unix:/tmp/fcgi.sock"' >> /etc/rc.conf

Configure nginx

Setup nginx to start automatically:

echo 'nginx_enable="YES"' >> /etc/rc.conf

Create a new nginx configuration file for your site at /usr/local/etc/nginx/rip.conf (or /etc/nginx/conf.d/rip.conf):

server {
    listen 80;             # Port 80
    server_name localhost; #
    root /var/www; # Or the location of the rip repo
    autoindex on;          # 

    ## You may need to create /var/log/nginx
    access_log /var/log/nginx/rip_access.log combined;
    error_log /var/log/nginx/rip_error.log info;
    log_not_found off;

    location / {
        try_files $uri $uri/ @proxy;
    }
    location ~ (\.cgi|\.py|\.sh|\.pl|\.lua)$ {
        gzip off;       # Don't gzip scripts
        root /var/www;  # Run scripts in root directory
        autoindex on;   #
        fastcgi_pass  unix:/tmp/fcgi.sock; # fcgi socket passthrough
        include /etc/nginx/fastcgi_params; # Pull in FCGI Parameters
        fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
    }
}

Include rip.conf in the main nginx.conf

Edit the /etc/nginx/nginx.conf file to include the rip.conf file:

  • Open /etc/nginx/nginx.conf and under http { put:
  • include conf.d/rip.conf or the location of the rip.conf file from above

Start nginx

Then start nginx with /usr/local/etc/rc.d/nginx start

The rip.conf configuration should be immediately tested and errors will display if there are any.

Test

Visit http://<server_ip>/ and see if it worked!

"200 Bad Gateway"

This error can happen when the fcgi socket file (/tmp/fcgi.sock) does not have proper permissions. To fix, you can chown www-data /tmp/fcgi.sock (using the user nginx runs under in place of www-data).


Alternative to fcgiwrap

This script starts the fcgiwrap workers. Be sure to kill any pre-existing workers with killall fcgiwrap, or ps aux | grep fcgiwrap to see if there are any running.

#!/usr/bin/perl

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

use IO::Socket::UNIX;

my $bin_path = '/usr/sbin/fcgiwrap'; 
my $socket_path = $ARGV[0] || '/tmp/fcgiwrap.nginx.socket';
my $num_children = $ARGV[1] || 5;

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;
chmod 0666, $socket_path;

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";
}