Flask Deploy on Apache
How To configure Flask and Apache
This page explains how to deploy a simple Flask application sandboxed with a virtualenv and served by Apache HTTP server using the mod_wsgi module.
Note: this setup was tested on CentOS but can be easily adapted to be executed on other platforms.
​

Dependencies

​

Prepare the environment

Install Apache server
1
$ sudo yum install httpd
2
$
3
$ # by default the server is down.
4
$ sudo systemctl start httpd
Copied!
Install mod_wsgi​
1
$ sudo yum install mod_wsgi
2
$
3
$ # restart apache
4
$ sudo systemctl restart httpd
Copied!
Test if the mod_wsgi module is loaded
1
$ sudo httpd -M | grep wsgi
2
wsgi_module (shared) # <-- the OK response
Copied!
Install Virtualenv​
Virtual environments will sandbox the app to run isolated from the global server environment
1
$ sudo pip install virtualenv
Copied!

​

Code the Flask App

We will use a simple Flask application that serves a simple Hello World message for all routes. As mentioned before, this setup is executed on CentOs. The steps are:
Go to /var/www - the www_root of Apache server, and create the project directory.
1
$ cd /var/www
2
$ mkdir hitme
Copied!
To have a runnable Flask app, we need to create two files: run.py and app.py inside the app folder. The files are structured as below:
1
/var/www/hitme
2
| - run.py
3
| - app/__init__.py
Copied!
Where run.py is responsible to bootstrap the Flask app defined in the app directory.
app/init.py file contents
1
from flask import Flask
2
app = Flask(__name__)
3
​
4
@app.route("/")
5
def hello():
6
return "Hello world!"
Copied!
run.py file contents
1
import os
2
from app import app
3
​
4
#----------------------------------------
5
# launch
6
#----------------------------------------
7
​
8
if __name__ == "__main__":
9
port = int(os.environ.get("PORT", 5000))
10
app.run(host='0.0.0.0', port=port, debug=True)
Copied!

​

Test the Flask App

We have the test application, now let's start it to see something on the screen. First, we need to create and activate the virtual environment.
1
$ cd /var/www
2
$ virtualenv --python=python3 hitme # the venv is created inside the app folder
3
$ cd /var/www/hitme
4
$ source bin/activate
Copied!
At this point, we will run the next commands inside the VENV. Let's install Flask
1
$ pip install flask
Copied!
To run, a Flask application require FLASK_APP environment variable.
1
$ export FLASK_APP=run.py # please notice the name
2
$ flask run # start the app
3
$ # our app is running on port 5000
Copied!

​

Apache Configuration

To execute a Flask application under the Apache HTTP server we need to bridge our application to the Apache engine using the mod_wsgi module. For this we need to create a new file wsgi.py inside our project folder:
1
/var/www/hitme
2
| - wsgi.py
3
| - run.py
4
| - app/__init__.py
Copied!
wsgi.py file contents
1
#!/usr/bin/env python
2
​
3
import sys
4
import site
5
​
6
site.addsitedir('/var/www/hitme/lib/python3.6/site-packages')
7
​
8
sys.path.insert(0, '/var/www/hitme')
9
​
10
from app import app as application
Copied!
The next step is to configure Apache to serve the app and use this wsgi loader. The following settings should be added to the httpd.conf. On CentOS the file location is /etc/httpd/conf/httpd.conf
1
<VirtualHost *:80>
2
​
3
ServerName localhost
4
​
5
WSGIDaemonProcess hitme user=apache group=apache threads=2
6
​
7
WSGIScriptAlias / /var/www/hitme/wsgi.py
8
​
9
<Directory /var/www/hitme>
10
Require all granted
11
</Directory>
12
​
13
</VirtualHost>
Copied!
Close and save the file and restart Apache to load the new settings.
1
$ # restart apache
2
$ sudo systemctl restart httpd
Copied!
Our Flask app should be served by the Apache HTTP server. We can test the deploy by using lynx command:
1
$ lynx localhost # lynx
Copied!
Flask App deployed on Apache
Last modified 6d ago