6

我搜索了很多,但我的 django 网站的静态文件(css、图像、...)仍然存在问题。

我在archlinux 64位上使用mod_wsgi和apache

我已将其添加到我的 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 /)(奇怪的是,如果我使用这里给出的测试脚本它会起作用)

所有目录和内容(apache 文件夹、wsgi-script、martfiles)都具有775 root:devusersdevusers 组的权限,包括我的用户、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 ''

/etc/httpd/conf/http.conf

/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 文件夹中

4

3 回答 3

7

仅仅包含静态文件的目录“/home/mart/programmation/python/django/martfiles/media”是可读和可搜索的是不够的。运行 Apache 的用户必须具有读取和潜在搜索访问权限,对它的所有父目录备份到根目录。由于许多系统上的主目录是 'rwx-----' 这将拒绝 Apache 访问,而与 Apache 配置中的拒绝/允许指令无关。

建议您将 Django 项目和静态文件放在您的家庭帐户之外的某个地方,并根据需要放宽文件系统权限。

于 2010-01-17T04:55:18.167 回答
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 是否真的可以访问它们...

于 2010-01-16T22:54:46.170 回答
0

这似乎是我的应用程序所拥有的,只是我没有NameVirtualHost在 http.conf 中看到如果你想设置虚拟服务器是必需的指令。您可以尝试NameVirtualHost *:80在虚拟主机定义之前添加。

于 2010-01-16T22:14:57.023 回答