Flask Deploy on Apache
How To configure Flask and Apache
Last updated
Was this helpful?
How To configure Flask and Apache
Last updated
Was this helpful?
This page explains how to deploy a simple 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.
/ httpd
(on CentOS) server
web framework
Install server
Install
Test if the
mod_wsgi
module is loaded
Virtual environments will sandbox the app to run isolated from the global server environment
Go to /var/www
- the www_root of Apache server, and create the project directory.
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:
Where run.py
is responsible to bootstrap the Flask app defined in the app
directory.
app/init.py file contents
run.py file contents
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.
At this point, we will run the next commands inside the VENV
. Let's install Flask
To run, a Flask application require FLASK_APP
environment variable.
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:
wsgi.py file contents
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
Close and save the file and restart Apache to load the new settings.
Our Flask app should be served by the Apache HTTP server. We can test the deploy by using lynx
command:
Install
We will use a simple application that serves a simple Hello World
message for all routes. As mentioned before, this setup is executed on CentOs. The steps are: