Skip to content

Using uWSGI with Nginx

Lou Wolford edited this page Sep 22, 2016 · 1 revision

Nginx is a high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server that in this case will be used for serving our static files. uWSGI is both a protocol and server that allows Python applications to communicate with web servers like Nginx. Using these tools with the LRS is a large step up from the development web server Django includes. It will allow faster processing of more data and is openly configurable to fit your needs.

uWSGI

To setup uWSGI:

  1. In your virtualenv: (env)admin:~$ pip install uwsgi

  2. Create a directory to keep your uWSGI .ini files (uWSGI Emperor refers to them as vassals). In this example we'll create it at /etc/uwsgi/vassals. You will need to use sudo for admin privileges.

  3. Here is an example uwsgi .ini file that you can copy and paste into a file (lets call it lrs_uwsgi.ini) in the /etc/uwsgi/vassals directory. You will need to use sudo for admin privileges:

[uwsgi]

# Django-related settings
# the base directory of django project
chdir = /path/to/ADL_LRS
# Django's wsgi file
module = adl_lrs.wsgi:application
env = DJANGO_SETTINGS_MODULE = adl_lrs.settings
# virtualenv path
home = /path/to/env
enable-threads = true
workers = 5
processes = 5
harakiri = 10
vacuum = true
master = true
http-socket = :8000
daemonize = /path/to/logs/uwsgi/lrs_uwsgi.log

NOTE: These are just example parameters you can set in the file. Include whatever parameters you need to for your system. A list of uWSGI config parameters can be found here here. Also, if you want to run multiple uWSGI instances, create a file for each one but make sure you change the http-socket parameter in each one and make sure they are all in the same directory. Your Nginx upstream section will have to also be adjusted to listen for each instance. For more info on uWSGI vassals, look here.

Upstart

Create an Upstart script with root permissions (/etc/init/lrs.conf). You will need to use sudo again:

description     "lrs"

start on runlevel [2345]
stop on runlevel [!2345]

respawn

exec /path/to/env/bin/uwsgi --emperor /etc/uwsgi/vassals

If your server ever goes down, uWSGI will automatically start now and can be stopped and started:

sudo start lrs
sudo stop lrs

NOTE: If you're using Ubuntu 15+ it uses systemd instead of upstart by default. Follow these instructions for a systemd script

Create a systemd script with root permissions (/lib/systemd/system/lrs.service). You will need to use sudo again:

[Unit]
Description=LRS

[Service]
ExecStart=/path/to/env/bin/uwsgi --emperor /etc/uwsgi/vassals
Restart=on-failure

[Install]
WantedBy=multi-user.target

If your server ever goes down, uWSGI will automatically start now and can be stopped and started:

sudo systemctl start lrs
sudo systemctl stop lrs

Nginx

Once uWSGI is running, you'll want to install Nginx. You'll need to use sudo for admin privileges.

sudo apt-get install nginx

Create the default nginx configuration file at /etc/nginx/conf.d/default.conf. Again, use sudo.

Copy and paste this config file for a basic LRS config (default.config):

# The default server
#
upstream lrs {
    server localhost:8000;
    ip_hash;
}
server {
    listen 80;
    server_name localhost;    
    charset utf-8;

    # full path to the project dir - the dir that contains the urls.py file
    root /path/to/ADL_LRS/adl_lrs;
    access_log /path/to/logs/nginx/nginx_access.log;
    error_log /path/to/logs/nginx/nginx_error.log;

    ## compression
    gzip on;
    gzip_buffers 16 8k;
    gzip_comp_level 4;
    gzip_http_version 1.0;
    gzip_min_length 1280;
    gzip_types text/plain text/css application/x-javascript application/json text/javascript image/jpeg image/bmp;
    gzip_vary on;

    location / {
        client_max_body_size 1M;
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://lrs;
    }

    location /static/ {
        autoindex on;
        expires 1w;
        root /path/to/ADL_LRS/adl_lrs;
    }

    location /static/endless_pagination {
        autoindex on;
        expires 1w;
        alias /path/to/env/lib/python2.7/site-packages/endless_pagination/static/endless_pagination;
    }

    # IF USING THE UPDATED LRS CODE (POST 1.0.2) YOU WILL NEED THIS LOCATION INSTEAD OF ENDLESS_PAGINATION
    # location /static/el-pagination {
        # autoindex on;
        # expires 1w;
        # alias /path/to/env/lib/python2.7/site-packages/el_pagination/static/el-pagination;
    # }


    location /static/admin {
        autoindex on;
        expires 1w;
        alias /path/to/env/lib/python2.7/site-packages/django/contrib/admin/static/admin;
    }
}

Restart Nginx admin:~$ sudo service nginx restart

Now you should be able to go to http://localhost/ and be able to view your LRS with full CSS that is being served by Nginx.

NOTE: This is a basic configuration. We highly recommend using https for security reasons. More on using SSL/https with Nginx can be found here.