Overview

This is the 2nd part of the 3-part guide on installing, configuring and deploying a Python Web/Flask application on an Ubuntu system that uses NGINX and uWSGI. For the installation part go here. If you’ve finished Installation and Configuration you can go here to deploy the app.

Prerequisites

  • Ubuntu 18.04
  • Python 3.x has been installed. For instructions see here.
  • Pip and Virtual Environment has been installed. For instructions see here.
  • Installed NGINX, uWSGI, and uWSGI-Emperor

 

uWSGI Configuration

01. Create the uWSGI configuration in /etc/uwsgi/apps-available/.

$ sudo nano /etc/uwsgi/apps-available/geek_flask_app.ini

 

Set the content as follows

[uwsgi]

project         = geek_flask_app

chdir           = /var/www/geek_flask_app

module          = app_wsgi

virtualenv      = /home/ubuntu/geek-venv

plugins         = http,python3

socket          = /var/run/%n.sock

chmod-socket    = 666

http-socket     = 127.0.0.1:8081

enable-threads  = true

vacuum          = true

die-on-term     = true

 

02. Create a symlink in /var/uwsgi-emperor/vassals/.

$ sudo ln -s /etc/uwsgi/apps-available/geek_flask_app.ini /etc/uwsgi-emperor/vassals/geek_flask_app.ini

 

03. Reload the uwsgi-emperor service.

$ sudo service uwsgi-emperor reload

 

04. To check if app is being deployed, see the uwsgi emperor log. Note that this will contain error as the application is not yet deployed.

</pre>

$ sudo tail -f /var/log/uwsgi/emperor.log

Press Ctrl+C to exit

 

uWSGI Configuration

Run uWSGI Emperor service as root user

01. Edit the emperor configuration.

Note: To avoid file permission issues, run the service as root user. 

$ sudo nano /etc/uwsgi-emperor/emperor.ini

 

02. Comment the uid and gid. This will set the process owner for uWSGI emperor to be the root user. If you plan to use a specific user, change the values here. For this example we are commenting these lines of configs.

# Comment the following

# user identifier of uWSGI processes

#uid = www-data

# group identifier of uWSGI processes

#gid = www-data

 

03. Restart the uwsgi emperor process.

$ sudo service uwsgi-emperor restart

 

NGINX Configuration

01. Create the nginx configuration for the flask app.

Note: This nginx configuration assumes that only one site is enabled. For multiple enabled sites, define the server_name for each site.

$ sudo nano /etc/nginx/sites-available/geek_flask_app.conf

 

Set the content as follows.

server
{
  listen        80;
  server_name   _;

  location /
  {
    include     uwsgi_params;
    uwsgi_pass  unix:/run/geek_flask_app.sock;
  }
}

 

02. Create a symlink in /etc/nginx/sites-enabled/.

$ sudo ln -s /etc/nginx/sites-available/geek_flask_app.conf /etc/nginx/sites-enabled/geek_flask_app.conf

 

03. Test nginx configuration.

$ sudo nginx -t

 

Click here for Part 01 and here for Part 03.

Previous Using Wall Street Secrets To Reduce The Cost Of Cloud Infrastructure
Next Microsoft Build 2019 | End to End Azure DevOps