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. For the configuration part go here.
From our partners:
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 from previous guides.
- Configured NGINX, uWSGI and uWSGI-Emperor from previous guides.
Deploy a Flask App/API using uWSGI and NGINX
Note: For this demo, we used the sample flask app and a python virtual environment. And it will be located under /var/www
01. Create the following directories and files
Directories and file structure
|__ /var/www |__ geek_flask_app |__ api |__ __init__.py |__ res |__ __init__.py |__ endpoints.py |__ app_wsgi.py |__ requirements.txt
Or create the files and directories using the following commands
$ cd /var/www $ sudo mkdir -p geek_flask_app/api/res
/var/www/geek_flask_app/api/__init__.py content
from flask import Flask application = Flask(__name__) import api.res.endpoints
/var/www/geek_flask_app/app_wsgi.py
from api import application
/var/www/geek_flask_app/api/res/endpoints.py
# -*- coding: utf-8 -*- from flask import jsonify from api import application @application.route('/', methods=['GET']) def get_app_properties(): name = 'geek-flask-api' version = '0.0.1' app_properties = \ { 'name': name , 'version' : version } return jsonify(app_properties)
/var/www/geek_flask_app/requirements.txt
flask
02. Install python libraries that is required by your application.
Note: Since we are using a virtual environment, activate the virtual environment and install the python libraries. See the Setup the Python Virtual Environment section.
$ source ~/geek-venv/bin/activate (geek-venv) ubuntu@{host}:~$ cd /var/www/flask_app (geek-venv) ubuntu@{host}:~$ pip install -r requirements.txt (geek-venv) ubuntu@{host}:~$ deactivate
03. Reload the uwsgi-emperor service.
$ sudo service uwsgi-emperor reload
04. To verify if the flask app is deployed, test using the http-socket defined in uwsgi configuration.
$ curl http://127.0.0.1:8081
It should return a JSON structured response
{"name":"geek-flask-api","version":"0.0.1"}
05. Next is to verify if NGINX can communicate with our Python Flask app on uWSGI. Restart the NGINX server
$ sudo service nginx restart
06. Verify using cURL on the NGINX.
$ curl http://localhost
It should also show the same response
{"name":"geek-flask-api","version":"0.0.1"}
Next steps is to configure your DNS and map it to your NGINX configuration. And you should next configure the previously commented gid and uid to use the proper user, an example is “www-data”.
Click here for Part 01 and here for Part 02.
For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!
Our humans need coffee too! Your support is highly appreciated, thank you!