Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dvas0004/3d26c25d614c54ecdf296003d68cddaa to your computer and use it in GitHub Desktop.
Save dvas0004/3d26c25d614c54ecdf296003d68cddaa to your computer and use it in GitHub Desktop.
Python Flask on IIS with wfastcgi

Assume IIS is installed. My machine already had IIs 8.5.

DV : Adding some notes when using MS IIS Express 10

Install Python

  1. Download web installer (Python 3.6.3).
  2. Run as Administrator.
  3. Select custom installation for all users.
  4. Choose install directory such that there are no white spaces in the path. Not sure if it needs to be done. Just being cautious.
  5. Check the box for "Add Python 3.6 to PATH".

Install wfastcgi and others

  1. Open Windows Powershell as Adminstrator.
  2. Run: pip install wfastcgi
  3. Run: pip install flask

Setting up Website

  1. On the powershell, run: wfastcgi-enable It will produce configuration related output. Example:

    Applied configuration changes to section "system.webserver/fastcgi" for "MACHINE/WEBROOT/APPHOST" at configuration commit path "MACHINE/WEBROOT/APPHOST".
    "c:\python36\python.exe|c:\python36\lib\site-packages\wfastcgi.py" can now be used as a FastCGI script processor.
    

    DV: Make a note of the above output, the last line needs to be used in web.config later on. For IIS Express w/ default config and setup, run:

     wfastcgi-enable "C:\Program Files (x86)\IIS Express\appcmd.exe" /apphostconfig:"C:\Users\davev\Documents\IISExpress\config\applicationhost.config"
    
  2. Run IIS as administrator.

    DV: For IIS Express simply open cmd, navigate to "C:\Program Files (x86)\IIS Express" and issue command "iisexpress.exe" Skip steps 3-6

  3. Go to Connections and expand the tree.

  4. Select "Sites".

  5. Select "Add Website" under Actions panel on the right of the window.

  6. A new window will pop up titled "Add Website". Fill in the necessary info: Site name, Directory containing the website content, IP address and port (I entered 5000). Since I want to simply run it on local host, IP address can be left as "All unassigned".

  7. The Physical Path you specified in Add Website contains the following files (barebones):

    **DV: For IIS Express by default this would be in your Documents folder, under "My Web Sites", example: "C:\Users\davev\Documents\My Web Sites\WebSite1". Create the "web.config" file in this directory

    a. web.config: contains web configuration. It has the following content:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <system.webServer>
      <handlers>
       <remove name="Python27_via_FastCGI" />
       <remove name="Python34_via_FastCGI" />
       <add name="Python FastCGI"
          path="*"
          verb="*"
          modules="FastCgiModule"
          scriptProcessor="C:\Python36\python.exe|C:\Python36\Lib\site-packages\wfastcgi.py"
         
          resourceType="Unspecified"
          requireAccess="Script" />
      </handlers>
    </system.webServer>
    <appSettings>
      <!-- Required settings -->
      <add key="WSGI_HANDLER" value="myapp.app" />
      <add key="PYTHONPATH" value="C:\inetpub\wwwroot\stealth" />
    </appSettings>
    </configuration>
    

    b. myapp.py: contains Flask application:

    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/hello")
    def hello():
        return "Hello Stealth!"
    
  8. You might have to restart the Server and the website after configuration changes. Option will be under Actions on the right.

  9. If you select the root node, you'll see a bunch of configuration features. We are interested in FastCGI Settings and Handler Mappings. a. Under FastCGI settings, I have the following:

    Full Path                |          Arguments
    c:\python36\python.exe   |          c:\python36\lib\site-packages\wfastcgi.py
    c:\Program Files\PHP\php-cgi.exe
    

    b. Under Handler Mappings, you'll see different names. Based on web.config, you'll see "Python FastCGI".

  10. You can now enter "localhost:5000" into the browser.

DV: found this link useful: https://pypi.org/project/wfastcgi/

@Javit96
Copy link

Javit96 commented Feb 14, 2020

If i have a virtualenv, then i have to modif the python path on web.config file?

@pranavjahagirdar
Copy link

If i have a virtualenv, then i have to modif the python path on web.config file?

Yes, you should be doing all the commands inside your virtualenv, installing wfastcgi, enabling it everything. So, when you run wfastcgi-enable, the path shown in command prompt will be the path that you should be using.

@jeffny2015
Copy link

jeffny2015 commented Nov 25, 2020

Any idea how to set up flask wfastcgi with iis to support more than 500 concurrent users ???

@uqji1
Copy link

uqji1 commented Feb 27, 2021

What if the flask app need to be run as ">flask run -? -? -?" mode, with the 'app.app' wrapped inside the create_app() in the package init file.

@postboxat18
Copy link

is wfastcgi support python 3.10?

@emcourtney1
Copy link

re: uqji1
To use a flask app written with a create_app() method in its init.py file use:
add key="WSGI_HANDLER" value="<your_package_name>.create_app()"

Further hint, your_package_name will be the name of the directory enclosing the init.py file

@boytjuge
Copy link

boytjuge commented Apr 5, 2024

wfastcgi รองรับ python 3.10 หรือไม่

yes i use 3.12 work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment