Production Setup with Gunicorn, Nginx, and Supervisor¶
Production setup of the Kegbot server consists of the following components:
- Nginx (public facing web server)
- Gunicorn (internal HTTP application server)
- supervisord (process control and monitor)
This chapter will guide you though running these two components.
Note
It is possible to use other components in place of the ones recommended here; you’ll need to figure out those steps yourself.
Gunicorn¶
Starting with version 0.9.4, Gunicorn is automatically installed with Kegbot
Server. You can test it with the built-in command, run_gunicorn
:
(kb) $ kegbot run_gunicorn --debug
If this works, you’re ready to fire up nginx.
Nginx¶
Install Nginx¶
Nginx is a fast and flexible HTTP server written in C. You can install it with
the nginx
package on Ubuntu:
$ sudo apt-get install nginx
After nginx is installed and started, a default HTTP server will be running on port 80.
Create Kegbot nginx config¶
Kegbot includes a sample nginx configuration file in the deploy/
directory
which needs to be tailored to your setup. The contents are also included below:
# Kegbot nginx.conf file
#
# Instructions:
# - Replace "kegbot.example.com" with your external host name
# - Replace "/data/kegbot/www/media" with the path you configured for
# MEDIA_ROOT
# - Replace "/data/kegbot/www/static" with the path you configured for
# STATIC_ROOT
# - Replace "/data/kegbot/www" with that parent directory name.
# - Replace ":8000" with the port number of the Kegbot application server, if
# changed.
upstream kegbot {
server 127.0.0.1:8000;
}
server {
listen 80;
tcp_nopush on;
tcp_nodelay on;
gzip on;
gzip_disable "msie6";
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
keepalive_timeout 0;
client_max_body_size 10m;
location / {
proxy_redirect off;
proxy_set_header 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-Protocol $scheme;
proxy_pass http://kegbot;
}
location /media/ {
alias /data/kegbot/www/media/;
access_log off;
log_not_found off;
expires 7d;
add_header pragma public;
add_header cache-control "public";
}
location /static/ {
alias /data/kegbot/www/static/;
access_log off;
log_not_found off;
expires 7d;
add_header pragma public;
add_header cache-control "public";
}
location /robots.txt {
root /data/kegbot/www/static/;
access_log off;
log_not_found off;
}
location /favicon.ico {
root /data/kegbot/www/static/;
access_log off;
log_not_found off;
}
}
Create a copy of this file, editing the paths noted in the comments at the top. Finally, install it:
$ sudo cp kegbot-nginx.conf /etc/nginx/sites-available/
$ sudo rm /etc/nginx/sites-enabled/default
$ sudo ln -s /etc/nginx/sites-available/kegbot-nginx.conf /etc/nginx/sites-enabled/
$ sudo service nginx restart
supervisord¶
Install supervisor using apt-get
:
$ sudo apt-get install supervisor
Supervisor manages programs according to its configuration files. Once again,
there is a template in the deploy/
directory:
# Supervisor configuration for Kegbot server.
#
# Instructions:
# - Replace "/data/kegbot/kb/bin" with the installed path of the kegbot
# programs.
# - Replace "user=ubuntu" with the username you wish to run the programs.
# - Edit paths.
# - Copy to /etc/supervisor/conf.d/kegbot.conf
[group:kegbot]
programs=gunicorn,workers
[program:gunicorn]
command=/data/kegbot/kb/bin/gunicorn pykeg.web.wsgi:application -w 3
directory=/data/kegbot
user=ubuntu
autostart=true
autorestart=true
redirect_stderr=true
[program:workers]
command=/data/kegbot/kb/bin/kegbot run_workers
directory=/data/kegbot
stopasgroup=true
user=ubuntu
autostart=true
autorestart=true
redirect_stderr=true
Make a copy of this file, editing the fields as noted, and install it:
$ sudo cp kegbot-supervisor.conf /etc/supervisor/conf.d/kegbot.conf
Restart supervisor so that it reads the Kegbot config:
$ sudo service supervisor restart
Finally, launch the web server and workers:
$ sudo supervisorctl start kegbot:gunicorn
$ sudo supervisorctl start kegbot:workers