0

So my web.config currently looks like the following for production and the site works when I visit it

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Python310\python.exe|C:\Python310\lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
  </system.webServer>
<appSettings>
    <add key="WSGI_HANDLER" value="wsgi.app" />
    <add key="PYTHONPATH" value="E:\apps\prod" />
    <add accessType="Allow" users="*" />
  </appSettings>
</configuration>

However reading https://docs.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2022#configure-the-httpplatform-handler it suggests that isn't the way to go so have amended the web.config to the below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
    </handlers>
    <httpPlatform processPath="C:\Python310\python.exe"
                  arguments="E:\Apps\prod\wsgi.py --port %HTTP_PLATFORM_PORT%"
                  stdoutLogEnabled="true"
                  stdoutLogFile="E:\logs\python.log"
                  startupTimeLimit="60"
                  processesPerApplication="16">
      <environmentVariables>
        <environmentVariable name="SERVER_PORT" value="%HTTP_PLATFORM_PORT%" />
      </environmentVariables>
    </httpPlatform>
  </system.webServer>
</configuration>

Now, when I go to the site, it just spins around for a bit and the log file says:

 * Serving Flask app 'digital' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses (0.0.0.0)
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://127.0.0.1:5000
 * Running on http://x.x.x.x:5000 (Press CTRL+C to quit)

Sure enough if I go to the IP, it runs but when I go to my domain/hostname, it doesn't run and instead, hangs and logs the above to file

wsgi.py

"""
Application entry point
"""

from digital import init_app

app = init_app()

if __name__ == "__main__":
    app.run(host="0.0.0.0",threaded=True)

Any suggestions?

Thanks

pee2pee
  • 351
  • 1
  • 3
  • 11
  • 1. Your .py file didn't make use of the port number passed from IIS. That prevents your Python app server to run on the port IIS expects and things break apart as expected. 2. The warning of "this is a development server" is well explained in posts like https://stackoverflow.com/a/54381386/11182 so you need to switch to a production Python app server. – Lex Li Jul 07 '22 at 05:25
  • If I put in 443 then I get an error of `An attempt was made to access a socket in a way forbidden by its access permissions` I am trying to use HttpPlatform which is what MS suggest – pee2pee Jul 07 '22 at 08:03
  • How do I get it to run on SSL along with the hostname as it currently works with fastcgi but using httpPlatformHandler or even waitress – pee2pee Jul 07 '22 at 09:07

1 Answers1

0

Multiple issues are there with your IIS setup, so Python via HttpPlatformHandler won't work yet.

To learn more of a typical setup, you can refer to my blog post.

Interpretation of web.config

With the XML configuration as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
    </handlers>
    <httpPlatform processPath="C:\Python310\python.exe"
                  arguments="E:\Apps\prod\wsgi.py --port %HTTP_PLATFORM_PORT%"
                  stdoutLogEnabled="true"
                  stdoutLogFile="E:\logs\python.log"
                  startupTimeLimit="60"
                  processesPerApplication="16">
      <environmentVariables>
        <environmentVariable name="SERVER_PORT" value="%HTTP_PLATFORM_PORT%" />
      </environmentVariables>
    </httpPlatform>
  </system.webServer>
</configuration>

You must make sure that,

  1. In wsgi.py the proper code is written to pass %HTTP_PLATFORM_PORT% via either the environment variable SERVER_PORT or the argument --port.
  2. Your wsgi.py should launch a production Python application server such as Unicorn (as discussed extensively in https://stackoverflow.com/a/54381386/11182), not the Flask dev server.

However, you failed to do any of those in the current wsgi.py. Thus, as a Python developer you need to dig further into that part, and many useful resources are out there from search engines.

Correct URLs to Use in Browsers

You didn't reveal IIS site bindings here, so it is impossible to comment what are the correct URLs you can use to access the site, with IP addresses or host names.

That's very much a separate question, because IIS site bindings are not easy to understand themselves. But you might get some hints from my post.

SSL/HTTPS

Again, that's also a separate topic.

Please first start with an HTTP site binding, and once that's working you can move on to configure a valid HTTPS binding with proper certificate. I also have some hints.

Lex Li
  • 912
  • 6
  • 10