我有一个 django 应用程序,可以在 django 开发服务器下完美运行。现在我尝试使用 mod_wsgi 在 apache2.2 下运行它。
在apache的httpd.conf文件中我添加了:
<IfModule wsgi_module>
WSGIScriptAlias /index my_path_to_wsgi_modules/django.wsgi
</IfModule>
并且django.wsgi模块包含django文档中解释的所有基本配置:http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
不幸的是,当我运行服务器并尝试访问主页时,我获得了模板页面,但其中没有变量数据。服务器日志显示:
[Fri Feb 18 13:50:33 2011] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/api, referer: http://127.0.0.1/example/
正如我之前所说,奇怪的是相同的代码在 django 开发服务器下完美运行。我是网络应用程序编程新手,请问有人可以帮忙吗?
我的 django.wsgi 文件如下所示:
导入操作系统
导入系统
从 os.path 导入 sep
基本路径 = '/home/example/WorkSpace/examplews/src'
sys.path.append(基本路径)
sys.path.append('/home/example/WorkSpace/examplews/src/examplews')
os.environ['DJANGO_SETTINGS_MODULE'] = 'examplews.settings'
导入 django.core.handlers.wsgi
应用程序 = django.core.handlers.wsgi.WSGIHandler()
和我的 httpd.con 文件,如下所示:
服务器根“/usr/local/apache2”
听80
LoadModule wsgi_module 模块/mod_wsgi.so
用户 apache
组阿帕奇
ServerAdmin [电子邮件受保护]
DocumentRoot“/usr/local/apache2/htdocs”
<目录/>
选项 FollowSymLinks
允许覆盖无
订单拒绝、允许
全部否认
<目录“/usr/local/apache2/htdocs”>
期权索引 FollowSymLinks
不允许覆盖无
订单允许、拒绝
允许所有人
DirectoryIndex index.html index.php index.sh default.jsp
订单允许、拒绝
全部否认
满足大家
错误日志“日志/error_log”
日志级别警告
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" 组合
LogFormat "%h %l %u %t \"%r\" %>s %b" 常见
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O"组合
CustomLog“logs/access_log”常见
WSGIScriptAlias /main /home/example/WorkSpace/examplews/src/examplews/apache_conf/django.wsgi
<目录“/home/example/WorkSpace/examplews/src/examplews/apache_conf”>
订单允许、拒绝
允许所有
默认类型文本/纯文本
类型配置conf/mime.types
AddType 应用程序/x-compress .Z
AddType 应用程序/x-gzip .gz .tgz
内置 SSLRandomSeed 启动
SSLRandomSeed 内置连接
I've a django app that works perfectly under the django development server. Now I'm trying to run it under apache2.2 using the mod_wsgi.
In the httpd.conf file of apache I've added:
<IfModule wsgi_module>
WSGIScriptAlias /index my_path_to_wsgi_modules/django.wsgi
</IfModule>
and the django.wsgi module contains all the basic configuration as explained in django documentation: http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
Unfortunately when I run the server and try to access the main page I obtain the template page but without variable data in it. The server log says:
[Fri Feb 18 13:50:33 2011] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/api, referer: http://127.0.0.1/example/
As I said before the strange thing is that the same code works perfectly under django development server. I'm new in programming web app, please, can anybody help?
My django.wsgi file looks like this:
import os
import sys
from os.path import sep
basepath = '/home/example/WorkSpace/examplews/src'
sys.path.append(basepath)
sys.path.append('/home/example/WorkSpace/examplews/src/examplews')
os.environ['DJANGO_SETTINGS_MODULE'] = 'examplews.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
And my httpd.con file, like this:
ServerRoot "/usr/local/apache2"
Listen 80
LoadModule wsgi_module modules/mod_wsgi.so
User apache
Group apache
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache2/htdocs"
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
<Directory "/usr/local/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<IfModule dir_module>
DirectoryIndex index.html index.php index.sh default.jsp
</IfModule>
<FilesMatch "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" common
</IfModule>
<IfModule alias_module>
</IfModule>
<IfModule cgid_module>
</IfModule>
<IfModule wsgi_module>
WSGIScriptAlias /main /home/example/WorkSpace/examplews/src/examplews/apache_conf/django.wsgi
</IfModule>
<Directory "/home/example/WorkSpace/examplews/src/examplews/apache_conf">
Order allow,deny
Allow from all
</Directory>
DefaultType text/plain
<IfModule mime_module>
TypesConfig conf/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
</IfModule>
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
发布评论
评论(1)
最后我发现了错误。它没有连接到 httpd.conf 文件,而是连接到如何将 URL 指定给 django urls.py 文件和模板。
当我以这种方式安装 myapp 时:
我相信 django 模板文件中指定的 URL 应该带有初始斜杠,如下所示:
'/api/dir'
这样一来,应用程序只能在 django 开发服务器上运行,而不能在 apache 上运行。
相反,如果您使用不带开头斜杠的 URL,例如:
'api/dir'
该应用程序在 django 开发服务器和 apache 上都能正常工作!
即使在 django urls.py 文件的模式匹配中,也必须避免使用起始斜杠:
像这样:
(r'^api/dir$', 'available_services')
而不是这样的:
(r'^/api/dir$', 'available_services')
也许这对于 django 专家用户来说是显而易见的事情,但如果你是像这样的新手对我来说,这可能会让您损失一定的时间,因为这是一个很难检测到的问题。
Finally I found the error. It was not connected to httpd.conf file but to how URLs are specified both to django urls.py file and to templates.
As I mounted myapp in this way:
I was believing that URLs specified in django template files should carry an initial slash, like this:
'/api/dir'
It results that in this way the application works only on django development server but not on apache.
Instead if you use URLs without initial slash like:
'api/dir'
The app works correctly both on django development server and on apache!
You must avoid using starting slashes even on pattern matching of django urls.py file:
like this:
(r'^api/dir$', 'available_services')
and NOT like this:
(r'^/api/dir$', 'available_services')
Maybe this is an obvious thing for expert django users but if you're novice like me, this can make you loose a certain amount of time because it's a hard problem to be detected.