如何在 Apache 中使用 Django?
Posted
技术标签:
【中文标题】如何在 Apache 中使用 Django?【英文标题】:How to use Django with Apache? 【发布时间】:2017-11-17 00:50:27 【问题描述】:有人可以帮我看看有什么问题吗?
我需要使用Django + Apache
。
当我尝试启动 Apache 时,它显示错误:The requested operation has failed.
我尝试将Listen 80
更改为不同的值,但没有帮助。在将下一个设置添加到httpd.conf
文件后也会出现此错误(没有这些设置 Apache 启动正常):
LoadModule wsgi_module modules/mod_wsgi.so
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
我用:
1) Apache 2.4 (x64)
2) Python 3.5.2 (x64)
3) Django 1.10.5
4) mod_wsgi‑4.5.15+ap24vc14‑cp35‑cp35m‑win_amd64.whl
5) Windows 8.1 (x64)
也许我用错了mod_wsgi
?我从mod_wsgi‑4.5.15+ap24vc14‑cp35‑cp35m‑win_amd64.whl
存档中获取mod_wsgi.cp35-win_amd64.pyd
文件并将该文件重命名为mod_wsgi.so
。之后将该文件放入 Apache 的模块文件夹中。
error.log 文件如下所示:
[Wed Jun 14 21:42:30.381146 2017] [mpm_winnt:notice] [pid 11892:tid 692] AH00455: Apache/2.4.2 (Win64) configured -- resuming normal operations
[Wed Jun 14 21:42:30.381146 2017] [mpm_winnt:notice] [pid 11892:tid 692] AH00456: Server built: May 11 2012 20:42:30
[Wed Jun 14 21:42:30.381146 2017] [core:notice] [pid 11892:tid 692] AH00094: Command line: 'C:\\Program Files\\Apache2.4\\bin\\httpd.exe -d C:/Program Files/Apache2.4 -f C:\\Program Files\\Apache2.4\\conf\\httpd.conf -d C:\\Program Files\\Apache2.4\\.'
[Wed Jun 14 21:42:30.381146 2017] [mpm_winnt:notice] [pid 11892:tid 692] AH00418: Parent: Created child process 13624
[Wed Jun 14 21:42:30.646739 2017] [mpm_winnt:notice] [pid 13624:tid 328] AH00354: Child: Starting 64 worker threads.
[Wed Jun 14 21:42:40.736467 2017] [mpm_winnt:notice] [pid 11892:tid 692] AH00422: Parent: Received shutdown signal -- Shutting down the server.
[Wed Jun 14 21:42:42.759516 2017] [mpm_winnt:notice] [pid 13624:tid 328] AH00364: Child: All worker threads have exited.
[Wed Jun 14 21:42:42.775140 2017] [mpm_winnt:notice] [pid 11892:tid 692] AH00430: Parent: Child process exited successfully.
[Wed Jun 14 21:42:51.755536 2017] [mpm_winnt:notice] [pid 7988:tid 696] AH00455: Apache/2.4.2 (Win64) configured -- resuming normal operations
[Wed Jun 14 21:42:51.756537 2017] [mpm_winnt:notice] [pid 7988:tid 696] AH00456: Server built: May 11 2012 20:42:30
[Wed Jun 14 21:42:51.756537 2017] [core:notice] [pid 7988:tid 696] AH00094: Command line: 'C:\\Program Files\\Apache2.4\\bin\\httpd.exe -d C:/Program Files/Apache2.4 -f C:\\Program Files\\Apache2.4\\conf\\httpd.conf -d C:\\Program Files\\Apache2.4\\.'
[Wed Jun 14 21:42:51.757538 2017] [mpm_winnt:notice] [pid 7988:tid 696] AH00418: Parent: Created child process 8692
[Wed Jun 14 21:42:51.973446 2017] [mpm_winnt:notice] [pid 8692:tid 636] AH00354: Child: Starting 64 worker threads.
[Wed Jun 14 21:59:11.710766 2017] [mpm_winnt:notice] [pid 7988:tid 696] AH00422: Parent: Received shutdown signal -- Shutting down the server.
[Wed Jun 14 21:59:13.712698 2017] [mpm_winnt:notice] [pid 8692:tid 636] AH00364: Child: All worker threads have exited.
[Wed Jun 14 21:59:13.731009 2017] [mpm_winnt:notice] [pid 7988:tid 696] AH00430: Parent: Child process exited successfully.
[Wed Jun 14 23:12:16.868389 2017] [mpm_winnt:notice] [pid 7988:tid 692] AH00455: Apache/2.4.2 (Win64) configured -- resuming normal operations
[Wed Jun 14 23:12:16.869389 2017] [mpm_winnt:notice] [pid 7988:tid 692] AH00456: Server built: May 11 2012 20:42:30
[Wed Jun 14 23:12:16.869389 2017] [core:notice] [pid 7988:tid 692] AH00094: Command line: 'C:\\Program Files\\Apache2.4\\bin\\httpd.exe -d C:/Program Files/Apache2.4 -f C:\\Program Files\\Apache2.4\\conf\\httpd.conf -d C:\\Program Files\\Apache2.4\\.'
[Wed Jun 14 23:12:16.870391 2017] [mpm_winnt:notice] [pid 7988:tid 692] AH00418: Parent: Created child process 7152
[Wed Jun 14 23:12:17.107601 2017] [mpm_winnt:notice] [pid 7152:tid 616] AH00354: Child: Starting 64 worker threads.
[Wed Jun 14 23:12:24.939591 2017] [mpm_winnt:notice] [pid 7988:tid 692] AH00422: Parent: Received shutdown signal -- Shutting down the server.
[Wed Jun 14 23:12:26.941397 2017] [mpm_winnt:notice] [pid 7152:tid 616] AH00364: Child: All worker threads have exited.
[Wed Jun 14 23:12:26.960081 2017] [mpm_winnt:notice] [pid 7988:tid 692] AH00430: Parent: Child process exited successfully.
wsgi.py:
import os, sys
sys.path.append('C:/Program Files/Apache2.4/htdocs/RMS/RMS')
sys.path.append('C:/Program Files/Apache2.4/htdocs/RMS')
os.environ.setdefault["DJANGO_SETTINGS_MODULE"] = "RMS.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandlers()
httpd-vhosts.conf:
<VirtualHost *:8080>
WSGIScriptAlias /rms 'C:/Program Files/Apache2.4/htdocs/RMS/RMS/wsgi.py'
Alias /media/ 'C:/Program Files/Apache2.4/htdocs/RMS/media'
Alias /media/ 'C:/Program Files/Apache2.4/htdocs/RMS/media'
<Directory "C:/Program Files/Apache2.4/htdocs/RMS/static">
<Files wsgi.py>
Order allow, deny
Allow from all
</Files>
</Directory>
<Directory "C:/Program Files/Apache2.4/htdocs/RMS/media">
Order allow, deny
Allow from all
</Directory>
</VirtualHost>
httpd.conf:
Listen 8080
LoadModule wsgi_module modules/mod_wsgi.so
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
【问题讨论】:
你能把你的conf文件和wsgi文件的内容包含进去吗? 您好!我用我所做的所有设置更新我的帖子。我需要说httpd.conf
文件太大了。我只添加我编辑的部分。你能说什么?也许我使用了错误的mod_wsgi
文件?
我在这个网站上使用mod_wsgi
:lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi你能给我一些建议吗?
在 error.log 文件中我注意到这一点:httpd.exe: Syntax error on line 171 of C:/Program Files/Apache2.4/conf/httpd.conf: Cannot load C:/Program Files/Apache2.4/modules/mod_wsgi.so into server: \xcd\xe5 \xed\xe0\xe9\xe4\xe5\xed \xf3\xea\xe0\xe7\xe0\xed\xed\xfb\xe9 \xec\xee\xe4\xf3\xeb\xfc.
我加载的 mod_wsgi 似乎有问题。
你使用谁的 Apache 发行版?仅推荐安装 Windows 是使用 ApacheLounge 发行版,然后使用 pip install mod_wsgi
安装方法,然后运行 mod_wsgi-express module-config
以显示您需要添加到 Apache 配置以加载 mod_wsgi 模块的配置。见pypi.python.org/pypi/mod_wsgi
【参考方案1】:
Graham Dumpleton 提到的方法对我有用,我很幸运地遇到了这个项目:modwgsi Google Group 再次由 Graham 提供。
-
使用 pip 安装
mod_wsgi
(最好安装到 virtualenv 中)
运行 mod_wsgi-express module-config
将设置复制到您的 httpd.conf
文件中,我选择将这些复制到 LoadModules 列表上方。
不要移动和重命名.pyd
文件,不要管它。要进行测试,请创建一个 helloworld.wsgi,其内容如下:
def application(environ, start_response):
status = '200 OK'
output = b'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
在您的 VirtualHost 中,创建一个 WSGIScriptAlias 指向所创建的 wsgi 文件,例如WSGIScriptAlias /test C:/wsgitest/helloworld.wsgi
【讨论】:
以上是关于如何在 Apache 中使用 Django?的主要内容,如果未能解决你的问题,请参考以下文章
Django & Couchdb:如何在没有 Apache 服务器的情况下进行部署
我如何在 ubuntu 的 apache 网络服务器上运行 Django 和 phpmyadmin
Django:如何在生产中的apache服务器上部署静态文件
[django]django 在apache2上部署静态文件如何加载