I wrote an article on how to run python Flask application on Apache Server on a Windows machine. The setup is so tricky that I thought I should write another article on running python Flask Application from an IIS server. I hope this helps some one.
- If you don’t already have IIS installed go to Start->contol panel->Programs->Programs and Features->Turn Windows Features on or Off
- Make Sure CGI is installed
- Now go to this previous post on How to Deploy Python Flask Application with Apache on a Windows Server and Follow the instructions up to number 12. It will show you how to install python and run the Develoment Server for python and come back and continue from here.
- Install wfastcgi by running pip install wfastcgi from the command line. And copy the wfastcgi.py file from C:\Python34 to the root of your application. C:\myapp\app
- Now open the command line as admin and Run
C:\Windows\System32>cd /myapp/app icacls . /grant "NT AUTHORITY\IUSR:(OI)(CI)(RX)" icacls . /grant "Builtin\IIS_IUSRS:(OI)(CI)(RX)"
The codes above allows IIS to read and execute the files in the directory of your application. It took me a week to figure this out. Trust me this can be a nightmare if you do not run those two commands. It will look like this
5. Now open IIS and create a new website with the root at your application like this
6. Now click on the name of your website in IIS on the left and click on Handle Mapping as show below
6. Click on Add Module Mapping on the right and complete the fields on the forms like so
Before you click on OK, click on Request Restrictions and uncheck the box that says Invoke handler only if request is mapped to:
Click on OK and OK again to save the changes
7. Now click on the server name on the left and Go to the root server settings and click “FastCGI Settings”:
8. Double Click on the python handler for your application and click on the Environmental Variable Collections with the “…” as show on the image below
9. Set the PYTHONPATH like the image below where by the Value is the path to your application in my case it is C:\myapp\app
10. Set the WSGI_HANDLER which in this case is the name of your application file. If yours is mysite.py the make it mysite.app in my case it is app.py so it will be app.app
11. Now go to your browser and type in http://localhost and you will see
Now you have IIS running python website. Let me know if you have any questions.
Note: Don’t forget to install wfastcgi by running pip install wfastcgi from the command line. And move the wfastcgi file to the root of your application.
install wfastcgi – https://pypi.python.org/pypi/wfastcgi
Another tutorial – http://netdot.co/2015/03/09/flask-on-iis/
Another breakdown – http://stackoverflow.com/questions/6823316/python-on-iis-how
If you enjoyed this article, share or tell a friend and be on the lookout for more from storehubs.com/Blog