Flask Application

How To Deploy a Flask Application on Ubuntu 24.04 or Newer

Welcome to the Greenhost.cloud blog! Today, we’re going to walk you through the steps necessary to deploy a Flask application on Ubuntu 24.04 or newer. Flask is a lightweight web framework for Python that allows you to create web applications quickly and easily. Ubuntu, being one of the most popular Linux distributions, provides a solid foundation for hosting your applications.

Prerequisites

Before we dive into the deployment steps, ensure you have the following:

  1. Ubuntu 24.04 or newer installed on your server.
  2. A non-root user with sudo privileges.
  3. Python 3 and pip installed. You can check if they are installed by running:
   python3 --version
   pip3 --version
  1. Basic knowledge of command-line operations.

Step 1: Update Your System

It’s always a good practice to update your system before installing new software. Run the following commands to update your package list and upgrade your installed packages:

sudo apt update
sudo apt upgrade -y

Step 2: Install Required Packages

You will need to install some packages to get your Flask application up and running. This includes Python itself, pip for managing Python packages, and a web server like Nginx or Apache. In this example, we’ll use Nginx due to its performance and ease of use.

Install the necessary packages:

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

Step 3: Set Up Your Flask Application

  1. Create a project directory: Navigate to your home directory and create a new directory for your Flask application:
   mkdir ~/myflaskapp
   cd ~/myflaskapp
  1. Create a virtual environment: It’s a good idea to use a virtual environment to manage your Python dependencies. Run the following commands:
   python3 -m venv venv
   source venv/bin/activate
  1. Install Flask: With the virtual environment activated, install Flask:
   pip install Flask gunicorn
  1. Create a simple Flask application: Create a file called app.py and add the following code:
   from flask import Flask

   app = Flask(__name__)

   @app.route('/')
   def hello():
       return "Hello, Flask on Ubuntu 24.04!"

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

Step 4: Test Your Flask Application

Before setting up Nginx, let’s test if your Flask application runs correctly. In your project directory, run:

python app.py

You should see output indicating that the server is running. Open a web browser and navigate to http://your_server_ip:8000. You should see “Hello, Flask on Ubuntu 24.04!” displayed in your browser.

Step 5: Configure Gunicorn

Gunicorn is a Python WSGI HTTP server that will serve your Flask application. To start Gunicorn, run the following command:

gunicorn --bind 0.0.0.0:8000 app:app

Step 6: Set Up Nginx as a Reverse Proxy

Now, we’ll configure Nginx to act as a reverse proxy to forward requests to Gunicorn.

  1. Create an Nginx configuration file for your Flask application:
   sudo nano /etc/nginx/sites-available/myflaskapp
  1. Add the following configuration:
   server {
       listen 80;
       server_name your_server_ip;  # Change this to your server's IP or domain

       location / {
           proxy_pass http://127.0.0.1:8000;
           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;
       }
   }
  1. Enable the new configuration: Create a symbolic link to the sites-enabled directory:
   sudo ln -s /etc/nginx/sites-available/myflaskapp /etc/nginx/sites-enabled
  1. Test the Nginx configuration for syntax errors:
   sudo nginx -t
  1. Restart Nginx:
   sudo systemctl restart nginx

Step 7: Run Gunicorn as a Service

To ensure your application runs continuously, even after a server restart, you can create a systemd service for Gunicorn.

  1. Create a service file:
   sudo nano /etc/systemd/system/myflaskapp.service
  1. Add the following configuration:
   [Unit]
   Description=Gunicorn instance to serve myflaskapp
   After=network.target

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

   [Install]
   WantedBy=multi-user.target

Be sure to replace your_username with your actual username.

  1. Start and enable the Gunicorn service:
   sudo systemctl start myflaskapp
   sudo systemctl enable myflaskapp

Step 8: Access Your Flask Application

Now that everything is set up, you can access your Flask application by navigating to your server’s IP address in a web browser. You should see the “Hello, Flask on Ubuntu 24.04!” message.

Conclusion

Congratulations! You’ve successfully deployed a Flask application on Ubuntu 24.04 or newer. This setup uses Gunicorn as the application server and Nginx as a reverse proxy, providing a robust environment for your Flask apps.

Feel free to reach out to us at Greenhost.cloud if you have any questions or need assistance with your web hosting needs. Happy coding!