uWSGI + nginx + systemdΒΆ

This chapter provides an example for configuring uWSGI, nginx, and systemd for a Pyramid application.

Below you can find an almost production ready configuration. "Almost" because some uwsgi parameters might need tweaking to fit your needs.

An example systemd configuration file is shown here:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# /etc/systemd/system/pyramid.service

[Unit]
Description=pyramid app

# Requirements
Requires=network.target

# Dependency ordering
After=network.target

[Service]
TimeoutStartSec=0
RestartSec=10
Restart=always

# path to app
WorkingDirectory=/opt/env/wiki
# the user that you want to run app by
User=app

KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

# Main process
ExecStart=/opt/env/bin/uwsgi --ini-paste-logged /opt/env/wiki/development.ini

[Install]
WantedBy=multi-user.target

Note

In order to use the --ini-paste-logged parameter (and have logs from an application), PasteScript is required. To install, run:

pip install PasteScript

uWSGI can be configured in .ini files, for example:

1
2
3
4
5
6
7
# development.ini
# ...

[uwsgi]
socket = /tmp/pyramid.sock
chmod-socket = 666
protocol = http

Save the files and run the below commands to start the process:

systemctl enable pyramid.service
systemctl start pyramid.service

Verify that the file /tmp/pyramid.sock was created.

Here are a few useful commands:

systemctl restart pyramid.service # restarts app
journalctl -fu pyramid.service # tail logs

Next we need to configure a virtual host in nginx. Below is an example configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# myapp.conf

upstream pyramid {
    server unix:///tmp/pyramid.sock;
}

server {
    listen 80;

    # optional ssl configuration

    listen 443 ssl;
    ssl_certificate /path/to/ssl/pem_file;
    ssl_certificate_key /path/to/ssl/certificate_key;

    # end of optional ssl configuration

    server_name  example.com;

    access_log  /opt/env/access.log;

    location / {
        proxy_set_header        Host $http_host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;

        client_max_body_size    10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout   60s;
        proxy_send_timeout      90s;
        proxy_read_timeout      90s;
        proxy_buffering         off;
        proxy_temp_file_write_size 64k;
        proxy_pass http://pyramid;
        proxy_redirect          off;
    }
}

A better explanation for some of the above nginx directives can be found in the cookbook recipe nginx + pserve + supervisord.