django 中带有 mod_wsgi 的静态文件

发布于 2024-08-18 06:20:19 字数 2069 浏览 5 评论 0原文

我已经搜索了很多,但我的 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 但它不起作用(访问权限被拒绝/此处给出的测试脚本,它会起作用)

code>) (奇怪的是, 目录和内容(apache文件夹,wsgi-script,martfiles)具有权限775 root:devusers,组devusers包括我的用户,

模板base.html中的http和root,我称之为css这样:

 <html>  <head>
     <link rel="stylesheet" href="/media/css/style.css" />

中的错误

 [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 ''

以及 /var/log/http/error.log /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 文件夹中

I've searched a lot but I still have a problem with the static files (css, image,...) with my django website.

I'm using mod_wsgi with apache on archlinux 64bits

I've added it in my 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>

I tried to use the django.wsgi in my home folder but it doesn't work (permission denied to access /) (strangely it works if I use the test script given here)

all the directories and content (apache folder, wsgi-script, martfiles) have the permission 775 root:devusers with the group devusers including my user, http and root

in my template base.html, I call the css this way :

 <html>  <head>
     <link rel="stylesheet" href="/media/css/style.css" />

and the error in /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

thank you


edit : I precise that my django website is working fine (except the sessions but I don't think it's related) so I'm not sure it's related to the django.wsgi file (maybe I'm wrong) but what is sure is that I should be able to use the django.wsgi from outside the apache folder

if I change the line Alias /media /home/mart/programmation/python/django/martfiles/media/ with Alias /media /srv/http/media/ and gives the right permissions, it works. But I don't want (and shouldn't) to put all my media in the apache folder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

誰認得朕 2024-08-25 06:20:19

仅包含静态文件的目录“/home/mart/programmation/python/django/martfiles/media”是可读和可搜索的。 Apache 运行的用户必须具有对其所有父目录(直至根目录)的读取和潜在搜索访问权限。由于许多系统上的主目录是“rwx------”,因此无论 Apache 配置中的拒绝/允许指令如何,这都会拒绝 Apache 访问。

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

It is not sufficient for just the directory '/home/mart/programmation/python/django/martfiles/media' containing static files to be readable and searchable. The user that Apache runs as must have read and potentially search access, to all parent directories of it back up to root directory. Since home directories on many systems are 'rwx------' this would deny Apache access irrespective of the Deny/Allow directives in Apache configuration.

Suggest you place the Django project and static files outside of your home account somewhere and relax the file system permissions as necessary.

无言温柔 2024-08-25 06:20:19

您的 django.wsgi 文件

WSGIScriptAlias / /srv/http/wsgi-scripts/django.wsgi

位于由以下定义的 之外:

<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 [email protected]
    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 定义在可通过 指令访问的位置,您可以还有 sudo su httpd 如果这是在您的系统上运行 apache 的用户,并且只需尝试读取 css 文件,看看 apache 是否真的可以访问它们...

Your django.wsgi file,

WSGIScriptAlias / /srv/http/wsgi-scripts/django.wsgi

is outside of the <Directory> defined by:

<Directory /home/mart/programmation/python/django/martfiles/>

Try adding this to httpd.conf:

<Directory /srv/http/wsgi-scripts/>
    Order allow,deny
    Allow from all
</Directory>

Or, put your django.wsgi file somewhere inside /home/mart/programmation/python/django/martfiles/. That should work.

EDIT: alright, here's an example httpd.conf that is working on a production machine:

<VirtualHost *:80>
    # Admin email, Server Name (domain name) and any aliases
    ServerAdmin [email protected]
    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>

So, if your dhango.wsgi is defined in a place set as accessible by a <Directory> directive, you could also sudo su httpd if that's the user that runs apache on your system, and simply try to read the css files, to see if apache can really access them...

水波映月 2024-08-25 06:20:19

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

This seems to be what I have for my application except that I didn't see the NameVirtualHost directive in the http.conf which is required if you want to setup virtual servers. You can try adding NameVirtualHost *:80 before the virtual host definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文