阿帕奇403而服务Django的静态文件 [英] Apache 403 while serving Django static files

查看:133
本文介绍了阿帕奇403而服务Django的静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了很多相关的帖子看,但似乎没有要帮忙。结果
相关信息:

I've looked through a lot of the related posts but nothing seems to be helping.
Relevant Info:

Django的版本 - 1.4

Django version - 1.4

Apache的版本 - 2.2

Apache version - 2.2

Python版本 - 2.7

Python version - 2.7

OS - Xubuntu的12.04

OS - Xubuntu 12.04

DB - Mysql的

DB - Mysql

我试图让阿帕奇同时服务于Django应用程序和静态文件。这个问题成为管理网站,无法显示任何的CSS样式或图片的明显。我管理的网站目前是这样的:

I'm trying to get Apache to serve both the django app and static files. The issue become apparent in the admin site which fails to display any of the CSS styles or images. My admin site currently looks like:

(好吧,我会包含一个图像,但堆栈溢出没有让我。我只想说,它看起来像谁是张贴在这个问题每个人的管理页面,请参阅Apache不提供Django管理静态文件

(well, I would have included an image but stack overflow didn't let me. Suffice to say it looks like the admin page of everyone else who's posted on this topic, see Apache not serving django admin static files )

应用作品就像我的登录页面和一些动态内容的工作就好了,但是当我尝试提供静态内容,我得到一个403错误。此外,当我试图通过查看管理页面呈现的HTML和点击的链接样式上

Application pieces like my login page and some dynamic content work just fine, but when I try to serve static content, I get a 403 error. Additionally when I try to navigate to the stylesheet manually by looking at the admin page's rendered html and clicking on the link to the stylesheet at

http://localhost/static/admin/css/base.css 

我得到一个403错误。我可以在一个终端导向那里,并且改变了权限的文件夹,以便Apache的www数据的用户明确地访问所有文件。

I get a 403 error. I can navigate there in a terminal, and changed the permissions for the folder so that Apache's www-data user explicitly has access to all the files.

下面是我的httpd.conf中的相关部分:

Here are the relevant pieces of my httpd.conf:

#AliasMatch ^/([^/]*\.css) /usr/local/wsgi/static/styles/$1

Alias /media/ "/usr/local/wsgi/media/"
Alias /static/ "/usr/local/wsgi/static/"

<Directory "/usr/local/wsgi/static">
Order deny,allow
Allow from all
</Directory>

<Directory "/usr/local/wsgi/media">
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / "/home/noah/Documents/Web/Basic/apache/django.wsgi"

<Directory "/usr/local/wsgi/scripts">
Order allow,deny
Allow from all
</Directory>

在一个朋友的建议,我也复制上面我的网站可用的默认值:

On the advice of a friend I also copied the above to my sites-available default:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    TypesConfig /etc/mime.types
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /home/noah/Documents/Web/Basic/apache/ >
        Options -Indexes FollowSymLinks
            AllowOverride AuthConfig FileInfo
            Order allow,deny
            Allow from all
        </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
    SetEnv DJANGO_SETTINGS_MODULE Basic.settings
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

#AliasMatch ^/([^/]*\.css) /usr/local/wsgi/static/styles/$1

Alias /media "/usr/local/wsgi/media/"
Alias /static "/usr/local/wsgi/static/"

<Directory "/usr/local/wsgi/static">
Order deny,allow
Allow from all
</Directory>

<Directory "/usr/local/wsgi/media">
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / "/home/noah/Documents/Web/Basic/apache/django.wsgi"

<Directory "/usr/local/wsgi/scripts">
Order allow,deny
Allow from all
</Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

下面是我的django.wsgi

Here is my django.wsgi

import os
import sys

path = '/home/noah/Documents/Web/Basic'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'Basic.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

最后,这里是我的settings.py:

And finally, here is my settings.py:

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = '/usr/local/wsgi/media/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = 'http://localhost/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/usr/local/wsgi/static/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = 'http://localhost/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'bmc&amp;epl=#u)r3elkvj#@90*cji*z^cg8dnh$7j9kh@g9wzw(ih'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'Basic.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'Basic.wsgi.application'

我的Django项目基本生活在〜/文档/网络/它被链接到/ var / WWW /

My Django project 'Basic' lives in ~/Documents/Web/ which is symlinked to /var/www/

任何帮助是极大AP preciated,让我知道如果你需要任何文件/资料。

Any help is greatly appreciated, and let me know if you need any more files/information.

推荐答案

我面对这个问题为好。

I faced this issue as well.

我不能在 /无功/网络静态文件放在为诺亚的建议。

i can't place the static files in /var/www as Noah suggested.

通过向 apache2.conf 解决:

<Directory /usr/local/django_apps/myapp/static>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

这篇关于阿帕奇403而服务Django的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆