Skip to main content
All CollectionsVPSVPS Management
How to install Flask on Ubuntu 24.04
How to install Flask on Ubuntu 24.04

Installing Flask application on Ubuntu 24.04

Updated over a week ago

Flask is a lightweight, open-source web framework for Python, designed to help developers build modern web applications with ease. It provides essential features like routing, request handling, and Jinja2 templating, while allowing flexibility through extensions. With its simplicity, scalability, and strong community support, Flask is a popular choice for developing applications and APIs, from small prototypes to large-scale projects.

This guide walks you through the process of installing Flask with Gunicorn and Nginx on Ubuntu 24.04 VPS.

Step 1 - Connect to your VPS via SSH

Step 2 - Update your package list and installed packages

Run the following commands:

sudo apt update
sudo apt upgrade -y

Step 3 - Set up Flask application

A few essential components need to be installed, including Python, venv (the Python virtual environment module), pip (for managing Python packages), and Nginx. Run the following command to install the required packages:

sudo apt install python3 python3-pip python3-venv nginx -y

Step 4 - Create a Project Directory

Navigate to your home directory and set up a new folder by running the commands:

mkdir /home/user/flaskapp  
cd /home/user/flaskapp

Step 5 - Set up a Virtual Environment

By using a virtual environment helps manage your Python dependencies efficiently. Run the following commands to create and activate one

python3 -m venv flaskappenv  
source flaskappenv/bin/activate

Step 6 - Install Flask

With the virtual environment activated, install Flask along with Gunicorn for running the application:

pip install Flask gunicorn  

Step 7 - Create a basic Flask application

In your project directory, create a new file named app.py

nano flaskapp.py


then add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello, Flask!'

if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)


Run the flaskapp.py application using Python:

python3 flaskapp.py


An output confirming that the server is running should appear. Next, open your web browser and navigate to http://your_server_ip:8000. If the setup is correct, "Hello, Flask!" will be displayed on the page.

Step 8 - Create the WSGI entry point

This will tell the Gunicorn server how to interact with the application, create a new file called wsgi.py

nano wsgi.py

Then add the following configuration to it:

from flaskapp import app

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

Step 9 - Configure Gunicorn

Once the virtual environment setup is complete, deactivate it by running the following command:

deactivate

This will return you to the system’s default Python environment.

Next, create a systemd service unit file to allow Ubuntu's init system to automatically start Gunicorn and serve the Flask application each time the server boots up.

To begin, create a unit file with a .service extension in the /etc/systemd/system directory.

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


Then add the following (replace user with your actual user, and paths /home/user/flaskapp with your actual project path):

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

[Service]
User=user
Group=www-data
WorkingDirectory=/home/user/flaskapp
Environment="PATH=/home/user/flaskapp/flaskappenv/bin"
ExecStart=/home/user/flaskapp/flaskappenv/bin/gunicorn --workers 3 --bind unix:flaskapp.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target


You can now start the Gunicorn service that was created and enable it to start at boot. To check the service status, use the following command:

sudo systemctl start flaskapp
sudo systemctl enable flaskapp

and check the service status:

sudo systemctl status flaskapp


If any errors are visible in the service status, they should be resolved before continuing with the tutorial.

Step 10 - Configure Nginx

The Gunicorn application server should now be running and listening for requests through the socket file in the project directory. Next, configure Nginx to forward web requests to this socket by modifying its configuration.

Start by creating a new server block configuration file in Nginx’s sites-available directory. Name the file flaskapp to maintain consistency with the rest of the tutorial.

sudo nano /etc/nginx/sites-available/flaskapp

Then add the following command (Make sure to replace your_domain and path to the correct ones for your project)

server {
listen 80;
server_name your_domain www.your_domain;

location / {
include proxy_params;
proxy_pass http://unix:/home/user/flaskapp/flaskapp.sock;
}
}

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/flaskapp /etc/nginx/sites-enabled

With the file in that directory, test for syntax errors:

sudo nginx -t

If no issues are reported, restart the Nginx process to apply the new configuration using the following command:

sudo systemctl restart nginx

Finally, adjust the firewall, allowing full access to the Nginx server:

sudo ufw allow 'Nginx Full'

That's it! You should now be able to navigate to your server’s domain name in your web browser! 🚀

A Flask application was set up, Gunicorn was configured as the WSGI server, and a systemd service was created for automatic startup. Additionally, Nginx was set up to relay web traffic. This flexible stack can now be used to deploy Flask applications on your VPS.

Did this answer your question?