环境: win10 + IIS7.5 + Python3.7.1 32bit + Django(v1.11.20) + wfastcgi
一、Open IIS + CGI
控制面板>Programs>windows features
二、Source code
1. copy the source code to ’D:\websites\Demo‘
2. setup virtual env(虚拟环境)
三、wfastcgi enable
发布server若为局域网,连接外网受限制,pip install wfastcgi 失败,因此可以采用下面两种方法
1. 从已有的环境中copy
2. 别处下载wfastcgi setup files(wfastcgi 3.0.0)copy到发布server上, enable 虚拟环境venv,并把路径切到下载的wfastcgi所在位置 , 执行'python setup.py install'
如果采用方法1,wfastcgi-script.py’,‘wfastcgi-enable-script.py’ ,‘wfastcgi-disable-script.py’这三个文件的第一行python.exe路径需要改为enable的虚拟环境python.exe 路径
admin运行cmd,
红色框里的显示路径需要copy一下,在IIS配置中需要用到。
四、配置web.config
Django项目根目录下添加web.config文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="Python FastCGI"
path="*"
verb="*"
modules="FastCgiModule"
scriptProcessor="xxx\python.exe|xxx\Lib\site-packages\wfastcgi.py"【这里就是刚才记录的地址】
resourceType="Unspecified"
requireAccess="Script"/>
</handlers>
</system.webServer>
<appSettings>
<add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" />
<add key="PYTHONPATH" value="D:\websites\Demo" />【这是是网站的根目录】
<add key="DJANGO_SETTINGS_MODULE" value="DemoApp.settings" />【这里是Django网站的配置,其他框架相应调整即可】
</appSettings>
</configuration>
如果全局配置了Fast CGI 和模块映射(IIS配置中Fast CGI设置 路径|参数),可以不要 <system.webServer> 节点,直接简化如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" />
<add key="PYTHONPATH" value="D:\websites\Demo" />【这是是网站的根目录】
<add key="DJANGO_SETTINGS_MODULE" value="DemoApp.settings" />【这里是Django网站的配置,其他框架相应调整即可】
</appSettings>
</configuration>
另外,对于django的static文件,我们还需要配置web.config,放到static目录下,否则会出现找不到static file error
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<!-- this configuration overrides the FastCGI handler to let IIS serve the static files -->
<handlers>
<clear/>
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
</handlers>
</system.webServer>
</configuration>
五、建立站点
打开IIS manager(控制面板->管理工具->Internet Information Services(IIS) Manager)
1.网站->右击->添加网站
2. 为网站添加identity,自定义用户名密码(Custom account)
六、unlock modify configs
如果在设好站点后访问站点时出现HTTP 错误 500.19 Internal Server Error,是因为IIS7之后版本采用了更安全的 web.config 管理机制,打开CMD,可以执行下面命令解锁:
>%windir%\system32\inetsrv\appcmd unlock config -section:system.webServer/handlers
>%windir%\system32\inetsrv\appcmd unlock config -section:system.webServer/modules