我搜索了很多,但我的django网站仍然存在静态文件(css,image,...)的问题。
我在archlinux 64bits上使用带有apache的mod_wsgi
我在http.conf中添加了它:
LoadModule wsgi_module modules/mod_wsgi.so
<VirtualHost *:80>
WSGIDaemonProcess mart.localhost user=mart group=users processes=2 threads=25
WSGIProcessGroup mart.localhost
LogLevel debug
Alias /media /home/mart/programmation/python/django/martfiles/media/
<Directory /home/mart/programmation/python/django/martfiles/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /srv/http/wsgi-scripts/django.wsgi
</VirtualHost>
我尝试在我的主文件夹中使用django.wsgi,但它不起作用(permission denied to access /
)(奇怪的是,如果我使用here给出的测试脚本,它会起作用)
所有目录和内容(apache文件夹,wsgi-script,martfiles)都拥有权限775 root:devusers
,包括我的用户,http和root
在我的模板base.html中,我用这种方式调用css:
<html> <head>
<link rel="stylesheet" href="/media/css/style.css" />
和/var/log/http/error.log中的错误
[Sat Jan 16 13:22:21 2010] [error] [client 127.0.0.1] (13)Permission denied: access to /media/css/style.css denied, referer: http://localhost/
[Sat Jan 16 13:22:21 2010] [info] mod_wsgi (pid=14783): Attach interpreter ''
/srv/http/wsgi-script/django.wsgi
/home/.../martfiles/settings.py
谢谢
编辑:我确定我的django网站工作正常(除了会话但我不认为它是相关的)所以我不确定它是否与django.wsgi文件有关(也许我错了)但可以肯定的是我应该可以在apache文件夹之外使用django.wsgi
如果我使用Alias /media /home/mart/programmation/python/django/martfiles/media/
更改行Alias /media /srv/http/media/
并提供正确的权限,则可以。但我不希望(也不应该)将我的所有媒体都放在apache文件夹中
答案 0 :(得分:7)
仅包含静态文件的目录'/ home / mart / programmation / python / django / martfiles / media'是可读的和可搜索的。 Apache运行的用户必须具有读取和潜在搜索访问权限,并将其备份到根目录的所有父目录。由于许多系统上的主目录都是'rwx ------',因此无论Apache配置中的拒绝/允许指令如何,都会拒绝Apache访问。
建议您将Django项目和静态文件放在家庭帐户之外,并根据需要放宽文件系统权限。
答案 1 :(得分:3)
您的django.wsgi
文件,
WSGIScriptAlias / /srv/http/wsgi-scripts/django.wsgi
超出了<Directory>
定义的:
<Directory /home/mart/programmation/python/django/martfiles/>
尝试将此添加到httpd.conf
:
<Directory /srv/http/wsgi-scripts/>
Order allow,deny
Allow from all
</Directory>
或者,将django.wsgi
文件放在/home/mart/programmation/python/django/martfiles/
内的某处。这应该有用。
编辑:好的,这是一个在生产机器上运行的httpd.conf示例:
<VirtualHost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin testing@example.de
ServerName www.example.de
DocumentRoot /home/example/testing/parts/public
Alias /media /home/example/testing/parts/public/media
# WSGI Settings
WSGIDaemonProcess example user=example group=example threads=25
WSGIProcessGroup example
WSGIScriptAlias / /home/example/testing/parts/public/django.wsgi
<Directory "/home/example/testing/parts/public">
# Allow Apache to follow links
Options FollowSymLinks
# Turn on the ability to use .htaccess files
AllowOverride All
# Controls who can get stuff from this directory
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
因此,如果你的dhango.wsgi是在<Directory>
指令可以访问的地方定义的,你也可以sudo su httpd
如果那是在你的系统上运行apache的用户,那么只需尝试阅读css文件,看看apache是否真的可以访问它们......
答案 2 :(得分:0)
这似乎是我对我的应用程序所拥有的,除了我没有在http.conf中看到NameVirtualHost
指令,如果你想设置虚拟服务器,这是必需的。您可以尝试在虚拟主机定义之前添加NameVirtualHost *:80
。