Running a Flask application on a Linux server

Running a Flask application on a Linux server

Who this article is for

This article is for those who want to run a website or API on Flask on their own server and make it available from the internet. If you are just making a stub for a test, or a local website, these steps may be redundant.

Related Articles:

Introduction

The first deployment of a project on a real server is always a challenge.

A huge number of difficulties may appear that a person learning programming did not even suspect.

Often you need skills that are very far from what you could have learned in lessons on language syntax or some framework.

You need to stock up on patience and be ready to read, search, ask and check a lot.

We will run the project in the following environment:

Debian

Nginx

Gunicorn

Preparation

To read about installing and configuring Debian Linux see the article Debian tutorial

To read about installing Nginx on Debian and Ubuntu, see the articles Nginx on Debian and Nginx on Ubuntu

Log in to the server and run

sudo apt update

Pip

Check whether pip3 is installed with the command

pip3 --version

If you got something like

pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

Then Pip is already installed; if not, you need to run

sudo apt install python3-pip

You can read the details about pip here

Installing packages

Copy the folder with our project to the server, go to the directory that contains the file requirements.txt and run

pip3 install -r requirements.txt

If you got a warning

The script flask is installed in '/home/andrei/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Then you need to add '/home/andrei/.local/bin' to PATH

You can read how to do this in the article Linux PATH

Gunicorn

After successfully installing pip3 and the dependency packages, you need to install Gunicorn.

There are other means of deploying Flask; you can study them here

sudo apt install gunicorn3

Gunicorn can be installed via pip; installing gunicorn in a virtual environment will be especially useful.

In this example, for simplicity, the installation was done via apt

Suppose the main file of your application is called aredel_com.py and you are in the same directory as it.

The command to run the application

gunicorn3 aredel_com:app

[2020-06-19 12:49:13 +0300] [10692] [INFO] Starting gunicorn 19.9.0 [2020-06-19 12:49:13 +0300] [10692] [INFO] Listening at: http://127.0.0.1:8000 (10692) [2020-06-19 12:49:13 +0300] [10692] [INFO] Using worker: sync [2020-06-19 12:49:13 +0300] [10695] [INFO] Booting worker with pid: 10695

As you may have noticed, Gunicorn listens on port 8000.

The HTTP request must be listened for on port 80, but Nginx will do that and then redirect to 8000.

We hide Gunicorn behind Nginx, because Nginx has many capabilities for working with load and security that Gunicorn does not have.

Let us stop Gunicorn by pressing CTRL + C and start it as a daemon

gunicorn3 -D aredel_com:app

 

Nothing should appear on the command line, unlike a normal launch.

To make sure that gunicorn3 is running, execute

ps -A | grep gunicorn

10704 ? 00:00:00 gunicorn3 10707 ? 00:00:00 gunicorn3

To check whether Nginx is running, you can open the server in a browser.

The IP of my server is 192.0.2.131

To read about how to configure network connections in Linux see the articles Debian Network and Ubuntu Network

Nginx keeps the available sites in /etc/nginx/sites-available/

cd /etc/nginx/sites-available/
ls

drwxr-xr-x 2 root root 4096 syys 17 16:38 . drwxr-xr-x 8 root root 4096 syys 17 16:38 .. -rw-r--r-- 1 root root 2416 maalis 26 2020 default

The default site, which you can see by entering the IP of your server in the browser address bar (mine is 192.0.2.131), looks approximately like this:

Nginx default www.aredel.com
Nginx default

We do not need the default site, so we can simply delete it

sudo rm default

Configuring Nginx

To run your own site on Nginx, we will follow the instructions from gunicorn.org

We copy the configuration for nginx. I advise doing this not from here but from the gunicorn site

server { listen 80; server_name example.org; access_log /var/log/nginx/example.log; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

We run

sudo vi default

And paste the settings.

If you have difficulties using vi, read the article «The vi text editor»

We edit the settings so that they match the project name better

server { listen 80; server_name aredel.com; access_log /var/log/nginx/aredel_com.log; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

Let us restart nginx

sudo service nginx restart

You can find more detailed information about configuring the Nginx - Gunicorn bundle here

To read about how to configure Gunicorn to start when the system reboots, see here

Summary

Now, if you open your server in a browser, your application will appear there.

How it happened: gunicorn3 started the Flask application and listened on port 8000

We started Nginx, which listens on port 80 and forwards the connection to port 8000 - pay attention to the line

proxy_pass http://127.0.0.1:8000;

↓ Articles about Flask ↓
Python
Flask
Running Flask on hosting
Running Flask on a Linux server
The first Flask project
Jinja templates
Flask FAQ
Web Forms
Errors

Article author: Andrei Olegovich