Django上線部署之IIS
環境:
1.Windows Server 2016 Datacenter 64位
2.SQL Server 2016 Enterprise 64位
3.Python 3.6.0 64位
4.administrator使用者,無域環境,最高許可權
要求:
按照順序部署
1.安裝資料庫
2.安裝資料庫客戶端【SSMS】
3.安裝Python
4.安裝IIS
5.安裝CGI
6.將原始碼拷貝至【C:\inetpub\wwwroot\MySite】
(MySite是自定義資料夾,wwwroot是IIS預設網站存放的資料夾,為避免許可權問題,不建議變動)
7.cmd 執行:pip install -r requirements(安裝依賴包)
8.cmd 執行:pip install wfastcgi
9.cmd 執行:wfastcgi-enable(獲取指令碼處理器資訊,第8步需要使用)
【scriptProcessor】
結構:<Python安裝路徑>\python.exe|<Python安裝路徑>\lib\site-packages\wfastcgi.py
例如:【d:\programs\python\python.exe|d:\programs\python\lib\site-packages\wfastcgi.py】
7.開啟IIS管理器新增網站,網站名稱為【MySite】,物理路徑為【C:\inetpub\wwwroot\MySite】,選擇IP和埠
8.在【C:\inetpub\wwwroot\MySite】資料夾下新增檔案【web.config】,內容如下(【】內是需要替換的部分):
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 <system.webServer> 4 <handlers> 5 <add name="Python FastCGI" 6 path="*" 7 verb="*" 8 modules="FastCgiModule" 9 scriptProcessor="【d:\programs\python\python.exe|d:\programs\python\lib\site-packages\wfastcgi.py】" 10 resourceType="Unspecified" 11 requireAccess="Script"/> 12 </handlers> 13 </system.webServer> 14 <appSettings> 15 <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" /> 16 <add key="PYTHONPATH" value="【C:\inetpub\wwwroot\MySite】" /> 17 <add key="DJANGO_SETTINGS_MODULE" value="【專案名.settings】" /> 18 </appSettings> 19 </configuration>
9.在網站【MySite】新增【static】虛擬目錄
10.在【C:\inetpub\wwwroot\MySite\static】資料夾下新增檔案【web.config】,內容如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <configuration> 4 <system.webServer> 5 <!-- this configuration overrides the FastCGI handler to let IIS serve the static files --> 6 <handlers> 7 <clear/> 8 <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" /> 9 </handlers> 10 </system.webServer> 11 </configuration>View Code
11.在網站【MySite】新增【media】虛擬目錄
12.在【C:\inetpub\wwwroot\MySite\media】資料夾下新增檔案【web.config】,內容如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 <system.webServer> 4 <!-- this configuration overrides the FastCGI handler to let IIS serve the static files --> 5 <handlers> 6 <clear/> 7 <add name="MediaFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" /> 8 </handlers> 9 </system.webServer> 10 </configuration>View Code
許可權相關的報錯,執行如下三步:
13.cmd 執行:%windir%\system32\inetsrv\appcmd unlock config -section:system.webServer/handlers
14.cmd 執行:%windir%\system32\inetsrv\appcmd unlock config -section:system.webServer/modules
15.右鍵點選【MySite】,點選【編輯許可權】,在【安全】選項卡里給IIS_IUSERS賦予【完全控制】的