Moving to Nginx and Gunicorn

Selectel resumed cloud server service selectel, so I decided to create new server in new server pool and also move from lighttpd to nginx.

Server is running on Debian Squeeze Mini.


Nginx is installed from squeeze-backports repository.

From root:

echo "# squeeze-backports" >> /etc/apt/sources.list
echo "deb squeeze-backports main" >> /etc/apt/sources.list
apt-get update
apt-get -t squeeze-backports install nginx

For controlling server processes install supervisord:

apt-get install supervisor

Install virtualenv and mercurial:

apt-get install virtualenv
apt-get install mercurial

Creating user and setting up virtualenv

useradd -m -s /bin/bash blog
su blog

From blog user:

cd ~
virtualenv env
cd env
. bin/activate

Under env:

easy_install pip
pip install gunicorn

Install blohg

I use customized version, so I get it from my repository (under env):

cd ~
hg clone
cd blohg
python install

If there is an error while installing, install gcc and python-dev (under root):

apt-get install gcc
apt-get install python-dev

For stock version (under env):

pip install blohg

Configuring gunicorn

Create in ~blog:

from blohg import create_app

app = create_app("/home/blog/blog")

Clone your blog to server (as here):

hg clone my_blohg ssh://blog@yourdomain.tld/home/blog/blog/

Create in ~blog:

import multiprocessing

workers = multiprocessing.cpu_count() * 2 + 1

bind = "unix:/home/blog/blog.socket"
pidfile = "/home/blog/"
logfile = "/home/blog/logs/gunicorn.log"
loglevel = "info"
proc_name = "blog"
user = "www-data"
group = "www-data"

Configuring supervisor

Create /etc/supervisor/conf.d/blog.conf:

command = /home/blog/env/bin/gunicorn blog_app:app -c /home/blog/
directory = /home/blog/
autostart = true
autorestart = true
redirect_stderr = true
daemon = false
debug = false
logfile = /home/blog/logs/supervisor.log
loglevel = "info"
user = "www-data"

Start supervisor:

/etc/init.d/supervisor start

Configuring nginx

Create /etc/nginx/sites-available/blog:

server {
        listen 80;

        server_name yourdomain.tld;
        access_log /home/blog/logs/nginx_access.log;
        error_log /home/blog/logs/nginx_error.log;

        location / {
            proxy_pass http://unix:/home/blog/blog.socket;
            include proxy_params;

Create link in sites-enabled:

cd /etc/nginx/sites-enabled
ln -s ../sites-available/blog blog

Start nginx:

/etc/init.d/nginx start

That's all!

blog comments powered by Disqus