更新: | 2013-11-09 |
---|---|
バージョン: | 0.0.0 |
作者: | @voluntas |
URL: | http://voluntas.github.io/ |
Django 前提
url: | http://gunicorn.org/ |
---|
割愛
- Django を Gunicorn で起動出来る
- Gunicorn の設定を最小限理解出来る
- Nginx + Gunicorn の連携を理解することが出来る
- Ansible から Gunicorn を組み込むことが出来る
$ pip install gunicorn setproctitle
settings.py
INSTALLED_APPS = ( ... 'gunicorn', ... )
app_name: | levy |
---|
gunicorn_levy.conf.py
import multiprocessing # Server Socket bind = 'unix:/tmp/gunicorn_levy.sock' backlog = 2048 # Worker Processes workers = multiprocessing.cpu_count() * 2 + 1 worker_class = 'sync' worker_connections = 1000 max_requests = 0 timeout = 30 keepalive = 2 debug = False spew = False # Logging logfile = '/var/log/gunicorn/levy.log' loglevel = 'info' logconfig = None # Process Name proc_name = 'gunicorn_levy'
$ python manage.py run_gunicorn --config gunicorn_levy.conf.py 2013-11-09 06:29:40 [8620] [INFO] Starting gunicorn 18.0 2013-11-09 06:29:40 [8620] [INFO] Listening at: unix:/tmp/gunicorn_levy.sock (8620) 2013-11-09 06:29:40 [8620] [INFO] Using worker: sync 2013-11-09 06:29:40 [8624] [INFO] Booting worker with pid: 8624 2013-11-09 06:29:40 [8625] [INFO] Booting worker with pid: 8625 2013-11-09 06:29:40 [8626] [INFO] Booting worker with pid: 8626 2013-11-09 06:29:40 [8627] [INFO] Booting worker with pid: 8627 2013-11-09 06:29:40 [8628] [INFO] Booting worker with pid: 8628 2013-11-09 06:29:40 [8629] [INFO] Booting worker with pid: 8629 2013-11-09 06:29:40 [8630] [INFO] Booting worker with pid: 8630 2013-11-09 06:29:40 [8631] [INFO] Booting worker with pid: 8631 2013-11-09 06:29:40 [8632] [INFO] Booting worker with pid: 8632
$ sudo groupadd nginx $ sudo useradd -g nginx nginx $ sudo usermod -s /bin/false nginx
$ curl -O http://nginx.org/download/nginx-1.4.3.tar.gz $ tar xvfz nginx-1.4.3.tar.gz $ cd nginx-1.4.3 $ ./configure --prefix=/opt/nginx/1.4.3 \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_gzip_static_module \ --http-log-path=/var/log/nginx/access.log $ make $ make install $ sudo chown nginx:nginx /opt/nginx/1.4.3/* $ sudo chown nginx:nginx /var/log/nginx/* $ sudo chown nginx:nginx /var/run/nginx/*
nginx.conf:
user nginx nginx; worker_processes 1; events { worker_connections 1024; accept_mutex off; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; tcp_nopush on; keepalive_timeout 5; gzip on; include levy.conf; }
nginx/levy.conf:
upstream levy-backend { server unix:/tmp/gunicorn_levy.sock fail_timeout=0; } server { listen 80; server_name levy.example.com; access_log /var/log/nginx/levy.example.com-access.log main; error_log /var/log/nginx/levy.example.com-error.log info; location ~ /media/(.*)$ { alias /opt/local/lib/python2.7/site-packages/django/contrib/admin/media/$1; break; } location / { proxy_pass http://levy-backend; break; } }
$ sudo sbin/nginx -c nginx.conf
[program:gunicorn] command=/path/to/gunicorn main:application -c /path/to/gunicorn.conf.py directory=/path/to/project user=nobody autostart=true autorestart=true redirect_stderr=True
未検証
$ ansible-playbook gunicorn.yml
gunicorn.yml templates/ gunicorn.conf.py.j2
template: src=templates/gunicorn.conf.py.j2 dest=/etc/gunicorn.conf.py owner=bin group=wheel mode=0644
templates/gunicorn.conf.py.j2
import multiprocessing
# Server Socket
bind = 'unix:/tmp/{{ app_name }}.sock'
backlog = 2048
# Worker Processes
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = 'sync'
worker_connections = 1000
max_requests = 0
timeout = 30
keepalive = 2
debug = False
spew = False
# preload_app = True
# dameon = False
# pidfile = ''
# user = ''
# group = ''
# umask = 0002
# Logging
logfile = '/var/log/gunicorn/{{ app_name }}.log'
loglevel = 'info'
logconfig = None
# Process Name
proc_name = '{{ app_name }}'
supervisor.d/gunicorn_{{ app_name }}.j2
[program:gunicorn] command=/path/to/gunicorn main:application -c /path/to/gunicorn.conf.py directory=/path/to/project user=nobody autostart=true autorestart=true redirect_stderr=True
- Configuration Files for Nginx + Gunicorn + Supervisord
- https://gist.github.com/dstufft/997475
- nginxとgunicornとsupervisorを連携させる - saito’s blog
- http://d.hatena.ne.jp/saitodevel01/20110811/1313019218
- Deploying Gunicorn — Gunicorn 18.1 documentation
- http://docs.gunicorn.org/en/latest/deploy.html
- Serving multiple Django applications with Nginx and Gunicorn - Michał Karzyński
- http://michal.karzynski.pl/blog/2013/10/29/serving-multiple-django-applications-with-nginx-gunicorn-supervisor/