Tuesday, April 24, 2018

Configure IIS To Host Python

Overview

To host python on an IIS web server, you must install python and configure IIS on the machine. 

In this case my organization doesn't allow internal servers any internet connectivity at all, so i had to figure out a way to get the dependent python packages via a manual download onto the server and install manually.  

For some reason I had a hard time configuring the proxy for python, so I run Telerik Fiddler to funnel my traffic through the proxy anytime i need to install packages via pip.   


The reason why I wanted to use Python for this project is it's really easy to prototype Object oriented libraries and expose a REST API via Flask.  I was able to turn around a proof of concept whole project from inception to functioning site in a week (while juggling my other responsibilities).

Steps

1.       Figure out which Packages you need
a.       This step assumes you have Python installed locally.  If not, download the Python installer and run it
b.      From your local workstation, run Fiddler.  This will funnel python web requests via the proxy
c.       Open a command line as administrator
d.      Use "pip" to install the packages you want. 
e.      Use the command "pip freeze > requirements.txt" to generate a list of installed packages.  IF you can't use pip, write down the dependencies so you can find and download them manually

f.      Use command `pip download -r requirements.txt -d offline_install` to download the packages you need.  IF you can't use pip: go to PyPi, search for and download all the dependencies and packages you need
                                      i.            Make sure you download the correct package for the version of python you have
                                    ii.            If there is a choice of a wheel file (.whl) or a source zip, choose the wheel file
g.       For a Flask application, you will likely need a minimum of
                                      i.            click
                                    ii.            Flask
                                  iii.            itsdangerous
                                   iv.            Jinja
                                     v.            MarkupSafe
                                   vi.            Werkzeug
                                 vii.            Wfastcgi
h.      Your flask website will likely have a routing file in it where you instantiate the Flask app. 
                                      i.            I will refer to the file name (without an extension) where the flask instance is created as <FILE>, e.g. for file "flask_app.py", <FILE> would be "flask_app".
                                    ii.            The name of your Flask instance variable will be referred to as <APP>, e.g. if in your script you say app = Flask() then <APP> would be "app"
                                  iii.            <FILE>.<APP> will be the name of your file (no extension) and app separated by a period.  e.g. flask_app.app
2.       Install Python and the necessary packages on the server
a.       Copy the Python installer to the server (I used version 3.6.5 for this example)
b.      Install python
                                      i.            When installing, choose a path without any spaces, e.g. C:\Python3.6
                                    ii.            I will refer back to this path as <MyPythonPath>
c.       Copy the package files  to the server
d.      Run "pip" to install each local package
                                      i.            You may need to use the full path of pip to install, unless you add the python directory to your PATH environment variable
3.       Configure IIS to use Python with FastCGI
a.       Open IIS and choose "Add role Services"
b.      Ensure CGI is selected
c.       Right click on "sites" and choose "Add Web Site"
d.      Add a site with whatever name you choose, e.g. ReportingSite. 
                                      i.            You will need to select the folder where your web site will be at this stage
                                    ii.            I will refer back to this folder as <MyProject>, e.g. D:\Reporting\ReportingSite
                                  iii.            You will also need to select a port that is not in use
e.      Click on the web site you created and double click "Handler Mappings"
f.        Pick "Add Module Mapping" from the right
g.       At each prompt enter the following, replacing <MyPythonPath> with the directory where you installed python in step 2b.
                                      i.            Request Path: *
                                    ii.            Module: FastCgiModule
                                  iii.            Executable:  <MyPythonPath>\python.exe|<MyPthonPath>\lib\site-packages\wfastcgi.py
                                   iv.            Name: python-wfastcgi
h.      When you continue, you will be prompted to create a FastCGI application. Click "Yes"
i.        Not e that the wfastcgi package should have put the wfastcgi.py file in the folder you specified.  Check that it exist just to be sure.
j.        Click on your new website again and go to "Application settings" under ASP.Net and add:
                                      i.            PYTHONPATH as <MyProject>
                                    ii.            WSGI_HANDLER as  <FILE>.<APP>
4.       Create a rule in the firewall to allow requests to go through
a.       Open windows firewall
b.      Add an Incoming rule to allow requests on the port you specified in step 3d

No comments:

Post a Comment