Configuring UWSGI with Python, Django, and NGINX: Difference between revisions

No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:


Install uwsgi:
=== Setup WSGI ===
<pre>
<pre>
apt-get install build-essential python
apt-get install build-essential python
Line 14: Line 14:
[Service]
[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown www-data:www-data /run/uwsgi'
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown www-data:www-data /run/uwsgi'
#ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
#ExecStart=/usr/local/bin/uwsgi --http 0.0.0.0:8001 --wsgi-file stocks/wsgi.py --master -b 30000 --enable-threads --processes 4 --threads 2
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
Restart=always
Line 32: Line 30:
sudo vi /etc/uswsgi/sites/stocks.ini</pre>
sudo vi /etc/uswsgi/sites/stocks.ini</pre>


And then add:
And then add (there will be 1 per application):


<pre>
<pre>
Line 41: Line 39:


chdir = %(base)/%(project)
chdir = %(base)/%(project)
#wsgi-file = %(project)/wsgi.py
module=%(project).wsgi:application
module=%(project).wsgi:application
#module=django.core.handlers.wsgi:WSGIHandler()


master = true
master = true
Line 49: Line 45:
threads=2
threads=2


http = 0.0.0.0:8001
http = 0.0.0.0:8001</pre>
#chown-socket = %(uid):www-data
 
#chmod-socket = 664
Now start uwsgi
#vacuum = true
 
#plugins=python</pre>
<pre>sudo service uwsgi start</pre>
 
Check that the service and your application are running at:
 
<pre>http://www.lab.bpopp.net:8001</pre>
 
Set service to start automatically
 
<pre>sudo systemctl enable uwsgi.service</pre>
 
=== Configure NGINX (optional) ===
 
At the top (outside the server declaration) of /etc/nginx/conf.d/default.conf:
<pre>
 
upstream uwsgicluster {
        server 127.0.0.1:8001;
    }
</pre>
 
At the bottom (inside the server declaration) add the proxy:
 
<pre> location /django/stocks {
        include uwsgi_params;
        uwsgi_pass  uwsgicluster;
 
        proxy_redirect    off;
        proxy_set_header  Host $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-Host $server_name;
    }
</pre>

Latest revision as of 13:04, 1 November 2020

Setup WSGI

apt-get install build-essential python
sudo pip3 install uwsgi

Create a service in /etc/systemd/system/uwsgi.service

[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown www-data:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

Create an emperor file at:

sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/sites
sudo vi /etc/uswsgi/sites/stocks.ini

And then add (there will be 1 per application):

[uwsgi]
project = stocks
uid = bpopp
base = /usr/share/nginx/html/django

chdir = %(base)/%(project)
module=%(project).wsgi:application

master = true
processes = 5
threads=2

http = 0.0.0.0:8001

Now start uwsgi

sudo service uwsgi start

Check that the service and your application are running at:

http://www.lab.bpopp.net:8001

Set service to start automatically

sudo systemctl enable uwsgi.service

Configure NGINX (optional)

At the top (outside the server declaration) of /etc/nginx/conf.d/default.conf:


upstream uwsgicluster {
        server 127.0.0.1:8001;
    }

At the bottom (inside the server declaration) add the proxy:

 location /django/stocks {
        include uwsgi_params;
        uwsgi_pass  uwsgicluster;

        proxy_redirect     off;
        proxy_set_header   Host $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-Host $server_name;
    }