阿帕奇403而服务Django的静态文件(Apache 403 while serving Djan

2019-06-25 10:20发布

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

Django的版本 - 1.4

Apache的版本 - 2.2

Python版本 - 2.7

OS - Xubuntu的12.04

DB - Mysql的

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

(当然,我会包括图像,但堆栈溢出没有让我。我只想说,它看起来像其他人谁是发布关于这一主题的管理页面,看到的Apache服务不Django管理静态文件 )

应用作品就像我的登录页面和一些动态内容的工作就好了,但是当我尝试提供静态内容,我得到一个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>

在朋友的建议下我也复制上面的到我的网站,提供默认:

<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

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:

# 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 /

任何帮助是极大的赞赏,让我知道,如果你需要更多的文件/资料。

Answer 1:

我面对这个问题为好。

我不能将静态文件中/var/www 诺亚建议。

通过向解决apache2.conf

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


Answer 2:

另一种可能性是在静态的根目录中的失踪斜线。 下面的配置是导致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>

加上尾部斜杠之后/home/foo/assets ,一切都工作正常。 我希望帮助未来的Google。



Answer 3:

我想到了。 我感动了所有文件的静态和媒体文件到/ var / www和阿帕奇似乎更快乐



文章来源: Apache 403 while serving Django static files