使用域名在IIS中托管Flask应用的最佳实践 [英] Best practices to host Flask app in IIS with domain name

查看:64
本文介绍了使用域名在IIS中托管Flask应用的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个开发的 flask 应用程序(带有 SQL Server 数据库),该应用程序在本地公司防火墙后面运行.现在,我们需要本地网络之外的其他用户来访问此Web应用程序.由于我使用SQL Server(带有链接服务器选项)连接到网络中的其他本地数据库(因为我们没有API),因此我希望将Web应用程序托管在 IIS 本身(使用

  • 打开iis.右键单击服务器名称,然后选择添加站点.

  • 输入站点名称的物理路径和站点绑定.

  • 添加站点后,选择站点名称,然后从中间窗格中选择处理程序映射功能.

  • 从操作窗格中单击添加模块映射".

  • 可执行路径值:

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

  • 单击请求限制".确保未选中仅在请求映射到以下情况时调用处理程序:"复选框:

  • 在此处单击是":

  • 现在返回并选择应用程序设置功能.<​​/li>

  • 从操作窗格中单击添加.

  • 设置PYTHONPATH变量(这是您的站点文件夹路径):

  • 还有WSGI_HANDLER(我的Flask应用程序名为app.py,因此值为app.app -如果您的名称为site.py,则它将为site.app或类似名称):

单击确定"并浏览到您的站点.

如果要使用您的站点配置域名,则需要按以下方式设置站点绑定:

并将IP和域名绑定在位于"C:\ Windows \ System32 \ drivers \ etc \ hosts"的敌对位置

IP主机名

注意:这仅在本地有效.如果要为互联网用户设置域名,则需要购买域名和公共IP,并将IP与域名绑定.

要访问SQL Server iis用户没有足够的权限,您需要配置具有足够权限以使用iis应用程序池访问SQL Server的用户.

  • open iis mnager->选择应用程序池

  • 从操作窗格中选择高级设置.

  • 以身份检查自定义单选按钮,然后输入用户名和密码.

注意:确保将iis_iusrs和iusr权限分配给站点和python文件夹.

有关更多详细信息,您可以参考以下链接:

https://docs.microsoft.com/zh-CN/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2019

https://forums.iis.net/post/2159167.aspx

I have a flask app developed (with SQL Server db) which runs behind a corporate firewall locally. Now we need other users outside our local network to access this web app. Since I am using SQL Server (with Linked Server option) to connect to other local db in network (because we do not have API), I am looking to host the web app in IIS itself (using this tutorial). Please advise:

  1. Is it safe to host using IIS? Any best practices to be followed to safeguard our local network, in case any penetration. Can I run the web server in Virtual Machine? Will it make it safer?
  2. I also want to buy a domain name and associate with my web server IP address which will change frequently - any vendor suggestion where I can dynamically update when IP of my web server changes.

I am looking to learn the best practices to host flask web app locally using IIS with a domain name. Thanks for your inputs!

解决方案

you can follow the below steps to host the flask app on iis:

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

Download the python from the below link:

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

note: if possible please use the python version above 3.6.

after installing python install the wfastcgi.run the command prompt as administrator and run the below command:

pip install wfastcgi

wfastcgi-enable

below is my flask example:

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

after creating an application to run it use the below command:

python app.py

now enable the cgi feature of iis:

  • open iis.right-click on the server name and select add site.

  • enter the site name physical path and the site binding.

  • after adding the site select the site name and select the handler mapping feature from the middle pane.

  • Click "Add Module Mapping" from the action pane.

  • 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 the request is mapped to:" checkbox is unchecked:

  • Click "Yes" here:

  • now go back and select the application setting feature.

  • click add from the action pane.

  • Set the PYTHONPATH variable(which is your site folder path):

  • 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):

Click OK and browse to your site.

if you want to configure the domain name with your site you need to set the site binding as below:

and bind the IP and domain name in hostile which is located at "C:\Windows\System32\drivers\etc\hosts"

IP hostname

Note: this only works locally. if you want to set the domain for internet users you need to buy the domain name and public IP and bind the IP with the domain name.

To access the SQL server iis user does not have enough permission you need to configure the use which has enough permission to access the SQL server with iis application pool.

  • open iis mnager-> select application pool

  • select advance setting from the action pane.

  • in identity check the custom radio button and enter username and password.

Note: Make sure you assign the iis_iusrs and iusr permission to the site and python folder.

For more detail you could refer below links:

https://docs.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2019

https://forums.iis.net/post/2159167.aspx

这篇关于使用域名在IIS中托管Flask应用的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆