设置 Apache 为 django 管理文件提供服务

Setting up Apache to serve the django admin files

在我的 apache.conf 文件中(参见下面的代码°),VirtualHost 端口 80 配置工作正常。但是,在端口 443 中,Alias /admin/media/ /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/ 显示两个问题:

当我输入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 [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 [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)

您的配置有很多错误或次优配置。

首先是当使用mod_wsgi守护进程模式并且只有一个应用程序时,建议您强制使用主Python解释器上下文。这避免了某些不与 Python 子解释器一起工作的第三方 Python 模块的问题。为此,添加:

WSGIApplicationGroup %{GLOBAL}

两个 VirtualHost 定义。

第二个是您的 SSL VirtualHost 没有将 WSGI 应用程序委派给非 SSL VirtualHost 定义的 mod_wsgi 守护进程组中的 运行。您需要添加到 SSL VirtualHost:

WSGIProcessGroup askbot2

不需要向 SSL VirtualHost 添加 WSGIDaemonProcess 指令,因为它在非 SSL VirtualHost 中支持该设置以避免应用程序的多个副本。现在你有一个更大的问题,因为 SSL 变体 运行 在嵌入式模式下,更不理想的情况。

三是在设置Python虚拟环境时,应该使用python-home选项来引用Python虚拟环境的根目录,而不是使用python-path 引用 site-packages。有关详细信息,请参阅:

第四个问题是您如何处理 Django 初始化失败。无需在创建 WSGI 应用程序对象时使用 try/except。如果使用 mod_wsgi 守护进程模式,你应该做的是使用现代 mod_wsgi 版本(不太可能是你的 OS 包提供的古老版本),并设置 startup-timeout 选项WSGIDaemonProcess 指令。如果 WSGI 脚本文件未能在特定时间内加载,该选项将自动强制重启进程。有关该选项的详细信息,请参阅:

如果你不打算这样做,你至少需要在 except 部分添加一个 raise 以便将异常传播回 mod_wsgi 所以它知道 WSGI 脚本文件无法加载。如果你不这样做,mod_wsgi 认为 WSGI 脚本文件加载正常,但是当它查找 application 时找不到它,因此会生成 404 响应和你看到的错误。