【发布时间】:2017-08-07 10:49:37
【问题描述】:
在我的 apache.conf 文件中(参见下面的代码°),VirtualHost 端口 80 配置工作正常。但是,在端口 443 中,Alias /admin/media/ /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/ 显示了两个问题:
- 我的 settings.py 有:
STATIC_URL = '/m/'和ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/' - 我的管理目录是:
/home/user/project/virtual-environment/lib/python2.7/site-packages/django/contrib/admin/static/admin/
当我输入Alias /m/admin/ /home/user/project/virtual-environment/lib/python2.7/site-packages/django/contrib/admin/static/admin/ 显示错误 403 访问被禁止。
当我添加时:
<Directory "/home/user/project/virtual-environment/lib/python2.7/site-packages/django/contrib/admin/static/admin/">
Require all granted
</Directory>
<Directory "/home/user/project/">
<Files django.wsgi>
Require all granted
</Files>
</Directory>
它显示错误 404 未找到,error_log 说:[wsgi:error] Target WSGI script '/home/user/project/django.wsgi' does not contain WSGI application 'application'
您能帮我配置我的 apache 虚拟主机端口 443 到服务器 django admin 应用吗?
°我的 apache.conf 文件如下:
#The following two directories must be both readable and writable by apache
WSGISocketPrefix /var/run/apache2/wsgi
#WSGIPythonEggs /var/python/eggs
# the following directory must be readable by apache
WSGIPythonHome /home/user/project/virtual-environment/local/
# NOTE: all urs below will need to be adjusted if
# settings.FORUM_SCRIPT_ALIAS is anything other than empty string (e.g. = 'forum/')
# this allows "rooting" forum at http://domain-name/forum, if you like
#replace default ip with real IP address
<VirtualHost *:80>
ServerAdmin you@domain-name
DocumentRoot /home/user/project/
ServerName domain-name
# aliases to serve static media directly
Alias /m/ /home/user/project/static/
Alias /upfiles/ /home/user/project/askbot/upfiles/
<DirectoryMatch "/home/user/project/askbot/skins/([^/]+)/media">
Require all granted
</DirectoryMatch>
<Directory "/home/user/project/askbot/upfiles">
Require all granted
</Directory>
<Directory "/home/user/project/ask-skins">
Require all granted
</Directory>
<Directory "/home/user/project//static">
Require all granted
</Directory>
#must be a distinct name within your apache configuration
WSGIDaemonProcess askbot2 python-path=/home/user/project:/home/user/project/virtua-environment/lib/python2.7/site-packages
WSGIProcessGroup askbot2
WSGIScriptAlias / /home/user/project//django.wsgi
<Directory "/home/user/project/">
<Files django.wsgi>
Require all granted
</Files>
</Directory>
# make all admin stuff except media go through secure connection
<LocationMatch "/admin(?!/media)">
RewriteEngine on
RewriteRule /admin(.*)$ https://domain-name/admin$1 [L,R=301]
</LocationMatch>
CustomLog /var/log/apache2/domain-name/access_log common
ErrorLog /var/log/apache2/domain-name/error_log
LogLevel debug
</VirtualHost>
#again, replace the IP address
<VirtualHost *:443>
ServerAdmin you@domain-name
DocumentRoot /home/user/project/
ServerName domain-name
<LocationMatch "^(?!/admin)">
RewriteEngine on
RewriteRule django.wsgi(.*)$ http://domain-name$1 [L,R=301]
</LocationMatch>
SSLEngine on
#your SSL keys
SSLCertificateFile /etc/httpd/ssl.crt/server.crt
SSLCertificateKeyFile /etc/httpd/ssl.key/server.key
Alias /admin/media/ /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/
WSGIScriptAlias / /home/user/project/django.wsgi
CustomLog /var/log/httpd/askbot/access_log common
ErrorLog /var/log/httpd/askbot/error_log
</VirtualHost>
我的 django.wsgi 文件如下:
import os
import sys
import time
import traceback
import signal
current_directory = os.path.dirname(__file__)
parent_directory = os.path.dirname(current_directory)
module_name = os.path.basename(current_directory)
sys.path.append(parent_directory)
sys.path.append(current_directory)
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % module_name
from django.core.wsgi import get_wsgi_application
try:
application = get_wsgi_application()
print 'WSGI without exception'
except Exception:
print 'handling WSGI exception'
# Error loading applications
if 'mod_wsgi' in sys.modules:
traceback.print_exc()
os.kill(os.getpid(), signal.SIGINT)
time.sleep(2.5)
【问题讨论】:
-
您是否有理由不定义
STATIC_ROOT并运行python manage.py collectstatic将所有静态文件收集到一个位置,然后使用Alias引用它。您通常不会从 Python 安装目录中引用静态文件。见docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/… -
关于管理静态文件,我已经检查过它们位于“/home/user/project/static/admin/”但是当我执行“Alias /m/admin /home/user/项目/静态/管理员”它们没有被渲染。请指教
-
@GrahamDumpleton: 我一直在测试 ""Alias /m/admin /home/user/project/static/admin" 但管理应用程序继续显示而没有格式化。我应该为这个问题?
标签: django python-2.7 django-admin mod-wsgi askbot