Deploying a Flask App With NGINX Unit

Srujan Deshpande
The Startup
Published in
4 min readSep 19, 2020

Flask is a popular python web framework that is most commonly known for creating REST APIs very easily. They are usually deployed using WSGI servers such as Gunicorn or uWSGI.

Another server solution that is much easier to work with and more versatile is NGINX Unit and that’s what we will be looking at here.

NGINX Unit is a dynamic application server. It is not to be confused with the popular NGINX web server, although they work great together.

The great thing about Unit is that it supports a RESTful JSON API. That way you can deploy configuration changes without service disruptions and Unit even runs apps built with multiple languages and frameworks!

We will be deploying a very basic flask app and will use the basic hello world template. Create a file named app.py in your project directory. Here all we’re doing is importing the flask module and creating a simple message for the root route.

Basic flask application

Next, create a file named requirements.txt. This will contain details of the dependencies we have and since flask is the only one, that’s all we have to add! You can add futher dependencies here.

Requirements file

Make sure to create a virtual environment and then install flask with the command pip3 install -r requirements.txt. Then, test out the application using the command flask run.

Running the server using flask

If you see something similar to the above image, the server runs! Head to http://localhost:5000 to test out the server.

As the warning in the image mentions, using the command flask run starts up the flask server which is for development purposes only and shouldn’t be used for production. Lets use NGINX Unit instead for production.

There are many different ways of installing Unit and they can be seen here. The docker method is one of the most convienient as it only installs what is needed for the application and is easily portable. Unit only supports Unix based operating systems so using Docker ensures your server works everywhere.

Make sure you’ve got docker installed. You can install the docker engine here.

Create a new file named config.json. This will contain the configuration we provide to Unit.

Unit configuration file

Here, we specify the port as 8080, the type of application as python_app, the location of the module in the www folder and the module name as app.

Create a new file named Dockerfile. We place the commands used to interact with the NGINX Unit image in this.

To begin with, we start with the Unit image for Python 3.7. By pulling only this image, the overall container size stays low.

Then we expose the port 8080. This is the port we declared earlier on which the server will run. After that we copy over the requirements.txt file, and then install pip and the requirements.

Next, the configuration file is copied into the docker-entrypoint.ddirectory. The files in this directory are used when starting up the application server.

Finally the flask server code itself is copied over into the www folder.

And that’s all the code is! Its pretty simple and very versatile.

In order to run the docker container, first build it with the command.
sudo docker build -t build — tag=unit-flask .
We give it the tag name “unit-flask”.

What the docker build process should look like

If running on Windows, omit the sudo keyword in the docker commands.

Now run the built image using sudo docker run -p 8080:8080 unit-flask
Here, we are specifiying that port 8080 from inside the container has to be mapped to port 8080 on our local machine.

Now head to http://localhost:8080. You should see the hello world message and your server is sucessfully running!

NGINX Unit supports multiple languages and frameworks and can run multiple applications at the same time. The configuration file has to be modified accordingly and more info can be gained from the documentation. Unit can also be intergrated with the Open Source Nginx server to use as a reverse proxy and load balancer.

The complete code used here can be found at https://github.com/srujandeshpande/flask-unit-deploy

Drop a comment for any queries or suggestions!

--

--