我查看过很多相关帖子,但似乎没有任何帮助 相关信息:
Django版本 - 1.4
Apache版本 - 2.2
Python版本 - 2.7
OS - Xubuntu 12.04
DB - Mysql
我正在尝试让Apache同时提供django app和静态文件。管理站点中的问题变得明显,无法显示任何CSS样式或图像。我的管理网站目前看起来像:
(好吧,我会包含一张图片,但是堆栈溢出并不让我。我只想说它看起来像是关于这个主题的所有其他人的管理页面,请参阅Apache not serving django admin static files )
像我的登录页面和一些动态内容这样的应用程序工作正常,但是当我尝试提供静态内容时,我收到403错误。此外,当我尝试通过查看管理页面的渲染html并单击指向样式表的链接时手动导航到样式表
http://localhost/static/admin/css/base.css
我收到403错误。我可以在终端中导航,并更改了文件夹的权限,以便Apache的www-data用户显式访问所有文件。
以下是我的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&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 /
非常感谢任何帮助,如果您需要更多文件/信息,请与我们联系。
答案 0 :(得分:10)
我也遇到过这个问题。
我无法将静态文件放在/var/www
中作为 Noah 建议。
通过添加到apache2.conf
来解决:
<Directory /usr/local/django_apps/myapp/static>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
答案 1 :(得分:3)
另一种可能性是静态根目录中缺少尾部斜杠。以下配置导致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员工。
答案 2 :(得分:1)
我明白了。我将静态和媒体文件的所有文件移动到/ var / www和apache似乎更快乐