6

As wfastcgi module is not compatible with Python 3.7, What is the best way to host a python flask application on a Windows Server?

Ishan
  • 81
  • 1
  • 1
  • 5
  • If you cannot upgrade wfastcgi on your own, follow https://stackoverflow.com/tags/wfastcgi/info – Lex Li Oct 06 '19 at 22:31
  • The latest version of wfastcgi only support till Python 3.6. I am running Python 3.7 and getting the below error: the factcgi process exited unexpectedly. – Ishan Oct 07 '19 at 03:49
  • @Ishan Is your issue solved? If your issue is solved then I request you to mark the helpful suggestion as an answer. This will help other people who face the same issue. If your issue still exists then try to refer the solution given by the community members. If then also you have any further questions then let us know about it. We will try to provide further suggestions to solve the issue. Thanks for your understanding. – Jalpa Panchal Oct 10 '19 at 08:22

1 Answers1

13

you need to install the python,wfastcgi, and flask at your server.

You can download the python from below link:

https://www.python.org/downloads/

after installing python download the wfastcgi:

pip install wfastcgi

run the command prompt as administrator and run this command.

wfastcgi-enable

run this command to enable wfastcgi.

below is my flask example:

app.py:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello from FastCGI via IIS!"
if __name__ == "__main__":
app.run()

enter image description here

after creating an application to run it use below command:

python app.py

now enable the cgi feature of iis:

enter image description here

  • now open iis.
  • right-click on the server name and select add site.
  • enter the site name physical path and the site binding.
  • after adding site select the site name and select the handler mapping feature from the middle pane.
  • Click “Add Module Mapping” enter image description here
  • add below value:

enter image description here

enter image description here

enter image description here

executable path value:

C:\Python37-32\python.exe|C:\Python37-32\Lib\site-packages\wfastcgi.py

  • Click “Request Restrictions”. Make sure “Invoke handler only if request is mapped to:” checkbox is unchecked:

enter image description here

  • Click “Yes” here:

enter image description here

  • now go back and again select the server name and select fast CGI setting from the middle pane.

enter image description here

  • Double click it, then click the “…” for the Environment Variables collection to launch the EnvironmentVariables Collection Editor:

enter image description here

  • Set the PYTHONPATH variable:

enter image description here

  • And the WSGI_HANDLER (my Flask app is named app.py so the value is app.app — if yours is named site.py it would be site.app or similar):

enter image description here

  • Click OK and browse to your site:

enter image description here

Note: Do not forget to assign the iusr and iis_iusrs user permission to the flask site folder and python folder.

Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26
  • I am getting the below error now for the same code. I have checked the port 5000 is open and have given necessary permissions to iusr and iis_usrs: line 466, in server_bind self.socket.bind(self.server_address) OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions StdOut: * Serving Flask app "app" (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 StdErr: – Ishan Oct 07 '19 at 22:14
  • there are some issues in posting code. please check the edited post.this is the actual code: `from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello from FastCGI via IIS!" if __name__ == "__main__": app.run()` – Jalpa Panchal Oct 08 '19 at 02:51
  • make sure you install flask by using this command: `pip install Flask` – Jalpa Panchal Oct 08 '19 at 08:32
  • The code still isn't running and the error is below: OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions StdOut: – Ishan Oct 10 '19 at 10:19
  • @Ishan did you assign the iis_iusrs and iusr permission to the python and site folder? when did you get this error before hosting in iis or after hosting in iis? or try to se different port number. for more detaile refer this [link](https://stackoverflow.com/questions/48682476/how-to-prevent-flask-redirecting-to-the-default-port-of-5000) – Jalpa Panchal Oct 11 '19 at 01:46
  • I followed a guide similar to this and was able to get my flask app working with FastCGI. Regarding the note at the end of your answer: If you are seeing permission issues, I suggest changing your Anonymous Authentication Credentials. Select your Sites->Website Name->Virtual Folder then double-click on the Authentication button (a man with a lock icon). Click the Anonymous Authentication row and then Edit. Change the selection from "Specific User" to "Application Pool Identity" and click OK. – Jeff Hines Aug 06 '20 at 02:25
  • Make sure that port on which you are trying to run flask is not already occupied using netstat -na | find "your_port", if port is occupied then you need to stop a process running on that port, and then restart the flask app, then you will not get fobidden by its access permission error. – AutomationNerd Apr 13 '21 at 15:07
  • I had to put both parameters in double quote saparated by bar "|" e.g. `"c:\python39\python.exe"|"c:\python39\Lib\site-packages\wfastcgi.py"` – dawncode May 30 '21 at 12:41
  • I want to know, how can we deploy a compiled(.pyc) file on IIS and will it be able to run the code? – Piyush Sharma Jun 24 '22 at 13:49
  • How does one `assign the iusr and iis_iusrs user permission to the flask site folder and python folder`? Thanks. – tommy.carstensen Dec 01 '22 at 09:47