apache django 中带有 mod_wsgi 的静态文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2078160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:38:21  来源:igfitidea点击:

static file with mod_wsgi in django

pythondjangoapachemod-wsgi

提问by Martin Trigaux

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

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

I'm using mod_wsgi with apache on archlinux 64bits

我在 archlinux 64bits 上使用 mod_wsgi 和 apache

I've added it in my http.conf :

我已经在我的 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)

我试图在我的主文件夹中使用 django.wsgi 但它不起作用 ( permission denied to access /) (奇怪的是,如果我使用这里给出的测试脚本,它会起作用)

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

所有目录和内容(apache 文件夹、wsgi-script、martfiles)都具有 devusers775 root:devusers组的权限,包括我的用户、http 和 root

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

在我的模板 base.html 中,我以这种方式调用 css:

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

and the error in /var/log/http/error.log

以及 /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

/etc/httpd/conf/http.conf

/srv/http/wsgi-script/django.wsgi

/srv/http/wsgi-script/django.wsgi

/home/.../martfiles/settings.py

/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

编辑:我确定我的 django 网站运行良好(除了会话,但我认为它不相关)所以我不确定它与 django.wsgi 文件有关(也许我错了)但可以肯定的是我应该能够从 apache 文件夹之外使用 django.wsgi

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

如果我改变行Alias /media /home/mart/programmation/python/django/martfiles/media/Alias /media /srv/http/media/并给出正确的权限,它的工作原理。但我不想(也不应该)将我所有的媒体都放在 apache 文件夹中

回答by Graham Dumpleton

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.

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

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

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

回答by Attila O.

Your django.wsgifile,

你的django.wsgi档案,

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

is outside of the <Directory>defined by:

超出以下<Directory>定义:

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

Try adding this to httpd.conf:

尝试将其添加到httpd.conf

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

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

或者,将您的django.wsgi文件放在/home/mart/programmation/python/django/martfiles/. 那应该有效。

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

编辑:好的,这是一个在生产机器上工作的 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>

So, if your dhango.wsgi is defined in a place set as accessible by a <Directory>directive, you could also sudo su httpdif 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...

所以,如果你的 dhango.wsgi 被定义在一个<Directory>指令可以访问的地方,你也可以sudo su httpd在你的系统上运行 apache 的用户,然后简单地尝试读取 css 文件,看看 apache 是否真的可以访问它们...

回答by abhaga

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

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