gunicornインストール

gunicornをインストールしてみた。
フロントにはNginxを使っている。
Nginxインストール - i2bsの日記

gunicornインストール

apt-get install python-dev
wget -c http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py
easy_install pip
pip install gunicorn

Hello world

vi test.py
# -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.

def app(environ, start_response):
    """Simplest possible application object"""
    data = 'Hello, World!\n'
    status = '200 OK'
    response_headers = [
        ('Content-type','text/plain'),
        ('Content-Length', str(len(data)))
    ]
    start_response(status, response_headers)
    return iter([data])
gunicorn --workers=2 test:app --bind=HOST:PORT

gunicornユーザ作成

groupadd -g 2002 gunicorn
useradd -u 2002 -g gunicorn -d /home/gunicorn -m -s /bin/bash gunicorn

ディレクトリ作成

mkdir -p /usr/local/gunicorn/{conf,logs}

設定ファイル作成

vi /usr/local/gunicorn/conf/gunicorn.conf.py
## Server Socket
# bind = '127.0.0.1:xxxxx'
# bind = 'xx.xx.xx.xx:xxxxx'
bind = 'unix:/tmp/gunicorn.sock'

backlog = 2048

## Worker Processes
workers = 2
worker_class = 'sync'
worker_connections = 1000
max_requests = 1000
timeout = 30
keepalive = 2

debug = False
spew = False

## Server Mechanics
preload_app = True
daemon = True
pidfile = '/usr/local/gunicorn/logs/gunicorn.pid'
user = 'gunicorn'
group = 'gunicorn'
umask = 0002

## Logging
logfile = '/usr/local/gunicorn/logs/gunicorn.log'
loglevel = 'info'
logconfig = None

## Process Naming
proc_name = 'gunicorn'

起動

chown -R gunicorn. /usr/local/gunicorn
gunicorn --config /usr/local/gunicorn/conf/gunicorn.conf.py test:app

reload

kill -HUP `cat /usr/local/gunicorn/logs/gunicorn.pid`

Nginxの設定

vi /usr/local/nginx/conf/nginx.conf
...
http {
   ...
   include app_server_proxy.conf;
}
vi /usr/local/nginx/conf/app_server_proxy.conf
upstream app_server {
    server unix:/tmp/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name xxx.co.jp;
    root /usr/local/nginx/vhosts/xxx.co.jp/htdocs;

    access_log /usr/local/nginx/vhosts/xxx.co.jp/logs/access.log main;
    error_log /usr/local/nginx/vhosts/xxx.co.jp/logs/error.log info;

    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;

        proxy_pass http://app_server;
    }
}