SoFunction
Updated on 2025-03-04

Steps to Deploy Flask Applications on Ubuntu

1. Deployment preparation

Before you begin, make sure you meet the following conditions:

  • A server running Ubuntu (such as Ubuntu 20.04 or 22.04) with SSH access.
  • The code of Python application ensures that it can run properly locally.

2. Install Python environment

1. Install Python 3

Ubuntu 20.04 and later have Python 3 installed by default, but if you don't have it on your system, you can install it manually:

sudo apt update
sudo apt install python3 python3-pip python3-venv -y
  • python3: Install the Python 3 interpreter.
  • python3-pip: Install Python package management tools.
  • python3-venv: Install virtual environment support.

2. Verify Python installation

After the installation is complete, you can verify that Python and pip are installed correctly by using the following command:

python3 --version
pip3 --version

You should see an output similar to the following:

Python 3.8.10
pip 21.1.2

3. Install MySQL (if required)

1. Install MySQL Server

sudo apt install mysql-server -y

2. Start and set MySQL boot

sudo systemctl start mysql
sudo systemctl enable mysql

3. Verify MySQL installation

Log in to MySQL to confirm that the installation is successful.

sudo mysql -u root -p

MySQL default password is root, just log in directly.

4. Modify the initial password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';

5. Create libraries and tables

Here we use SQL statements to build the database needed by the project.

4. Create a virtual environment

To avoid global installation of Python packages and dependencies, you can use Python's virtual environment to manage project dependencies.

1. Create a virtual environment

In the application directory, run the following command to create a virtual environment:

python3 -m venv myenv

This will create a namedmyenvvirtual environment.

2. Activate the virtual environment

After creating the virtual environment, you need to activate it:

source myenv/bin/activate

After activating the virtual environment, you will see that the terminal prompt prefix becomes(myenv), indicating that the virtual environment has been activated.

5. Release Python applications

1. Upload project files

Upload application files to the production server to ensure all source code files andAll files have been uploaded.

2. Installation project dependencies

passFile installation dependency.

pip install -r 

3. Start the project

Go to the application directory and run the Python application to make sure it starts normally:

python3 

If the application is running normally, the application access address and port number will be displayed.

6. Configure Nginx as a reverse proxy (optional)

To forward external requests to Python applications, Nginx is usually used as the reverse proxy.

1. Install Nginx

If Nginx is not installed yet, use the following command to install:

sudo apt update
sudo apt install nginx -y

2. Configure Nginx

Open the Nginx configuration file and edit the reverse proxy settings:

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

existserverIn the block, configure the reverse proxy to forward the request to the port where the Python application is located (assuming that the Python application runs on port 8000):

server {
    listen 80;
    server_name your_domain_or_ip;

    location / {
        proxy_pass http://127.0.0.1:8000;  # Forward the request to a Python application        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Save and exit the editor.

3. Test Nginx configuration

Execute the following command to test whether the Nginx configuration is correct:

sudo nginx -t

If the configuration is correct, the output should be displayedsyntax is okayandtest is successful

4. Reload Nginx

Reload Nginx to apply the configuration:

sudo systemctl reload nginx

7. Set up system services (optional)

To ensure that the Python application starts automatically after the system restarts, you can create a systemd service file.

1. Create the systemd service file

Create a new service file:

sudo nano /etc/systemd/system/

Add the following:

[Unit]
Description=Python Application
After=

[Service]
User=your_user
Group=your_group
WorkingDirectory=/path/to/your/app
ExecStart=/path/to/your/app/myenv/bin/python3 /path/to/your/app/
Restart=always

[Install]
WantedBy=
  • UserandGroup: Set as the user and group to run the application.
  • WorkingDirectory: Point to the directory of the application.
  • ExecStart: Point to the Python interpreter and application portal in the Python virtual environment.

2. Reload the systemd configuration and start the service

sudo systemctl daemon-reload
sudo systemctl start 
sudo systemctl enable 

3. Check service status

sudo systemctl status 

Confirm that the application is running in the background and listens for the correct port.

8. Configure a firewall

If your server has a firewall enabled (for example UFW), make sure to allow HTTP (port 80) and the port your application uses (for example 8000) through the firewall:

sudo ufw allow 80
sudo ufw allow 8000

9. Test deployment

After the deployment is complete, access the server domain name or IP address you configured to check whether the Python application is normally served through Nginx.

10. Summary

Through this tutorial, we successfully deployed a Python application on Ubuntu, including steps such as environment preparation, dependency management, configuring reverse proxy, setting up system services, and log management. This deployment is ideal for production environments, ensuring that applications start automatically after system restarts and can handle highly concurrent external requests via Nginx.

The above is the detailed process steps for deploying Flask applications on Ubuntu. For more information about deploying Flask applications on Ubuntu, please follow my other related articles!