How-To: Deploy Python Flask applications using NGINX and uWSGI – Configuration (Part 2 of 3)

0
182
Image credit: Vecteezy, Python, uWSGI, NGINX and Ubuntu

After the installation of the requirement software, next is the configuration part. An application should have its own NGINX and uWSGI configuration. It’s not necessary to do this for NGINX but it is recommended. The reason is simple, for easier management. When you have multiple applications being managed, updating smaller settings is easier , less disruptive and safer to do.

uWSGI Configuration

A configuration is made for each application. For example if you have an multiple API and Web (e.g. Django) projects, each should have its own configuration file.

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         = %n
chdir           = /var/www/geek_flask_app
module          = app_wsgi
virtualenv      = /home/geek/geek-venv
plugins         = http,python3

socket          = /var/run/%n.sock
chmod-socket    = 666
http-socket     = 127.0.0.1:8081
logto2          = /var/log/uwsgi/%n.log

enable-threads  = true
vacuum          = true
die-on-term     = true
  • %n – Sets the value of the project to be the same as the filename w/o the extension. In this example it will be greek_flask_app

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 restart

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. 

$ 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. It should show that there are no configuration errors. If there are, double check them and validate it using the following command.

$ sudo nginx -t

04. Reload the NGINX service to apply the changes

$ sudo service nginx reload


For enquiries, product placements, sponsorships, and collaborations, connect with us at hello@firegulaman.com. We'd love to hear from you!