How to deploy a Flask ML model on Ngnix using wfastcgi & gunicorn gateway (Ubuntu 18.04) — Part II

Prachi More
FAUN — Developer Community 🐾
3 min readSep 12, 2019

--

This guide is a continuation of the earlier article.

We saw the setup of a sample python application in a custom conda environment anc configured the gunicorn application gateway to serve the application. In this post we’ll configure nginx to serve the application as a web API.

  1. Set up the application (gunicorn) to start via systemd and serve it on unix socket

Create a new systemd file.

$ sudo vi /etc/systemd/system/myflaskapp.service

Add below snippet. Notice the line — ExecStart=/home/ubuntu/miniconda3/envs/TestEnv/bin/gunicorn — workers 3 — bind unix:myflaskapp.sock -m 007 wsgi:app

wsgi:app stands for the wsgi connector file name : application callable within the file (refer the previous article).

[Unit]
Description=Gunicorn instance to serve my sample flask app
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu
Environment="PATH=/home/ubuntu/miniconda3/envs/TestEnv/bin"
ExecStart=/home/ubuntu/miniconda3/envs/TestEnv/bin/gunicorn --workers 3 --bind unix:myflaskapp.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target

Enable the service.

$ sudo systemctl start myflaskapp
$ sudo systemctl enable myflaskapp
Created symlink /etc/systemd/system/multi- user.target.wants/myflaskapp.service → /etc/systemd/system/myflaskapp.service.

Check the service status.

$ sudo systemctl status myflaskapp
● myflaskapp.service - Gunicorn instance to serve my sample flask app
Loaded: loaded (/etc/systemd/system/myflaskapp.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-09-12 07:39:29 UTC; 23s ago
Main PID: 18994 (gunicorn)
Tasks: 4 (limit: 4689)
CGroup: /system.slice/myflaskapp.service
├─18994 /home/ubuntu/miniconda3/envs/TestEnv/bin/python /home/ubuntu/miniconda3/envs/TestEnv/bin/gunicorn --workers 3 --bind unix:myflaskapp.sock -m 007 w
├─19012 /home/ubuntu/miniconda3/envs/TestEnv/bin/python /home/ubuntu/miniconda3/envs/TestEnv/bin/gunicorn --workers 3 --bind unix:myflaskapp.sock -m 007 w
├─19013 /home/ubuntu/miniconda3/envs/TestEnv/bin/python /home/ubuntu/miniconda3/envs/TestEnv/bin/gunicorn --workers 3 --bind unix:myflaskapp.sock -m 007 w
└─19014 /home/ubuntu/miniconda3/envs/TestEnv/bin/python /home/ubuntu/miniconda3/envs/TestEnv/bin/gunicorn --workers 3 --bind unix:myflaskapp.sock -m 007 w

A socket file ‘myflaskapp.sock’ will be created in the ‘WorkingDirectory=/home/ubuntu’ we mentioned in the systemd file.

Launch and test the application once again in the browser.

http://your_server_ip:5000

2. Nginx setup

It’s about time the application is ready to be served on the reverse proxy so that it can be served as a web API. Nginx will pass the web requests to the socket we created earlier.

Install nginx.

$ sudo apt-get install nginx

Add user ‘ubuntu’ in the group ‘www-data’.

$ sudo usermod ubuntu -g www-data

Create a conf file specific to our application.

$ sudo vi /etc/nginx/sites-available/myflaskapp

We’ll set the reverse proxy to pass requests to the socket file we created earlier.

And also bind the API to port 80.

server {
listen 80;
server_name <your_server_ip>;
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/myflaskapp.sock;
}
}

Create a symlink.

$ sudo ln -s /etc/nginx/sites-available/myflaskapp /etc/nginx/sites-enabled

Test the nginx configuration.

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Start the web server.

$ sudo systemctl restart nginx
$ ps -ef | grep nginx
root 19114 1 0 07:48 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 19115 19114 0 07:48 ? 00:00:00 nginx: worker process
www-data 19116 19114 0 07:48 ? 00:00:00 nginx: worker process

The application will be served on port 80.

http://your_server_ip:80

Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.

To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--