Atlantic.Net Blog

How to Install Flask with Nginx and Gunicorn on Ubuntu 22.04

Flask, a lightweight and versatile web framework for Python, empowers developers to build web applications with ease and flexibility. It allows developers to create web applications quickly and efficiently by providing the necessary tools and features without imposing too many constraints. When combined with Nginx, a high-performance web server renowned for its speed and efficiency, Flask-based applications can achieve optimal performance and scalability.

This guide aims to walk you through the process of installing Flask with Nginx on Ubuntu 22.04.

Step 1 – Install Required Dependencies

Before starting, you will need to install Python and other required dependencies to your server.

First, install the Python and Python virtual environment using the following command.

apt update -y
apt install python3-pip python3-venv -y

Next, change the default Python version with the following command.

update-alternatives --install /usr/bin/python python /usr/bin/python3 10

Next, verify the Python version using the following command.

python --version

Output.

Python 3.10.12

Next, install Nginx and Supervisor with the following command.

apt install nginx supervisor -y

Step 2 – Create a Flask Application

First, create your application directory.

mkdir -p /var/www/myapp

Next, change the ownership of the application directory.

chown -R www-data:www-data /var/www/myapp

Then, navigate to your application directory and create a Python virtual environment.

cd /var/www/myapp
python -m venv myenv

Next, activate the Python virtual environment.

source myenv/bin/activate

Next, install the Flask and Gunicorn.

pip install flask gunicorn

Next, create a application file.

nano app.py

Add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, World!"

if __name__ == '__main__':
    app.run()

Save and close the file, then run the application with the following command.

gunicorn -w 4 -b 0.0.0.0:8000 app:app

You will see the following output.

[2024-02-15 03:29:22 +0000] [114941] [INFO] Starting gunicorn 21.2.0
[2024-02-15 03:29:22 +0000] [114941] [INFO] Listening at: http://0.0.0.0:8000 (114941)
[2024-02-15 03:29:22 +0000] [114941] [INFO] Using worker: sync
[2024-02-15 03:29:22 +0000] [114942] [INFO] Booting worker with pid: 114942
[2024-02-15 03:29:22 +0000] [114943] [INFO] Booting worker with pid: 114943
[2024-02-15 03:29:22 +0000] [114944] [INFO] Booting worker with pid: 114944
[2024-02-15 03:29:22 +0000] [114945] [INFO] Booting worker with pid: 114945

Press CTRL+C to stop the application. Then, create a WSGI for your application.

nano wsgi.py

Add the following code:

from app import app

if __name__ == "__main__":
    app.run(debug=True)

Now, verify your application using WSGI.

gunicorn -w 4 --bind 0.0.0.0:8000 wsgi:app

Now, open your web browser and access your Flask app using the URL http://your-server-ip:8000. You will see your Flask application on the following screen.

Press CTRL+C to stop the application.

Step 3 – Create a Service File for the Flask App

Next, create a supervisor configuration file to manage your Flask app via systemd.

nano /etc/supervisor/conf.d/myapp.conf

Add the following configurations.

[program:myapp] 
command=/bin/bash -c 'source /var/www/myapp/myenv/bin/activate; gunicorn -w 3 --bind unix:/var/www/myapp/ipc.sock wsgi:app'
directory=/var/www/myapp
user=www-data
group=www-data
autostart=true 
autorestart=true 
stdout_logfile=/var/www/myapp/myapp.log 
stderr_logfile=/var/www/myapp/error.log

Save and close the file, then restart the Supervisor to apply the changes.

systemctl restart supervisor

You can check the status of the Supervisor with the following command.

systemctl status supervisor

Output.

● supervisor.service - Supervisor process control system for UNIX
     Loaded: loaded (/lib/systemd/system/supervisor.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2024-02-15 03:32:31 UTC; 5s ago
       Docs: http://supervisord.org
   Main PID: 115115 (supervisord)
      Tasks: 5 (limit: 4579)
     Memory: 67.8M
        CPU: 711ms
     CGroup: /system.slice/supervisor.service
             ├─115115 /usr/bin/python3 /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
             ├─115118 /var/www/myapp/myenv/bin/python /var/www/myapp/myenv/bin/gunicorn -w 3 --bind unix:/var/www/myapp/ipc.sock wsgi:app
             ├─115119 /var/www/myapp/myenv/bin/python /var/www/myapp/myenv/bin/gunicorn -w 3 --bind unix:/var/www/myapp/ipc.sock wsgi:app
             ├─115120 /var/www/myapp/myenv/bin/python /var/www/myapp/myenv/bin/gunicorn -w 3 --bind unix:/var/www/myapp/ipc.sock wsgi:app
             └─115121 /var/www/myapp/myenv/bin/python /var/www/myapp/myenv/bin/gunicorn -w 3 --bind unix:/var/www/myapp/ipc.sock wsgi:app

Step 4 – Configure Nginx for Flask App

First, create an Nginx virtual server block for the Flask app.

nano /etc/nginx/conf.d/myapp.conf

Add the following configurations.

server {
    listen 80;
    server_name app.example.com;

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/myapp/ipc.sock;
    }
}

Save and close the file, then restart the Nginx service to implement the changes.

systemctl restart nginx

Now, open your web browser and access your Flask App using the URL http://app.example.com.

Conclusion

Congratulations! You have successfully installed Flask with Nginx on Ubuntu 22.04. By following these steps, you can develop and deploy Flask-based web applications efficiently and securely. With Flask’s simplicity and flexibility combined with Nginx’s performance and reliability, you have a powerful platform for building robust web applications that can scale to meet the demands of your users. You can now test the Flask application on dedicated server hosting from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year