Hãy để tôi cảm ơn các bạn tại cộng đồng Stack Overflow đã giúp tôi với nhiều lỗi Django và Apache khác nhau (với mod_wsgi). Tôi đã hỏi về 5 câu hỏi liên quan cho đến nay và bây giờ tôi đang ngày càng gần hơn để đưa nội dung của mình ra trên một trang web sản xuất!Apache không phục vụ các tệp tĩnh của django quản trị
Vì vậy, tôi biết có rất nhiều câu hỏi tương tự về vấn đề này và tôi đã đọc một bunchofquestionsaboutservingstaticmediafilesonDjango.
Tôi đọc khoảng STATIC_URL
, STATIC_ROOT
, (sắp hết hạn) ADMIN_MEDIA_PREFIX
và đặt Alias /media/ ...
trong cấu hình Apache. Tôi đã cố gắng kiểm tra từng giải pháp một, nhưng tôi không thể làm được gì.
Dưới đây là những gì trang web quản trị của tôi trông giống như ngay bây giờ
Tôi cũng đang gặp một trường hợp kỳ lạ nơi bất kỳ subdomain hoạt động trên máy chủ của tôi. Ví dụ: tôi đã cố gắng thiết lập máy chủ của mình để http://www.satoshi.example.com/ cho phép nội dung bình thường (không phải Django) của tôi, trong khi http://django.satoshi.example.com/ sẽ cho phép phân phát nội dung Django của tôi. Nhưng hiện tại bất kỳ tên miền phụ nào, cho dù satoshi.example.com hay blahblahasdas.satoshi.example.com đang phân phối các tệp Django của tôi (tôi biết vì tôi có thể truy cập trang /admin
trên cả hai trang web, mặc dù chúng sẽ ở các phiên khác nhau).
Dù sao đây là tác phẩm của tôi trên máy chủ mà đang chạy CentOS
(không chắc chắn phiên bản), Apache 2.2.15
, Python 2.6.6
, django 1.3.1
, và mod_wsgi 3.2
.
tôi sẽ đăng những gì tôi nghĩ là các tập tin và cấu hình phù hợp nhất dưới đây:
Apache ném các lỗi này mỗi khi khởi động lại
[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
Dưới đây là /var/www/html/mysite/apache/apache_django_wsgi.conf
mà được nạp vào httpd.conf
của tôi với các tùy chọn 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>
Đây là /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()
Và cuối cùng đây là một phần của /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',
)
Hãy cho tôi biết nếu các bạn cần bất kỳ tập tin khác. Cảm ơn trước!
Câu hỏi hay. Bạn đã hoàn thành bài tập về nhà và cung cấp nhiều thông tin để làm việc. Giữ nó lên. –
Câu hỏi hay, tôi cũng bị mất và thêm ADMIN_MEDIA_PREFIX vào tập tin cài đặt của tôi đã làm các trick. – edu222