在我的apache.conf文件中(参见以下代码),VirtualHost端口80配置工作正常。但是,在端口443中,Alias /admin/media/ /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/显示了两个问题:
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管理应用程序?
我的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)发布于 2017-03-16 20:20:22
您的配置有许多错误或次优。
第一种情况是,当使用mod_wsgi守护进程模式并且只有一个应用程序时,建议您强制使用主要的Python解释器上下文。这避免了一些第三方Python模块无法与Python子解释器一起工作的问题。要做到这一点,请添加:
WSGIApplicationGroup %{GLOBAL}对于这两个VirtualHost定义。
第二个原因是,您的SSL没有委托WSGI应用程序在非SSL VirtualHost定义的同一个mod_wsgi守护进程组中运行。需要添加到SSL VirtualHost中。
WSGIProcessGroup askbot2不需要将WSGIDaemonProcess指令添加到SSL VirtualHost中,因为它是为了避免应用程序的多个副本而对非SSL VirtualHost中的设置进行备份。现在,您有一个更大的问题,因为SSL变体在嵌入式模式下运行,使用的场景甚至更不理想。
第三,在设置Python虚拟环境时,应该使用python-home选项来引用的根,而不是使用python-path来引用site-packages。有关这方面的详细情况,见:
第四个问题是如何处理Django初始化失败。除了创建WSGI应用程序对象之外,不需要使用try/。如果使用mod_wsgi守护进程模式,您应该做的是使用现代mod_wsgi版本(不太可能是您的OS包提供的旧版本),并在WSGIDaemonProcess指令中设置startup-timeout选项。如果WSGI脚本文件在一定时间内无法加载,该选项将自动强制重新启动进程。有关该选项的详细信息,请参阅:
如果您不打算这样做,那么您至少需要向除了部分添加一个raise,以便将异常传播回mod_wsgi,以便它知道无法加载WSGI脚本文件。如果没有,mod_wsgi认为WSGI脚本文件加载得很好,但是当它查找application时却找不到它,从而生成一个404响应和您看到的错误。
https://stackoverflow.com/questions/42831092
复制相似问题