How to serve the Flask app with nginx as the reverse proxy


We are going to build a simple and smooth process using the following steps:

  1. if you don’t have uwsgi installed, try this on ubuntu:

    sudo pip3 install uwsgi
  2. locate your flask project folder, if you don’t know how to create a a flask app, check this link:
    flask app creation
    https://datasciencebyexample.com/2021/09/29/2021-09-29-1/

  3. create a uswgi init file inside your flask project, for example, you can call it myproject.ini

    [uwsgi]
    module = main:app

    master = true
    processes = 2

    socket = myproject.sock
    chmod-socket = 660
    vacuum = true

    die-on-term = true
  4. Let’s then create the systemd service unit file. Creating a systemd unit file will allow Ubuntu’s init system to automatically start uWSGI and serve the Flask application whenever the server boots.

Create a unit file ending in .service within the /etc/systemd/system directory to begin, for example

sudo emacs /etc/systemd/system/myproject.service

inside the file:

[Unit]
Description=uWSGI instance to serve myproject
After=network.target

[Service]
User=<your user name>
Group=www-data


WorkingDirectory=<your flask app directory>

ExecStart=/usr/local/bin/uwsgi --ini myproject.ini

[Install]
WantedBy=multi-user.target

Then type these in the command line:

sudo systemctl start myproject
sudo systemctl enable myproject

you can check the service status

sudo systemctl status myproject

if any changes to the project, just do this to reflect new web app:

sudo service myproject restart
  1. install the nginx if you don’t have one
    sudo apt update
    sudo apt install nginx
    At the end of the installation process, Ubuntu (18.04) will start Nginx. The web server should already be up and running.

We can check with the systemd init system to make sure the service is running by typing:

systemctl status nginx
  1. Begin by creating a new server block configuration file in Nginx’s sites-available directory. Let’s call this myproject to keep in line with the rest of the guide:

    sudo emacs /etc/nginx/sites-available/myproject

    inside the file:

    server {
    listen 80;
    server_name englishlearningbyexample.com www.englishlearningbyexample.com;

    location / {
    include uwsgi_params;
    uwsgi_pass unix:<your flask app dir>/myproject.sock;
    }
    }

    above I was using
    www.englishlearningbyexample.com
    as the example for server_name, but you should replace with yours.

  2. final steps

To enable the Nginx server block configuration you’ve just created, link the file to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

then do

sudo systemctl restart nginx

now you should see the website running

If you encounter any errors, trying checking the following:

sudo less /var/log/nginx/error.log: checks the Nginx error logs.
sudo less /var/log/nginx/access.log: checks the Nginx access logs.
sudo journalctl -u nginx: checks the Nginx process logs.
sudo journalctl -u myproject: checks your Flask app’s uWSGI logs.

Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC