How to host Python Flask API on Apache web Server?

In my earlier post we have gone thru step by step instructions to install 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 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 purpose but when you move to production you need to host it in Web server. Here is the example 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 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 my using https://serverhostname/apiname. It should work perfectly.

Hope this post was useful.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.