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 24.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, verify the Python version using the following command.

python --version

Output.

Python 3.12.3

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 (/usr/lib/systemd/system/supervisor.service; enabled; preset: enabled)
Active: active (running) since Thu 2025-05-15 12:17:28 UTC; 3s ago
Docs: http://supervisord.org
Main PID: 41835 (supervisord)
Tasks: 5 (limit: 629145)
Memory: 71.9M (peak: 72.1M)
CPU: 631ms
CGroup: /system.slice/supervisor.service
├─41835 /usr/bin/python3 /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
├─41837 /var/www/myapp/myenv/bin/python3.12 /var/www/myapp/myenv/bin/gunicorn -w 3 --bind unix:/var/www/myapp/ipc.sock wsgi:app
├─41838 /var/www/myapp/myenv/bin/python3.12 /var/www/myapp/myenv/bin/gunicorn -w 3 --bind unix:/var/www/myapp/ipc.sock wsgi:app
├─41839 /var/www/myapp/myenv/bin/python3.12 /var/www/myapp/myenv/bin/gunicorn -w 3 --bind unix:/var/www/myapp/ipc.sock wsgi:app
└─41840 /var/www/myapp/myenv/bin/python3.12 /var/www/myapp/myenv/bin/gunicorn -w 3 --bind unix:/var/www/myapp/ipc.sock wsgi:appp

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 24.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!