Deployment with Nginx and supervisord

This setup can be accomplished simply and is capable of serving a large amount of traffic. Nginx is a highly optimized HTTP server, very capable of serving static content as well as acting as a proxy between other applications and the outside world.

This section assumes you have read Deployment Environment.

Managing Your Paster Processes with Supervisord

Supervisord is a program that will manage arbitrary processes, restarting them when they fail, providing hooks for sending emails when things change, and even exposing and XML-RPC interface for determining the status of your system. It will manage your PubliForge instance.

Install it on your system:

$ sudo aptitude install supervisor

Add in /etc/supervisor/conf.d a publiforge.conf configuration file such as:

[program:PFInstance]
command         = /usr/local/virtualenv/bin/paster serve
                  /home/pfinstance/PubliForge/pfinstance.ini
                  http_port=6600
startsecs       = 5
user            = pfinstance
environment     = VIRTUAL_ENV="/usr/local/virtualenv", HGENCODING="utf-8", LANG="fr_FR.UTF-8"
redirect_stderr = true
stdout_logfile  = /var/log/supervisor/pfinstance.log

The environment variable LANG must contain the value stored in /usr/default/locale.

Now, install paster:

$ source /usr/local/virtualenv/bin/activate
(virtualenv)$ pip install PasteScript

Then, reload supervisord:

$ sudo supervisorctl reload

Now, a PubliForge instance is listening on port 6600. You can visit it (http://localhost:6600) and use supervisorctl to supervise it.

Configuring Nginx

It’s time for us to publish our application to the World Wide Web. Install Nginx on your system:

$ sudo aptitude install nginx

Nginx needs to be configured as a proxy for your application. Create a publiforge.conf and save it in /etc/nginx/conf.d. An example is shown here:

upstream pfinstance-site {
    server 127.0.0.1:6600;
}

server {
    listen       80;
    server_name  www.pfinstance.fr;

    access_log  /var/log/nginx/access_pfinstance.log;

    location / {
        proxy_set_header        Host $host;  # $host:$server_port;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;

        client_max_body_size    10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout   60s;
        proxy_send_timeout      90s;
        proxy_read_timeout      90s;
        proxy_buffering         off;
        proxy_temp_file_write_size 64k;
        proxy_pass              http://pfinstance-site;
        proxy_redirect          off;
    }

    location /Static {
        root        /usr/local/virtualenv/lib/python2.7/site-packages/PubliForge-2.3.0-py2.7-linux-x86_64.egg/publiforge;
        expires     2d;
        add_header  Cache-Control public;
        access_log  off;
    }
}

server {
    server_name  www.pfinstance.org  www.pfinstance.com;
    rewrite      ^ http://www.pfinstance.fr/ permanent;
}

Read the configuration documentation of Nginx for more options.

The location /Static block will greatly improve performance because requests for this content (images, CSS and JavaScript) will not need to be proxied to PubliForge application and can be served directly. Correct root path according to your environment.

Your can now visit the URL used for server_name parameter (here http://www.pfinstance.fr).