Apache 403,同时提供Django静态文件

Apache 403,同时提供Django静态文件,django,apache,static,django-admin,mod-wsgi,Django,Apache,Static,Django Admin,Mod Wsgi,我浏览了很多相关的帖子,但似乎没有任何帮助。 相关信息: Django版本-1.4 Apache版本-2.2 Python版本-2.7 OS-Xubuntu 12.04 DB-Mysql 我试图让Apache同时提供django应用程序和静态文件。这个问题在管理站点中变得很明显,该站点无法显示任何CSS样式或图像。我的管理站点当前看起来像: (好吧,我本来会包括一个图像,但堆栈溢出不允许我。可以说,它看起来像是发布此主题的其他所有人的管理页面,请参阅 ) 像我的登录页面和一些动态内容这样的应用程

我浏览了很多相关的帖子,但似乎没有任何帮助。
相关信息:

Django版本-1.4

Apache版本-2.2

Python版本-2.7

OS-Xubuntu 12.04

DB-Mysql

我试图让Apache同时提供django应用程序和静态文件。这个问题在管理站点中变得很明显,该站点无法显示任何CSS样式或图像。我的管理站点当前看起来像:

(好吧,我本来会包括一个图像,但堆栈溢出不允许我。可以说,它看起来像是发布此主题的其他所有人的管理页面,请参阅 )

像我的登录页面和一些动态内容这样的应用程序工作得很好,但是当我尝试提供静态内容时,我得到一个403错误。此外,当我试图手动导航到样式表时,请查看管理页面的呈现html并单击指向以下位置的样式表链接:

http://localhost/static/admin/css/base.css 
我得到一个403错误。我可以在终端中导航到那里,并更改文件夹的权限,以便Apache的www数据用户可以显式访问所有文件

以下是我的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>
最后,这里是我的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项目“Basic”位于~/Documents/Web/中,它与/var/www/符号链接


非常感谢您的帮助,如果您还需要任何文件/信息,请告诉我。

我已经解决了。我将所有文件(静态文件和媒体文件)移动到/var/www,apache似乎更高兴了

我也遇到了这个问题

我不能像诺亚建议的那样将静态文件放在
/var/www

通过添加到
apache2.conf
解决:

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

选项索引跟随符号链接
不允许超限
要求所有授权

另一种可能是静态根目录中缺少尾部斜杠。以下配置导致403个错误:

WSGIPythonPath /home/foo/app/
WSGIPythonHome /home/foo/.envs/foo

<VirtualHost *:80>
    Alias /static/ /home/foo/assets
    ServerAdmin foo@bar.com
    WSGIScriptAlias / /home/foo/app/wsgi.py
    ErrorLog ${APACHE_LOG_DIR}/app_wsgi_error.log
    CustomLog ${APACHE_LOG_DIR}/app_wsgi_access.log combined
    <Directory /home/foo/app/>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    <Directory /home/foo/assets>
        Require all granted
    </Directory>
</VirtualHost>
WSGIPythonPath/home/foo/app/
WSGIPythonHome/home/foo/.envs/foo
别名/static//home/foo/assets
服务器管理员foo@bar.com
WSGIScriptAlias//home/foo/app/wsgi.py
ErrorLog${APACHE\u LOG\u DIR}/app\u wsgi\u error.LOG
CustomLog${APACHE\u LOG\u DIR}/app\u wsgi\u access.LOG组合
要求所有授权
要求所有授权

/home/foo/assets
中添加了一个尾部斜杠之后,一切都很顺利。我希望这能对未来的谷歌有所帮助。

可能重复的不同之处在于django版本具有不同的功能,而那里的解决方案对我来说根本不起作用。我将把检查转移到您的解决方案上,因为民主我想知道为什么需要在
apache.conf
中设置,并且在vhosts文件中是不够的……我同意,在自定义“站点可用”conf文件中设置的最佳实践我在站点可用conf文件中设置,解决了我的问题
<Directory /usr/local/django_apps/myapp/static>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
WSGIPythonPath /home/foo/app/
WSGIPythonHome /home/foo/.envs/foo

<VirtualHost *:80>
    Alias /static/ /home/foo/assets
    ServerAdmin foo@bar.com
    WSGIScriptAlias / /home/foo/app/wsgi.py
    ErrorLog ${APACHE_LOG_DIR}/app_wsgi_error.log
    CustomLog ${APACHE_LOG_DIR}/app_wsgi_access.log combined
    <Directory /home/foo/app/>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    <Directory /home/foo/assets>
        Require all granted
    </Directory>
</VirtualHost>