在Django中使用mod_wsgi的静态文件

6 投票
3 回答
8298 浏览
提问于 2025-04-15 18:05

我搜索了很多,但还是遇到了关于静态文件(比如css、图片等)的问题,这个问题出现在我的django网站上。

我在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脚本、martfiles)都有775 root:devusers的权限,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 ''

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

3 个回答

0

看起来我在我的应用程序中有的东西差不多,不过我在http.conf文件里没看到NameVirtualHost这个指令。如果你想设置虚拟服务器的话,这个指令是必须的。你可以试着在虚拟主机定义之前加上NameVirtualHost *:80

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>

所以,如果你的 django.wsgi 文件放在一个被 <Directory> 指令允许访问的地方,你也可以使用 sudo su httpd,如果这个用户是你系统上运行 Apache 的用户,然后简单尝试读取 CSS 文件,看看 Apache 是否真的能访问它们……

7

仅仅让包含静态文件的目录 '/home/mart/programmation/python/django/martfiles/media' 可读和可搜索是不够的。运行 Apache 的用户还必须对这个目录的所有上级目录(一直到根目录)都有读取和可能的搜索权限。因为在很多系统上,用户的主目录权限是 'rwx------',这会阻止 Apache 访问这些目录,无论 Apache 配置中的拒绝/允许指令如何设置。

建议你把 Django 项目和静态文件放到你的主账户之外的地方,并根据需要放宽文件系统的权限。

撰写回答