Python Flask is a popular web framework used for building web applications. Once you have built your Flask application, you need to deploy it on a server so that users can access it over the internet. Apache2 is a widely-used web server that can host Flask applications using the Mod WSGI module. In this blog post, we will walk you through the steps to deploy a Python Flask app on Apache2 with Mod WSGI, which is a common deployment strategy for Flask apps. By the end of this post, you will have a fully-functional Flask app running on your Apache2 web server.
How do I set up a python flask app on Apache2?
Click me to skip to Mod WSGI Config
Python Flask App Configuration
Before deploying a Python Flask app on Apache2 with Mod WSGI, it’s important to ensure that your app is tested and working correctly on your local environment. Here is some extra configuration for your Flask app to make it compatible with Apache2.
- Build and Test Locally: Make sure that your Flask app is built and tested locally on your development environment. This ensures that your app is functioning correctly and any issues or bugs are resolved before deploying it on the server.
- Install Mod WSGI Package: In addition to the packages required by your Flask app, you’ll also need to install the
mod_wsgi
package. This package provides an Apache module that allows Apache to serve `Flask` applications. You can install mod_wsgi
using the following command:
pip install mod_wsgi
- Set up a WSGI file: Next, you’ll need to create a WSGI file that tells Apache how to run your Flask app. For example, you can create a file called
myapp.wsgi
in the directory where your Flask app is located with the following contents:
import sys
sys.path.insert(0, '/var/www/myApp')
from main import application
In this file, we import our Flask app from the main.py
file. The sys.path is the path on your server where you will server the web server files from. Note: Your sys.path may be different, this post is assuming you’re hosting on Ubuntu server, and depending on how you’ve named your Python Flask app files your myapp.wsgi file could look something like:
import sys
sys.path.insert(0, '/var/www/myApp')
from myapp import app as application
Web Server Initial Configuration
Before we dive into the details of deploying a Python Flask app on Apache2 with Mod WSGI, we need to ensure that our Ubuntu server has the necessary software installed. Here are the packages that need to be installed:
- Apache2: This is the web server software that we will use to host our Flask app. It’s the most widely used web server software on the internet, and it’s included in Ubuntu’s default repositories. To install Apache2, run the following command:
sudo apt-get update
sudo apt-get install apache2
- Mod WSGI: This is an Apache module that allows Apache to serve Flask applications. To install Mod WSGI, run the following command:
sudo apt-get install libapache2-mod-wsgi-py3
- Python3: Flask is built on top of Python, so we need to ensure that our server has Python3 installed. To install Python3, run the following command:
sudo apt-get install python3
- Python3 pip: We will use pip to install the necessary Python packages required for our Flask app. To install pip, run the following command:
sudo apt-get install python3-pip
Once we have these packages installed, we can proceed with the deployment of our Flask app on Apache2 with Mod WSGI.
Next we need to load our app into our Ubuntu server.
- Log in to your Ubuntu server and navigate to the
/var/www
directory using the terminal.
- Clone your Flask app repository using the following command:
sudo git clone https://github.com/your_username/your_app.git myApp
This command will clone your app repository into a new directory called myApp
inside the /var/www
directory.
Setting up a Python virtual environment:
To set up a virtual environment for your app, you can use the venv
module in Python. Here are the steps to create a virtual environment:
- Open a terminal and navigate to the directory where you want to create the virtual environment.
- Run the following command to create a virtual environment:
python3 -m venv venv
This will create a new directory called venv
in your current directory that contains a Python environment.
- Activate the virtual environment using the following command:
source myenv/bin/activate
You should see (venv)
appear at the beginning of your terminal prompt, indicating that you are now working within the virtual environment.
Installing app requirements:
Once you have activated the virtual environment, you can install the required packages using pip
. You can create a requirements.txt
file that lists all the packages required by your app and install them using the following command:
pip install -r requirements.txt
After we have installed the requirements we need to give Apache2 access to our apps directory:
- Change the ownership of the
myApp
directory to the Apache2 user (www-data
) using the following command:
sudo chown -R www-data:www-data myApp
This command will ensure that the Apache2 server has permission to access and serve files from the myApp
directory.
Mod WSGI Configuration
Configuring Apache2 to serve a Flask app involves several steps, including enabling the Mod WSGI module, creating a virtual host configuration, and setting up a WSGI file.
To enable the Mod WSGI module on Apache2, you can use the following commands on your terminal:
sudo a2enmod wsgi
sudo service apache2 restart
The first command enables the mod_wsgi
module, while the second command restarts the Apache2 service to apply the changes.
There are some changes we need to make to make sure the mod_wsgi module knows how to operate correctly. First we need to give the wsgi.conf file the value of “WSGIPythonPath”.
cd /etc/apache2/mods-available
sudo nano wsgi.conf
Here you want to find and uncomment the WSGIPythonPath line and add the relevant folders that WSGI needs Python to access, they are separated by a colon:
WSGIPythonPath var/www/myapp:/var/www/myapp/venv:/var/www/myapp/venv/lib/python3.10/site-packages
In the directory we need to edit the wsig.load file with the following details. Your mod_wsgi server file may have a slightly different name but it should start with ‘mod_wsgi’ and end in ‘.so’. Follow the path below to find your own.
LoadModule wsgi_module "/var/www/myapp/venv/lib/python3.10/site-packages/mod_wsgi/server/mod_wsgi-py310.cpython-310-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/myapp/venv"
This should be the mod_wsgi module configured correctly and ready to go.
Virtual Host Configuration
To create a virtual host configuration for your Flask app, you’ll need to create a new Apache configuration file. For example, you can create a file called myapp.conf
in the /etc/apache2/sites-available
directory with the following contents:
<VirtualHost *:80>
ServerName myapp.example.com
ServerAdmin [email protected]
WSGIScriptAlias / /path/to/myapp.wsgi
<Directory /path/to/myapp></code>
Options ExecCGI MultiViews Indexes FollowSymLinks SymLinksIfOwnerMatch
MultiViewsMatch Handlers
AddHandler wsgi-script .wsgi
DirectoryIndex app.wsgi
AllowOverride all<code>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/myapp_error.log
CustomLog ${APACHE_LOG_DIR}/myapp_access.log combined
</VirtualHost>
In this configuration, we have defined a virtual host for the myapp.example.com
domain. The WSGIScriptAlias
directive specifies the path to the WSGI file that will run our Flask app. The <Directory>
block allows Apache to serve files from the directory where our Flask app is located. The ErrorLog
and CustomLog
directives specify the log files where Apache will write any errors or access logs for our app.
After creating the virtual host configuration, you’ll need to enable the virtual host configuration by creating a symbolic link in the /etc/apache2/sites-enabled
directory. If you have previously served sites on this server this is probably already done, but if not follow the next step. For example, you can use the following command:
sudo ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/
Once you’ve completed these steps, you can restart the Apache2 service using the following command:
sudo service apache2 restart
Your Flask app should now be accessible at the URL specified in your virtual host configuration.