You are currently viewing How to deploy flask app on Ubuntu Server using Nginx and Gunicorn

How to deploy flask app on Ubuntu Server using Nginx and Gunicorn

Lets go through step by step tutorial on how to deploy flask app on Ubuntu Server or Google Cloud Platform’s Virtual Machine using Nginx and Gunicorn.

Step 1. Install the required packages

 
sudo apt update

Now let’s install python3, python3-pip, and Nginx using the commands below:

 
sudo apt install python3-pip python3-dev nginx

Step 2. After installing python, pip and nginx, Create a directory (for our flask app) and virtual environment.

First install virtual environment.

 
sudo pip3 install virtualenv

 

Now lets create a directory to host our flask application.
 
mkdir myFlaskApp && cd myFlaskApp

Run the following command to create a virtual environment named env.

 
virtualenv env

And then activate the virtual environment.

 
source env/bin/activate

Now lets install flask and gunicorn using pip.

 
pip3 install flask gunicorn

Step 3. Create a demo project and wsgi entry point

 
sudo nano app.py

Paste the contents below to app.py file

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
      return "Hello World!"
if __name__ == '__main__':
      app.run(debug=True,host='0.0.0.0')

Now, we’ll create a file that will serve as the entry point for our application. This will tell our Gunicorn server how to interact with the application.

sudo nano wsgi.py

copy the contents below to wsgi.py

 
from app import app
if __name__ == "__main__":
     app.run()

Install the app requirement

 
pip install -r requirement.txt

now run the command below:

 
gunicorn --bind 0.0.0.0:5000 wsgi:app

This was just to test the app is running or not. Now lets deactivate our virtual environment now:

deactivate

Step 4. Creating a systemd service

 
sudo nano /etc/systemd/system/app.service

Now paste the contents below to this file:

 
[Unit]
#  specifies metadata and dependencies
Description=Gunicorn instance to serve myproject
After=network.target
# tells the init system to only start this after the networking target has been reached
# We will give our regular user account ownership of the process since it owns all of the relevant files
[Service]
# Service specify the user and group under which our process will run.
User=osmlist
# give group ownership to the www-data group so that Nginx can communicate easily with the Gunicorn processes.
Group=www-data
# We'll then map out the working directory and set the PATH environmental variable so that the init system knows where our the executables for the process are located (within our virtual environment).
WorkingDirectory=/home/osmlist/myFlaskApp/
Environment="PATH=/home/osmlist/myFlaskApp/env/bin"
# We'll then specify the commanded to start the service
ExecStart=/home/osmlist/myFlaskApp/env/bin/gunicorn --workers 3 --bind unix:app.sock -m 007 wsgi:app
# This will tell systemd what to link this service to if we enable it to start at boot. We want this service to start when the regular multi-user system is up and running:
[Install]
WantedBy=multi-user.target

Activate this service with the command below:

sudo systemctl start app
sudo systemctl enable app

A file named app.sock will be automatically created.

Step 5. Configuring Nginx

Create a file named app inside /etc/nginx/sites-available

sudo nano /etc/nginx/sites-available/app
Now copy the below contents to this file:
server {
listen 80;
server_name 172.158.155.154;
location / {
    include proxy_params;
    proxy_pass http://unix:/home/osmlist/myFlaskApp/app.sock;
           }
  }    
Change the server_name IP address with your own IP address.

Activate this configuration by executing the command below:

sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled
Restart nginx with command below and your web app should work.
sudo systemctl restart nginx
Visit http://172.158.155.154/ on your we browser you should see your app running fine.

If it is not running you may need to go to your firewall settings and allow the port 80 and  you should get it work just fine, or just run sudo ufw allow ‘Nginx Full’ command in the server.

You may need to install ufw in some server with sudo apt install ufw and you are good to go.

Leave a Reply