Flask is a popular web development framework written in Python. Flask is considered a microframework because it does not require particular tools or libraries.
Ubuntu 22.04 is the latest long term version(LTS) released by Canonical Ltd. the company behind Ubuntu software and related projects.
In this tutorial we will guide you on how to install and deploy flask on Ubuntu 22.04 with Apache and WSGI.
Before we proceed on the tutorial we assume that Apache is already installed on your system.
Let us start with the installation.
Updating the system
Let us first update our system to make sure that all the installed packages are up to date.
sudo apt update -y && apt upgrade -y
Installing Pip and Virtual environment
Pip is a python package manager and it is use in installing and managing software package. Virtual environment on the other hand is an environment to isolate your application, setting up virtual environment is recommend if you have multiple applications on one server and want to isolate them from each other, it means you can setup your application with different python version or package without affecting other applications.
To install Pip and Virtual environment run this command.
apt-get install python3-pip python3-venv
Installing and deploying Flask
Before we start installing Flask let us first create a directory where we will deploy our application.
Create a directory.
mkdir -p /opt/flask
Then change directory to the one we created.
cd /opt/flask/
Let us create a virtual environment.
python3 -m venv venv
Activate the virtual environment.
source venv/bin/activate
Let us now install Flask.
(venv) pip install flask
To verify if the installation is successful check the flask version.
(venv) flask --version
Output:
Python 3.10.4
Flask 2.1.2
Werkzeug 2.1.2
Creating sample application
Let us create a sample flask application that will return “Welcome to Flask”.
To create a sample app , we will create a file named “app.py“. To do that run this command.
(venv) nano app.py
Then insert the following code.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def welcome():
return 'Welcome to Flask'
Save the file by pressing “ctrl x” then press “Y” then enter.
Let us test the application we created by using the flask command.
First we need to set up the FLASK_APP environment variable.
(venv) export FLASK_APP=hello.py
Now let us run the application.
(venv) flask run
Output:
* Serving Flask app 'app.py' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
The default port for flask development server is port 5000. You can open your browser and access the development server at http://127.0.0.1:5000.
If you installed the Flask application on a publicly accessible server and you want to access the development server publicly you need to run this command.
(venv) flask run --host=0.0.0.0
Output:
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on all addresses (0.0.0.0)
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://127.0.0.1:5000
* Running on http://1.1.1.1:5000 (Press CTRL+C to quit)
You should be able to access the development server at http://your_public_ip_address:5000
If you want to change the port of the development server you can run this command:
flask run –host=0.0.0.0 –port=8089
The development server can be now accessible through port 8089
After checking that everything is working we can now deactivate our virtual environment.
(venv) deactivate
Apache and mod_wsgi installation and configuration
We have assume that Apache is already installed on your server but if it is not installed you can run this command to install apache.
sudo apt install apache2
Let us install mod_wsgi package.
sudo apt-get install libapache2-mod-wsgi-py3
Let us create a WSGI file.
sudo nano /opt/flask/flaskapp.wsgi
Insert the following content to the WSGI file.
import sys
sys.path.insert(0,’/opt/flask’)
from app import app as application
Close the file and save it.
Now let us create an Apache virtual host for our flask application.
sudo nano /etc/apache2/sites-available/flask.conf
Insert the following in the flask.conf file.
<VirtualHost *:80>
ServerName yourdomain.com
ServerAdmin webmaster@localhost
DocumentRoot /opt/flask/
WSGIDaemonProcess app user=www-data group=www-data threads=5 python-home=/opt/flask/venv
WSGIScriptAlias / /opt/flask/flaskapp.wsgi
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /opt/flask>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Require all granted
</Directory>
</VirtualHost>
Pleas don’t forget to change the yourdomain.com to your own domain lose the file and save it.
Let us enable the Apache virtual host we created.
sudo a2ensite flask2.conf
Restart Apache for the new configuration to take effect.
sudo systemctl restart apache2
You should be able to access your application at http://yourdomain.com.
Conclusion
In this tutorial you have learned how to install and deploy Flask application on Ubuntu 22.04 with Apache and WSGI. If you like this article you might also like our article on Django on Ubuntu 20.04.
If you have questions, feel free to leave a comment and we will try to answer it.
Thank you and hope you enjoy our tutorial ?