Apache不提供django admin静态文件 [英] Apache not serving django admin static files

查看:160
本文介绍了Apache不提供django admin静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我感谢你们在Stack Overflow社区帮助我使用各种Django和Apache(带有mod_wsgi)的错误。到目前为止,我已经询问了5个相关问题,现在我越来越近地在生产网站上收到我的内容!



所以我知道有很多类似的问题,我已经阅读了一个将允许我的正常(非Django)内容,而 http://django.satoshi.example。 com / 将允许我的Django内容。但是,目前任何子域,无论是satoshi.example.com还是blahblahasdas.satoshi.example.com正在为我的Django文件提供服务(我知道,因为我可以去 / admin 这两个站点,虽然他们将在不同的会话)。



无论如何,这里是我的文件在服务器上运行 CentOS (不确定哪个版本), Apache 2.2.15 Python 2.6.6 django 1.3.1 mod_wsgi 3.2



我会发布我认为是最相关的文件和配置如下:



Apache每次重新启动时抛出这些错误

  [Wed Feb 29 01:45:36 2012] [error]异常KeyError:< module'threading'from'/usr/lib64/python2.6/threading.pyc'中的KeyError(140249420548064) >忽略
[Wed Feb 29 01:45:36 2012] [error]异常KeyError:< module'threading'from'/usr/lib64/python2.6/threading.pyc'>中的KeyError(140249420548064) ;忽略
[Wed Feb 29 01:45:36 2012] [error]异常KeyError:< module'threading'from'/usr/lib64/python2.6/threading.pyc'>中的KeyError(140249420548064) ;忽略
[Wed Feb 29 01:45:36 2012] [error]异常KeyError:< module'threading'from'/usr/lib64/python2.6/threading.pyc'>中的KeyError(140249420548064) ;忽略
[Wed Feb 29 01:45:36 2012] [error]异常KeyError:< module'threading'from'/usr/lib64/python2.6/threading.pyc'>中的KeyError(140249420548064) ;忽略
[Wed Feb 29 01:45:36 2012] [error]异常KeyError:< module'threading'from'/usr/lib64/python2.6/threading.pyc'>中的KeyError(140249420548064) ;忽略
[Wed Feb 29 01:45:36 2012] [error]异常KeyError:< module'threading'from'/usr/lib64/python2.6/threading.pyc'>中的KeyError(140249420548064) ;忽略
[Wed Feb 29 01:45:36 2012] [error]异常KeyError:< module'threading'from'/usr/lib64/python2.6/threading.pyc'>中的KeyError(140249420548064) ;忽略
[Wed Feb 29 01:45:36 2012] [notice] SIGHUP收到。尝试重新启动
[Wed Feb 29 00:45:36 2012] [error]异常KeyError:来自/usr/lib64/python2.6/threading.pyc的< module'threading'中的KeyError(140249420548064) >忽略
[Wed Feb 29 01:45:36 2012] [notice]摘要:为消化验证生成秘密...
[Wed Feb 29 01:45:36 2012] [notice]摘要:完成
[Wed Feb 29 01:45:36 2012] [warn] mod_wsgi:编译为Python / 2.6.2。
[Wed Feb 29 01:45:36 2012] [warn] mod_wsgi:运行时使用Python / 2.6.6。
[Wed Feb 29 01:45:36 2012] [notice] Apache / 2.2.15(Unix)mod_auth_pgsql / 2.0.3 PHP / 5.3.3 mod_ssl / 2.2.15 OpenSSL / 1.0.0-fips mod_wsgi / 3.2 Python / 2.6.6 mod_perl / 2.0.4配置Perl / v5.10.1恢复正常操作

这里是 /var/www/html/mysite/apache/apache_django_wsgi.conf ,它被加载到我的 httpd.conf 选项 NameVirtualHost *:80

 < VirtualHost *: 80 GT; 
ServerName django.satoshi.example.com
ErrorLog/ var / log / httpd / django_error_log

WSGIDaemonProcess django
WSGIProcessGroup django

Alias / media //usr/lib/python2.6/site-packages/django/contrib/admin/media
< Directory/usr/lib/python2.6/site-packages/django/的contrib /管理/媒体>
订单允许,拒绝
选项索引
允许从所有
IndexOptions FancyIndexing
< / Directory>

<目录/ var / www / html / mysite>
订单允许,拒绝
选项索引
允许从所有
IndexOptions FancyIndexing
< / Directory>

WSGIScriptAlias //var/www/html/mysite/apache/django.wsgi

< Directory/ var / www / html / mysite / apache> ;
订单拒绝,允许
允许从所有
< / Directory>
< / VirtualHost>

这里是 / var / www / html / mysite / apache / django。 wsgi

  import os 
import sys

paths = [
'/ var / www / html / mysite',
'/ var / www / html',
'/usr/lib/python2.6/site-packages/',
]

路径中的路径:
如果路径不在sys.path中:
sys.path.append(路径)

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

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

最后这里是 /var/www/html/mysite/settings.py

 #将保存用户上传文件的目录的绝对文件系统路径。 
#示例:/home/media/media.lawrence.com/media/
MEDIA_ROOT =''

#处理从MEDIA_ROOT提供的媒体的URL。确保使用
#尾部斜线。
#示例:http://media.lawrence.com/media/,http://example.com/media/
MEDIA_URL =''

#静态文件的绝对路径应该被收集到。
#不要把任何东西放在这个目录下;在应用程序的static /子目录和STATICFILES_DIRS中存储静态文件
#。
#示例:/home/media/media.lawrence.com/static/
PROJECT_ROOT = os.path.normpath(os.path.dirname(__ file__))
STATIC_ROOT = os .path.join(PROJECT_ROOT,'static')

#静态文件的URL前缀。
#示例:http://media.lawrence.com/static/
STATIC_URL ='/ static /'

#admin静态文件的URL前缀 - CSS ,JavaScript和图像。
#确保使用尾部斜线。
#示例:http://foo.com/static/admin/,/ static / admin /。
ADMIN_MEDIA_PREFIX ='/ static / admin /'

#静态文件的附加位置
STATICFILES_DIRS =(
#将字符串放在这里,如/ home / html /静态或C:/ www / django / static
#即使在Windows上也可以使用正斜杠
#不要忘记使用绝对路径,而不是相对路径


#知道如何在
#各个位置找到静态文件的finder类的列表。
STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles .finders.DefaultStorageFinder',

让我知道,如果你需要任何其他文件。感谢提前!

解决方案

我想你应该更改:

 别名/媒体//usr/lib/python2.6/site-packages/django/contrib/admin/media

to:

 别名/ static / admin // usr / lib / python2.6 / site-packages / django / contrib / admin / media

  ADMIN_MEDIA_PREFIX ='/ static / admin /'


Let me thanks you guys at the Stack Overflow community for helping me with various Django and Apache (with mod_wsgi) errors. I've asked about 5 related questions so far and now I'm getting closer and closer to getting my content out on a production site!

So I know there are many similar questions about this and I have read a bunch of questions about serving static media files on Django.

I read about STATIC_URL, STATIC_ROOT, the (soon to be obsolete) ADMIN_MEDIA_PREFIX, and setting a Alias /media/ ... in the Apache configuration. I tried to test out each solution one by one, but I couldn't get anything working.

Here is what my admin site looks like right now

I'm also having a weird case where any subdomain works on my server. For example I was trying to set up my server so that http://www.satoshi.example.com/ would allow my normal (non-Django) content, while http://django.satoshi.example.com/ would allow my Django content to be served. But currently any subdomain, whether satoshi.example.com or blahblahasdas.satoshi.example.com is serving my Django files (I know because I can go to the /admin page on both site, although they will be in different sessions).

Anyway here are my files on the server which is running CentOS (not sure which version), Apache 2.2.15, Python 2.6.6, django 1.3.1, and mod_wsgi 3.2.

I will post what I think is the most relevant files and configuration below:

Apache throws these errors everytime I restart

[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [notice] SIGHUP received.  Attempting to restart
[Wed Feb 29 00:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [notice] Digest: generating secret for digest authentication ...
[Wed Feb 29 01:45:36 2012] [notice] Digest: done
[Wed Feb 29 01:45:36 2012] [warn] mod_wsgi: Compiled for Python/2.6.2.
[Wed Feb 29 01:45:36 2012] [warn] mod_wsgi: Runtime using Python/2.6.6.
[Wed Feb 29 01:45:36 2012] [notice] Apache/2.2.15 (Unix) mod_auth_pgsql/2.0.3 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.0-fips mod_wsgi/3.2 Python/2.6.6 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations

Here is /var/www/html/mysite/apache/apache_django_wsgi.conf which gets loaded into my httpd.conf with the option NameVirtualHost *:80

<VirtualHost *:80>
    ServerName django.satoshi.example.com
    ErrorLog "/var/log/httpd/django_error_log"

    WSGIDaemonProcess django
    WSGIProcessGroup django

    Alias /media/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"
    <Directory "/usr/lib/python2.6/site-packages/django/contrib/admin/media">
        Order allow,deny
        Options Indexes
        Allow from all
        IndexOptions FancyIndexing
    </Directory>

    <Directory "/var/www/html/mysite">
        Order allow,deny
        Options Indexes
        Allow from all
        IndexOptions FancyIndexing
    </Directory>

    WSGIScriptAlias / "/var/www/html/mysite/apache/django.wsgi"

    <Directory "/var/www/html/mysite/apache">
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

Here is /var/www/html/mysite/apache/django.wsgi

import os
import sys

paths = [
    '/var/www/html/mysite',
    '/var/www/html',
    '/usr/lib/python2.6/site-packages/',
]

for path in paths:
    if path not in sys.path:
        sys.path.append(path)

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

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

And finally here is part of /var/www/html/mysite/settings.py

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

# 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 = ''

# 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/"
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

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

# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'

# 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',
)

Let me know if you guys need any other files. Thanks in advance!

解决方案

I think you should change:

Alias /media/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"

to:

Alias /static/admin/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"

Because you have:

ADMIN_MEDIA_PREFIX = '/static/admin/'

这篇关于Apache不提供django admin静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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