How to Set Up Flask Python Application with Gunicorn and NGINX on Ubuntu 26.04

How to Set Up Flask Python Application with Gunicorn and NGINX on Ubuntu 26.04

Flask is one of the most popular microframeworks for creating websites in Python. Gunicorn (Green Unicorn) is a WSGI (Web Server Gateway Interface) HTTP server for the Python programming language. Gunicorn implements a standard WSGI server specification, allowing it to run Python web applications that also implement it, such as Django, Flask, or other frameworks. In this article, we will show you how to set up a Flask Python application with Gunicorn and Nginx on Ubuntu 26.04.

Prerequisites

Conventions

# – given commands should be executed with root privileges either directly as a root user or by use of sudo command
$ – given commands should be executed as a regular user

Step 1. Update the System

First of all, we need to log in to our Ubuntu 26.04 server through SSH:

ssh root@IP_Address -p Port_number

Replace “root” with a user that has sudo privileges. Additionally, replace “IP_Address” and “Port_Number” with your server’s respective IP address and SSH port number. Next, let’s make sure that we’re on Ubuntu 26.04. You can verify it with this command:

# lsb_release -a

You should get this as the output:

No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 26.04 LTS
Release:    26.04
Codename:    resolute

Then, run the following command to make sure that all installed packages on the server are updated to their latest available versions:

# apt update 

That’s it; the system package information should be updated now.

Step 2. Install Python

Almost every package we install relies on other packages to run properly or to enable installation. Before proceeding with the other steps, we need to install Python and its packages.

# apt install python3 python3-pip python3-venv

Step 3. Create a Python Virtual Environment

Python has been installed; we can now create a Python environment. Python virtual environment is an isolated environment that allows us to install dependencies without affecting the main Python system on our computer. This is very helpful in avoiding conflicts between different projects. Let’s install it now.

# mkdir /opt/flask
# cd /opt/flask
# python3 -m venv flask-venv
# source flask-venv/bin/activate

After running the “source flask-venv/bin/activate” command, you will be brought to the virtual environment.

Step 4. Install Flask and Gunicorn

We installed a Python virtual environment in the previous step. While in the virtual environment, execute this command:

(flask-venv) root@ubuntu26:/opt/flask# pip install flask gunicorn

Once completed, you will see a similar output to this:

Installing collected packages: packaging, markupsafe, itsdangerous, click, blinker, werkzeug, jinja2, gunicorn, flask
Successfully installed blinker-1.9.0 click-8.4.2 flask-3.1.3 gunicorn-26.0.0 itsdangerous-2.2.0 jinja2-3.1.6 markupsafe-3.0.3 packaging-26.2 werkzeug-3.1.8

Now, we can check both Flask and Gunicorn versions.

(flask-venv) root@ubuntu26:/opt/flask# flask --version

You will see an output like this:

Python 3.14.4
Flask 3.1.3
Werkzeug 3.1.8
(flask-venv) root@ubuntu26:/opt/flask# gunicorn --version

You will see:

gunicorn (version 26.0.0)

We can exit the virtual environment now.

(flask-venv) root@ubuntu26:/opt/flask# deactivate 

You will be out of the virtual environment shell and go back to the root SSS session.

Step 5. Create Flask App

For the purposes of this article, we will create a simple Flask application. Execute the command below:

# nano app.py

Insert the following into the file.

# app.py

from flask import Flask 

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, This is RoseHosting's Flask Test Page!"

Save the file, then exit. Now, let’s give the /opt/flask directory the correct permissions.

# chown -R www-data:www-data /opt/flask

Step 6. Create a Systemd File

To better manage our Flask service, we need to create a systemd file. Let’s create the file using the nano editor; you can use any other editor you like.

# nano /etc/systemd/system/flask.service

Insert the following into the file.

[Unit]
Description=Gunicorn instance to serve flaskapp
After=network.target 

[Service]
User=www-data
Group=www-data
WorkingDirectory=/opt/flask
Environment="PATH=/opt/flask/flask-venv/bin"
ExecStart=/opt/flask/flask-venv/bin/gunicorn --workers 3 --bind unix:flask.sock -m 007 app:app

[Install]
WantedBy=multi-user.target

Save the file, then exit from the editor. Now, we can reload the systemd daemon and start our Flask service.

# systemctl daemon-reload
# systemctl enable --now flask

Step 7. Install and Configure Nginx

In this tutorial, we will install and use Nginx instead of Apache. Run the following command to install it.

# apt install nginx -y

Next, we need to create an nginx server block or virtual host for our Flask service.

# nano /etc/nginx/conf.d/flask.conf

Insert the following into the file.

server {
    listen 80;
    server_name flask.yourdomain.com;

    location / {
        include proxy_params;
        proxy_pass http://unix:/opt/flask/flask.sock;
    }
}

Save the file, then exit and restart nginx

# systemctl restart nginx

At this point, you should be able to access your Flask website at http://flask.yourdomain.com

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
Flask Python Application with Gunicorn

Step 8. Install SSL Certificate

We are going to install an SSL certificate for our Flask website using the free SSL certificate from Let’s Encrypt. To achieve this, we can use Certbot. Certbot is a free and open-source tool for automatically generating and managing SSL/TLS certificates. Powered by Let’s Encrypt, it’s powerful and easy to use. Let’s execute the command below to install Certbot.

# apt install certbot python3-certbot-nginx -y

Once installed, you can run this command to issue an SSL certificate. Please make sure to replace the subdomain with your actual domain or subdomain name; it should match the one in the nginx server block configuration file we created earlier. Also, make sure the domain or subdomain is already pointing to your server’s IP address.

# certbot --nginx -d flask.yourdomain.com

Make sure to answer the prompts, and you will see an output like this:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): you@yourdomain.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at:
https://letsencrypt.org/documents/LE-SA-v1.8-July-06-2026.pdf
You must agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: n
Account registered.
Requesting a certificate for flask.yourdomain.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/flask.yourdomain.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/flask.yourdomain.com/privkey.pem
This certificate expires on 2026-10-15.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for flask.yourdomain.com to /etc/nginx/conf.d/flask.conf
Congratulations! You have successfully enabled HTTPS on https://flask.yourdomain.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

You installed an SSL certificate for your Flask website, and you should be able to access it with HTTPS at https://flask.yourdomain.com now.

You’ve Installed Flask Python Application with Gunicorn

Congratulations! You have learned how to set up a Flask Python application with Gunicorn and NGINX on Ubuntu 26.04

Of course, you don’t have to install Flask Python application with Gunicorn and NGINX on Ubuntu 26.04 if you have an Ubuntu server with us, in which case you can simply ask our expert Linux admins to install Flask Python application with Gunicorn and NGINX on Ubuntu 26.04 for you. They are available 24×7 and will address your request immediately. Managing a website is not just about installation; we can help you optimize it if you have an active service with us.

If you liked this post on installing Flask Python application with Gunicorn and NGINX on Ubuntu 26.04, please share it with your friends or leave a comment below.

Leave a Comment