首页 文章

django中带有mod_wsgi的静态文件

提问于
浏览
6

我搜索了很多,但我的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 与组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

谢谢


edit :我确定我的django网站工作正常(除了会话但是我没有't think it'相关)所以我'm not sure it'与django.wsgi文件相关(也许我错了)但是可以肯定的是我应该能够从apache文件夹外部使用django.wsgi

如果我用 Alias /media /srv/http/media/ 更改行 Alias /media /home/mart/programmation/python/django/martfiles/media/ 并给出正确的权限,它可以工作 . 但是我不想将我的所有媒体都放在apache文件夹中

3 回答

  • 7

    仅包含静态文件的目录'/ home / mart / programmation / python / django / martfiles / media'是可读和可搜索的 . Apache运行的用户必须具有读取和潜在搜索访问权限,并将其备份到根目录的所有父目录 . 由于许多系统上的主目录都是'rwx ------',因此无论Apache配置中的Deny / Allow指令如何,都会拒绝Apache访问 .

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

  • 0

    你的 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是否真的可以访问他们......

  • 3

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

相关问题