配置 IIS 为 Django 应用程序提供服务
Configure IIS to Serve Django Applications
我正在尝试通过 IIS 发布 Django 网络应用程序(使用 Python 2.7.15
创建)。首先我安装了应用程序:
:: setup folders
mkdir C:\Software\MyApp\
cd C:\Software\MyApp\
:: clone the git Repository
git clone https://gitlab.com/blablabla.git C:\Software\MyApp\MyAppServer
:: create the virtualenv
python -m pip install --upgrade pip
pip install virtualenv
virtualenv py2_env
call py2_env\Scripts\activate.bat
:: install the python-requirements in the virtualenv
pip install -r C:\Software\MyApp\MyAppServer\requirements.txt
:: copy all static files in C:\var\www
python C:\Software\MyApp\MyAppServer\manage.py collectstatic
:: just to check if the app works (and it works!)
python C:\Software\MyApp\MyAppServer\manage.py runserver
此时,我的文件夹组织是:
C:\
└── Software\
└── MyApp\
├── py2_env\
│ ├── Include\
│ ├── Lib\
│ ├── Scripts\
│ └── tcl\
└── MyAppServer\
├── manage.py
├── ...
└── myapp_django_server\
├── urls.py
├── wsgi.py
└── ...
我的 py2_env
虚拟环境中安装的软件包是:
(py2_env) C:\Software\MyApp\py2_env\Scripts>pip freeze --local
Django==1.11.2
djangorestframework==3.6.3
numpy==1.12.1
pytz==2018.7
wfastcgi==3.0.0
此时我按照 this guide (I jumped to the Configure IIS to Serve Django Applications chapter) but, at the end, if I browse to http://localhost:81 配置 IIS 我得到这个错误:
Error occurred:
Traceback (most recent call last):
File "C:\Software\EVARplanning\py2_env\Lib\site-packages\wfastcgi.py", line 847, in main
result = handler(record.params, response.start)
TypeError: get_wsgi_application() takes no arguments (2 given)
StdOut:
StdErr:
谁能告诉我怎么了?
问题出在我的 WSGI_HANDLER
和 IIS 的 FastCGI Settings
中。我将它的值从 django.core.wsgi.get_wsgi_application
转换为 django.core.wsgi.get_wsgi_application()
然后它起作用了。
您只需要 manage.py
旁边的 web.config
文件即可。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="Django" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\web\my-api\Scripts\python.exe|D:\web\my-api\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
</handlers>
</system.webServer>
</configuration>
还要确保您网站在 IIS 中的 ApplicationPool
使用的是 LocalSystem
而不是 ApplicationPoolIdentity
。
其余的应该很容易设置。
我正在尝试通过 IIS 发布 Django 网络应用程序(使用 Python 2.7.15
创建)。首先我安装了应用程序:
:: setup folders
mkdir C:\Software\MyApp\
cd C:\Software\MyApp\
:: clone the git Repository
git clone https://gitlab.com/blablabla.git C:\Software\MyApp\MyAppServer
:: create the virtualenv
python -m pip install --upgrade pip
pip install virtualenv
virtualenv py2_env
call py2_env\Scripts\activate.bat
:: install the python-requirements in the virtualenv
pip install -r C:\Software\MyApp\MyAppServer\requirements.txt
:: copy all static files in C:\var\www
python C:\Software\MyApp\MyAppServer\manage.py collectstatic
:: just to check if the app works (and it works!)
python C:\Software\MyApp\MyAppServer\manage.py runserver
此时,我的文件夹组织是:
C:\
└── Software\
└── MyApp\
├── py2_env\
│ ├── Include\
│ ├── Lib\
│ ├── Scripts\
│ └── tcl\
└── MyAppServer\
├── manage.py
├── ...
└── myapp_django_server\
├── urls.py
├── wsgi.py
└── ...
我的 py2_env
虚拟环境中安装的软件包是:
(py2_env) C:\Software\MyApp\py2_env\Scripts>pip freeze --local
Django==1.11.2
djangorestframework==3.6.3
numpy==1.12.1
pytz==2018.7
wfastcgi==3.0.0
此时我按照 this guide (I jumped to the Configure IIS to Serve Django Applications chapter) but, at the end, if I browse to http://localhost:81 配置 IIS 我得到这个错误:
Error occurred:
Traceback (most recent call last):
File "C:\Software\EVARplanning\py2_env\Lib\site-packages\wfastcgi.py", line 847, in main
result = handler(record.params, response.start)
TypeError: get_wsgi_application() takes no arguments (2 given)
StdOut:
StdErr:
谁能告诉我怎么了?
问题出在我的 WSGI_HANDLER
和 IIS 的 FastCGI Settings
中。我将它的值从 django.core.wsgi.get_wsgi_application
转换为 django.core.wsgi.get_wsgi_application()
然后它起作用了。
您只需要 manage.py
旁边的 web.config
文件即可。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="Django" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\web\my-api\Scripts\python.exe|D:\web\my-api\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
</handlers>
</system.webServer>
</configuration>
还要确保您网站在 IIS 中的 ApplicationPool
使用的是 LocalSystem
而不是 ApplicationPoolIdentity
。
其余的应该很容易设置。