Deploy python3.6 flask web app on Windows IIS

Environment

  • Windows Server 2012 R2
  • IIS 8.5

Requirement

System (download the installer)

Python Package (use “pip install”)

  • flask
  • wfastcgi

References

Steps

  1. Open Server Manager -> Manage -> Add Role and Features and make sure you have CGI installed.
  2. Install python3.6 and the Web Platform Installer(WPI).
  3. Install URL Rewrite
    1. Open IIS, click on the server name, then open WPI on the bottom of the page.
    2. Search “URL” in WPI, then install URL Rewrite.
    3. After this action, your IIS should looks like this:
  4. Install wfastcgi
    1. Open Command Prompt in Admin Privilege.
    2. Type in “pip install wfastcgi“, then Enter, wfastcgi should be installed properly.
    3. After the installation, we can use “wfastcgi-enable” to enable service, and “wfastcgi-disable” to disable it.
    4. Execute “wfastcgi-enable“, then remember the quote part of the output string, we will use that later.
  5. Add a new site in IIS, nothing special to talk about.
  6. Open your website’s directory, put everything in it, then add a new “web.config” file in the directory.
    1. Open “web.config”, add these lines:
      <?xml version="1.0" encoding="UTF-8"?>
      <configuration>
          <system.webServer>
              <handlers>
                 <add name="FlaskFastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="c:\program files\python36\python.exe|&quot;c:\program files\python36\lib\site-packages\wfastcgi.py&quot;" resourceType="Unspecified" requireAccess="Script" />
              </handlers>
              <security> 
                  <requestFiltering allowDoubleEscaping="true"></requestFiltering> 
              </security> 
          </system.webServer>
          <appSettings>
              <!-- Required settings -->
              <add key="WSGI_HANDLER" value="run.app" />
              <add key="PYTHONPATH" value="~/" />
          </appSettings>
      </configuration>
    2. There’s two things you should notice.
      1. In the
        <add name="FlaskFastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="c:\program files\python36\python.exe|&quot;c:\program files\python36\lib\site-packages\wfastcgi.py&quot;" resourceType="Unspecified" requireAccess="Script" />

        field, there’s a scriptProcessor value. This value should be modified as your “wfastcgi-enable” quote part of the output.
        Notice that you probably shouldn’t paste it directly, you should delete the quote symbol in the first part (the python executable file part), and replace the second part’s(the wfastcgi.py path) quote symbol as “&quot;”.
        It took me a lot of time to figure out why IIS didn’t work properly.

      2. In the
        <add key="WSGI_HANDLER" value="run.app" />

        field, the “xxx” in “xxx.app” value should be modified as your website start file’s name(for me it’s run.py, then the “xxx.app” is modified to “run.app”).

    3. Save changes, then you are on the go.

Visit “localhost:[your port]” in browser, then you should see the content of your website. Don’t forget to add firewall rules if you want to visit it on other PCs.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据