Flask is a WSGI (Web Server Gateway Interface) compatible micro web framework written in Python. The easy learning curve and wide adoption have made Flask, one of the first choices for REST-API development in python. Due to the wide adoption, we can find a lot of tutorials and blog posts on ‘How to Deploy Flask Apps in Linux Servers’ in contrast to ‘Flask App Deployment in Windows’. However, there can be situations where one want to deploy one’s Flask applications on Windows-Servers too. In such a scenario someone might find this blog post handy and helpful.
Before starting the guide you need to make sure that your Windows machine has following installed.
- Visual C++ Redistributable for Visual Studio
- Visual C++ Build Tools (https://visualstudio.microsoft.com/visual-cpp-build-tools/)
Installing Apache HTTP Server
Since we are focusing on production deployment the recommended mechanism is to deploy the application behind a battle-tested, production-ready web-server. If I am trying to deploy a Flask application in a Linux machine I would prefer Nginx Web-Server but for Windows machines, I would go with Apache HTTP Server. Well, let’s install Apache HTTP Server on our windows machine.
- Go to https://httpd.apache.org/docs/2.4/platform/windows.html and download wamp or apache standalone from there.
- I would go with apache-lounge and it can be downloaded from here, https://www.apachelounge.com/download/
- Extract the files and copy Apache24 to C:\Apache24 (This is the default location). Here onwards I would call this location as <Apache24-Home>
Here the number 24 is because we are using version 2.4, so intuitively we can understand what this location will be for a different version.
- Typically the structure of the extracted folder is as follows,
Note: Installation instructions can also be found in the Readme.txt which resides in the extracted folder.
Running Apache HTTP Server as a Windows Service
- Open command prompt terminal as administrator.
- Change directory into <Apache24-Home>\bin.
- Install Apache Server as a windows server.
httpd.exe -k install
- Open the windows service manager.
- Look for Apache2.4 (or your installed version). There we can start, restart, and stop the service as we wish.
- Now go to http://localhost and it should say, ‘It Works!’
Before installing mod_wsgi, we need to get the code for our Flask application into the deployment machine and then create a python3 virtualenv (say flask-app-env) for the application. After that, we need to install required flask and other dependencies into flask-app-env.
- Open a command prompt and activate flask-app-env and keep the environment activated throughout the following instructions.
- Install mod_wsgi package.
pip install mod_wsgi
Configuring mod_wsgi in Apache Server.
- Run the following command to get the configuration of the installed mod_wsgi for Apache Server.
The above command would output something like the following;
LoadFile "c:/users/curwsl/appdata/local/programs/python/python37/python37.dl"LoadModule wsgi_module "d:/flo2d-rest-service/flo2d/flo2d-venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"WSGIPythonHome "d:/flo2d-rest-service/flo2d/flo2d-venv"
- Copy the output of the above command and paste it in <Apache24-Home>\conf\httpd.conf. We can paste at the very end of the file.
- Deactivate flask-app-env and close the command prompt.
Note: For further details please refer to https://github.com/GrahamDumpleton/mod_wsgi#connecting-into-apache-installation
Configuring Apache Server to Deploy the Falsk App
Making the Apache Server Listen to a Desired Port
The first thing we need to do is to make the Apache Server listen to the port that we plan to expose the Flask App through. By default, Apache Server listens to the port 80 (the default HTTP port).
However, if we want to make Apacher Server listen to a particular port do the following;
- Add “Listen <port>” to <Apache24 root>/conf/httpd.conf
Configure a Virtual Host for the Flask App
In Apache 2.4 Server virtual host configurations are placed in the <Apache24-Home>\conf\extra\httpd-vhosts.conf file. Thus we need to edit that particular file.
- Open <Apache24-Home>\conf\extra\httpd-vhosts.conf for editing.
- Add the following virtual host configuration segment with corresponding changes into <Apache24-Home>\conf\extra\httpd-vhosts.conf.
ServerName server-name-here(e.g localhost:5000)
WSGIScriptAlias / "D:/myapp/app/index/web.wsgi"
Allow from all
Require all granted
CustomLog "D:/myapp/app/logs/access.log" common
In the above configuration, there are three important things.
- Directory privileges (i.e what we have within <Directory> </Directory> tag)
ServerName is the domain name for our Flask App. It can be a purchased one or just localhost.
WSGIScriptAlias specifies what script should be executed when the Apache Server get a request for <domain-name> root (e.g http://localhost:5000/).
<Directory/> is there for security purposes. Directory tag should allow the access to the WSGI-Script(i.e. web.wsgi)’s residing directory but not beyond that.
Note: Here D:/myapp/app is the root directory of the flask application code. From now on I would call it <flask-app-home>.
Include the virtual-hosts file (i.e. httpd-vhosts.conf) into httpd.conf
- Search for “httpd-vhosts.conf” in <Apache24 root>/conf/httpd.conf
- Uncomment the ‘Include’ line
- Or simply add the following lines;
# Virtual hosts
Create web.wsgi in <flask-app-root>/index/
You need to create a web.wsgi file with the following content in the <flask-app-home>/index/ directory.
from your_app_script import app as application
Obviously your your_app_script.py should look something like the following;
from flask import Flask
app = Flask(__name__)
return "Hello World"
if __name__ == "__main__":
That’s all folks. Happy Falsking on Windows! :D