簡體   English   中英

Apache不提供django管理靜態文件

[英]Apache not serving django admin static files

讓我感謝Stack Overflow社區的各位幫助我解決各種Django和Apache(帶有mod_wsgi)錯誤。 到目前為止,我已經詢問了5個相關的問題,現在我越來越接近在生產網站上獲取我的內容了!

所以,我知道有這個很多類似的問題,我已經讀了一堆 問題, 關於 服務 的靜態 媒體 文件 的Django

我讀了STATIC_URLSTATIC_ROOT ,(很快就會過時) ADMIN_MEDIA_PREFIX ,並在Apache配置中設置了Alias /media/ ... 我試圖逐個測試每個解決方案,但我無法正常工作。

這是我的管理網站現在的樣子

我也有一個奇怪的情況, 任何子域在我的服務器上工作。 例如,我試圖設置我的服務器,以便http://www.satoshi.example.com/允許我的正常(非Django)內容,而http://django.satoshi.example.com/將允許我的Django內容將被提供。 但是目前任何子域名,無論是satoshi.example.com還是blahblahasdas.satoshi.example.com都在為我的Django文件提供服務(我知道因為我可以訪問兩個站點上的/admin頁面,盡管它們將在不同的會話中)。

無論如何這里是我的服務器上的文件運行CentOS (不確定是哪個版本), Apache 2.2.15Python 2.6.6django 1.3.1mod_wsgi 3.2

我將發布我認為最相關的文件和配置如下:

每次重新啟動時,Apache都會拋出這些錯誤

[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

這是/var/www/html/mysite/apache/apache_django_wsgi.conf ,它使用選項NameVirtualHost *:80加載到我的httpd.conf 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>

這是/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()

最后這里是/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',
)

如果你們需要任何其他文件,請告訴我。 提前致謝!

我認為你應該改變:

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

至:

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

因為你有:

ADMIN_MEDIA_PREFIX = '/static/admin/'

那是因為你沒有設置你的STATIC文件......

添加到設置:

STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/static/'

然后運行“python manage.py collectstatic”

這將把所有文件放在STATIC_ROOT下STATIC_URL將服務...你不應該將Apache指向你的Python lib文件!

如果您還需要自己的應用程序特定的靜態文件,請設置“STATICFILES_DIRS”。

我得到了解決方案,我查看了/ var / log / httpd /中的access_log文件

127.0.0.1 - - [28/Dec/2013:14:49:20 -0500] "GET /static/admin/css/login.css HTTP/1.1" 200 836 "http://127.0.0.1/admin/" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 CentOS/3.6.24-3.el6.centos Firefox/3.6.24"

所以我在/etc/httpd/conf/httpd.conf文件中添加了以下標簽,

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

<VirtualHost 127.0.0.1:80>標簽內

然后我重新啟動服務使用

service httpd restart

它的工作原理!

以下為我做了訣竅。 (Django 1.11 with Python 3.6)

Alias /static/admin /usr/local/lib/python3.6/site-packages/django/contrib/admin/static/admin
<Directory "/usr/local/lib/python3.6/site-packages/django/contrib/admin/static/admin">
    Require all granted
    Order allow,deny
    Allow from all
   # AllowOverride All
</Directory>

Alias /static /var/www/app/static

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM