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

No edit summary
 
(2 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 30: 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 58: Line 58:


<pre>sudo systemctl enable uwsgi.service</pre>
<pre>sudo systemctl enable uwsgi.service</pre>


=== Configure NGINX (optional) ===
=== Configure NGINX (optional) ===


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


Line 70: Line 69:
</pre>
</pre>


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


<pre> location /django/stocks {
<pre> location /django/stocks {

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;
    }