User Tools

Site Tools


adm:setting_up_uwsgi

Setting Up uWSGI for Zibawa

Configure NGINX for uWSGI

Uwsgi Params file

Check that you have the following file at:

sudo nano /etc/nginx/uwsgi_params

The file should look like this. https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

NGINX configuration file

sudo nano /etc/nginx/conf.d/zibawa.conf
#modify the server name to your IP or FQDN

upstream django {
    server unix:///tmp/zibawa.sock; # for a file socket
    #server 127.0.0.1:8001; # alternative for a web port socket(we are not using this..)
}

# configuration of the server
server {


    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name app.zibawa.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /var/www/html/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /var/www/html/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file usually installed with nginx
    }
}

Restart Nginx to apply changes

sudo systemctl restart nginx

Test uwsgi with test.py Included in the zibawa distribution there is a file to test uwsgi. We will test with permissions 777 to avoid problems with permissions and to ensure our NGINX config is correct.

uwsgi --socket /tmp/zibawa.sock --wsgi-file /home/zibawa/zibawa/test.py --chmod-socket=777

If you navigate to <your domain name or IP> you should see “hello world”.

Run Zibawa using uWSGI

To run this command you must be in the Zibawa main directory (where manage.py is)

cd /home/zibawa/zibawa
uwsgi --socket /tmp/zibawa.sock --module zibawa.wsgi --chmod-socket=777

uwsgi ini file

There is a file called zibawa_uwsgi.ini inside the zibawa distribution. This file defines how the uwsgi should be run automatically, with secure permissions by the zibawa user and www-data group.

Before testing, you will need to remove the socket we created in the previous test (otherwize you will get error : bind(): Address already in use [core/socket.c line 230]

sudo rm /tmp/zibawa.sock

Test the uwsgi ini file using the following

sudo uwsgi --ini /home/zibawa/zibawa/zibawa_uwsgi.ini --uid zibawa --gid www-data

Use ctrl C to escape.

You must use sudo, or the process will run under your own user and cause errors because it is unable to write to the logs.

Create /etc/uwsgi/vassals directory and make sim link to above file

sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
sudo ln -s /home/zibawa/zibawa/zibawa_uwsgi.ini /etc/uwsgi/vassals/

Now run uwsgi in emperor mode

sudo uwsgi --emperor /etc/uwsgi/vassals --uid zibawa --gid www-data

Setup uWSGI with systemd

Find out where uWSGI executable is

whereis uwsgi

Create emperor.uwsgi.service file as follows

sudo nano /etc/systemd/system/emperor.uwsgi.service
#in this file it may be necessary to change ExecStart according to where executable is

[Unit]
Description=uWSGI Emperor
After=syslog.target

[Service]

ExecStart=/usr/local/bin/uwsgi --ini /etc/uwsgi/emperor.ini

# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

Also create emperor.ini file as specified in above file

sudo nano /etc/uwsgi/emperor.ini
[uwsgi]
emperor = /etc/uwsgi/vassals
uid = zibawa
gid = www-data

Now start and stop emperor service like any other systemd service

sudo systemctl restart emperor.uwsgi.service

To make reload on reboot

sudo systemctl daemon-reload
sudo systemctl enable emperor.uwsgi

For full information: https://uwsgi-docs.readthedocs.io/en/latest/Systemd.html

adm/setting_up_uwsgi.txt · Last modified: 2017/03/15 09:22 by matt