How to Install Flask on Ubuntu 22.04 with Apache and WSGI

install flask on ubuntu 22.04

In this tutorial, we are going to show you how to install the Flask application on Ubuntu 22.04 with Apache Web server and mod WSGI.

Flask is a very popular web framework written in Python and used by many developers worldwide. The Apache is the Web server where requests are coming to the application, and the Mod WSGI is the Apache module that implements a WSGI-compliant interface for hosting Python-based web applications.

Installing Flask with Apache and WSGI on Ubuntu 22.04 is a process that may take up to 30 minutes. Let’s get started!

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

Before installing the software, we need to update the system packages to the latest versions available.

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install Apache Web Server

To install the Apache Web server execute the following command:

sudo apt install apache2 -y

Once installed, start and enable the service.

sudo systemctl enable apache2 && sudo systemctl start apache2

Check if the service is up and running:

sudo systemctl status apache2

You should receive the following output:

root@host:~# sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-11-07 13:30:41 CST; 1 week 0 days ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 747 (apache2)
      Tasks: 55 (limit: 4575)
     Memory: 11.3M
        CPU: 49.680s
     CGroup: /system.slice/apache2.service
             ├─  747 /usr/sbin/apache2 -k start
             ├─70464 /usr/sbin/apache2 -k start
             └─70465 /usr/sbin/apache2 -k start

Nov 13 00:00:02 host.test.vps systemd[1]: Reloading The Apache HTTP Server...

Step 3. Install Python

We need to install the latest version of python3 for Ubuntu 22.04 and the python package manager pip along with the python virtual environment. To achieve this execute the following commands:

sudo apt-get install python3 python3-pip python3-venv

To check the installed python version, execute the following command:

python3 -V

You should get the following output:

root@host:~# python3 -V
Python 3.10.6

Step 4. Install Flask Application

The next step is to install and deploy the flask application in the virtual environment.

To create the virtual environment in the opt directory, execute the following command:

cd /opt && mkdir flask-app

cd flask-app

python3 -m venv flask-venv

Once the virtual flask environment is created, you need to activate it using the following command:

source flask-venv/bin/activate

The command prompt will change and will look as described below:

root@host:/opt/flask-app# source flask-venv/bin/activate
(flask-venv) root@host:/opt/flask-app#

The next step is to install the flask application inside the virtual environment with the following command:

pip3 install flask

The successfully installed flask application will have the following output:

(flask-venv) root@host:/opt/flask-app# pip3 install flask
Collecting flask
  Downloading Flask-2.2.2-py3-none-any.whl (101 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 101.5/101.5 KB 2.4 MB/s eta 0:00:00
Collecting click>=8.0
  Downloading click-8.1.3-py3-none-any.whl (96 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.6/96.6 KB 4.3 MB/s eta 0:00:00
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting Werkzeug>=2.2.2
  Downloading Werkzeug-2.2.2-py3-none-any.whl (232 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 232.7/232.7 KB 4.8 MB/s eta 0:00:00
Collecting Jinja2>=3.0
  Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.1/133.1 KB 4.0 MB/s eta 0:00:00
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: MarkupSafe, itsdangerous, click, Werkzeug, Jinja2, flask
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.1 Werkzeug-2.2.2 click-8.1.3 flask-2.2.2 itsdangerous-2.1.2

Next is to create a sample flask application python file:

sudo nano app.py

Insert the following lines of code:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello World'

Save the file, close it and set up the FLASK_APP environment variable.

export FLASK_APP=app.py

Now, we can test the application by running the following command:

flask run --host=0.0.0.0

You will receive the following output:

(flask-venv) root@host:/opt/flask-app# flask run --host=0.0.0.0
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://YourServerIP:5000
Press CTRL+C to quit
Hello World
YourServerIP - - [15/Nov/2022 07:37:13] "GET / HTTP/1.1" 200 -

As you can see in the output, this is a development server and is not recommended in a production deployment. We need to use the WSGI server instead. You can quit the running process with the CTRL+C command and deactivate the environment with the deactivate command for now. In the next step, we will configure the Apache Web server with mod WSGI.

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

Step 5. Create WSGI file

Before we create a wsgi file in the virtual flask environment, we first need to install some packages on the server level. To install the required libraries, execute the following command:

sudo apt-get install libapache2-mod-wsgi-py3

Once installed, create the file:

sudo nano /opt/flask-app/flask-app.wsgi

Paste the following lines of code, save and close the file.

import sys
sys.path.insert(0,'/opt/flask-app')

from app import app as application

We are done with this part. Let’s go to the next step.

Step 6. Create Apache Virtual Host File

Now, in the last step, we will create an Apache virtual host file and will configure it to work with the Mod WSGI parameters and flask-app.wsgi file.

touch /etc/apache2/sites-available/flask.conf

Open the file, and paste the following lines of code:

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
<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /opt/flask-app/

WSGIDaemonProcess app user=www-data group=www-data threads=5 python-home=/opt/flask-app/flask-venv
WSGIScriptAlias / /opt/flask-app/flask-app.wsgi

ErrorLog ${APACHE_LOG_DIR}/flask-error.log
CustomLog ${APACHE_LOG_DIR}/flask-access.log combined

<Directory /opt/flask-app>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Require all granted
</Directory>
</VirtualHost>

Enable the Apache2 configuration file:

sudo a2ensite flask.conf

Check the syntax of the Apache2 configuration.

apachectl -t

You should receive the following output:

root@host:~# apachectl -t
Syntax OK

If you receive this output, you can restart the Apache service safely.

sudo systemctl restart apache2

Now, you can access the Flask Web interface via your domain at http://YourDomain.com.

That’s it. Congratulations, you successfully installed and configured Flask Application on Ubuntu 22.04 with Apache and WSGI. If you find this process difficult, you can always contact our technical support, who will help you with any aspect of this configuration. Feel free to contact us anytime you want. We are available 24/7.

If you liked this post about installing Flask on Ubuntu 22.04 with Apache and WSGI, please share it with your friends on social networks or simply leave a reply below.

3 thoughts on “How to Install Flask on Ubuntu 22.04 with Apache and WSGI”

    • You can follow our blogpost at https://www.rosehosting.com/blog/how-to-install-lets-encrypt-with-apache-on-ubuntu-22-04/ to install a free SSL certificate from Let’s Encrypt

      Reply
  1. Very clear and complete tutorial. I’m migrating from Perl DBI / CGI.pm to Python SQL Alchemy and Flask / Apache and this was a great jumpstart. One thing I would mention is that if you need an SSL connection change the first line in the flask.conf to read:

    Reply

Leave a Comment