How to host Python Flask API on Apache web Server?

In my earlier post we have gone thru step by step instructions to install the Apache web server from scratch and host HTTPS enabled website into it. In this blog, we will go thru the step-by-step instructions to host Python Flask APIs in the Apache Web server. This is a requirement when you want to integrate front end Application(Angular SPA) with Azure AD because Angular SPA should call API hosted in Https Endpoint to work with Azure AD. In the production environment, you can not use Flask to host your API and you definitely need a production web server like Apache or Gunicorn. Flask is good for development purposes but when you move to production you need to host it on the Web server. Here is an example of Architecture where it can be helpful.

Step 1. Deploy Mod_ssl and apache web server. Please refer to my earlier post.

Step 2: Install Mod_wsgi module.

Please note that mod_wsgi module is required to host Flask API into the Apache web server.

yum -y mod_wsgi

Step 3: Perform Module configuration.

Add the below lines in the ssl.cnf ( SSL config file). Please refer to my earlier post to know what is ssl.cnf’s role here.

# You can define the thread counts for WSGI module and assign the script alias
WSGIDaemonProcess user=apache threads=5

#A request for in this case would 
#cause the server to run #the WSGI application defined in /web/wsgi-scripts/name 
#so this is very important

WSGIScriptAlias / /var/www/API/src/application_wsgi.wsgi

# is hosting the code for your Flask API
<Directory /var/www/API/src/>
    WSGIApplicationGroup %{GLOBAL}
    <IfVersion < 2.4>
        Order allow,deny
# Allow from All allows access from all
        Allow from all
    <IfVersion >= 2.4>
        Require all granted

Step 3: Test the API call by using https://serverhostname/apiname. It should work perfectly.

Hope this post was useful.

1 comment

Add yours
  1. 1

    The article on how to host Python Flask API on Apache web Server is given very nicely over here. As a fellow professional i have came across many fine companies with service provider like India access and hostasp.

Leave a Reply