简体   繁体   English

设置Apache以提供Django管理文件

[英]Setting up Apache to serve the django admin files

In my apache.conf file (see code below°), VirtualHost port 80 configuration works fine. 在我的apache.conf文件中(请参见下面的代码),VirtualHost端口80的配置工作正常。 However, in the port 443, the Alias /admin/media/ /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/ shows two issues: 但是,在端口443中, Alias /admin/media/ /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/显示了两个问题:

  • my settings.py has : STATIC_URL = '/m/' and ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/' 我的settings.py具有: STATIC_URL = '/m/'ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
  • my admin directory is : /home/user/project/virtual-environment/lib/python2.7/site-packages/django/contrib/admin/static/admin/ 我的管理目录是: /home/user/project/virtual-environment/lib/python2.7/site-packages/django/contrib/admin/static/admin/

When I put Alias /m/admin/ /home/user/project/virtual-environment/lib/python2.7/site-packages/django/contrib/admin/static/admin/ it shows error 403 access forbidden. 当我将Alias /m/admin/ /home/user/project/virtual-environment/lib/python2.7/site-packages/django/contrib/admin/static/admin/放入时, 它显示错误403访问被禁止。

When I add: 当我添加:

<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>

It shows error 404 not found with error_log saying : [wsgi:error] Target WSGI script '/home/user/project/django.wsgi' does not contain WSGI application 'application' 它显示未找到错误404并显示 error_log: [wsgi:error] Target WSGI script '/home/user/project/django.wsgi' does not contain WSGI application 'application'

Could you please help me configure my apache virtualhost port 443 to server django admin app? 您能帮我将我的Apache Virtualhost端口443配置为服务器django管理应用程序吗?

°My apache.conf file is as below: °我的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>

My django.wsgi file is as below : 我的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)

You have a number of things wrong or sub optimal with your configuration. 您的配置有很多错误或不理想的地方。

The first is that when using mod_wsgi daemon mode and have only the one application, it is recommended you force use of the main Python interpreter context. 第一个是在使用mod_wsgi守护程序模式且只有一个应用程序时,建议您强制使用主Python解释器上下文。 This avoids problems with some third party Python modules that do not work with Python sub interpreters. 这样可以避免某些无法与Python子解释器一起使用的第三方Python模块出现问题。 To do this, add: 为此,请添加:

WSGIApplicationGroup %{GLOBAL}

to both VirtualHost definitions. 这两个VirtualHost定义。

The second is that your SSL VirtualHost is not delegating the WSGI application to run in the same mod_wsgi daemon process group as non SSL VirtualHost defines. 第二个原因是您的SSL VirtualHost没有委托WSGI应用程序在非SSL VirtualHost定义的同一mod_wsgi守护进程组中运行。 You need to add to the SSL VirtualHost : 您需要添加到SSL VirtualHost

WSGIProcessGroup askbot2

There is no need to add a WSGIDaemonProcess directive to the SSL VirtualHost as it is piggy backing off that setup in non SSL VirtualHost to avoid multiple copies of application. 无需向SSL VirtualHost添加WSGIDaemonProcess指令,因为它可以支持非SSL VirtualHost中的设置以避免应用程序的多个副本。 Right now you have an even bigger problem in that the SSL variant is running in embedded mode, with is even less desirable scenario. 现在,您还有一个更大的问题,那就是SSL变体以嵌入式模式运行,这是不太理想的情况。

The third is that when setting up a Python virtual environment, you should use the python-home option to refer to the root of the Python virtual environment and not use python-path to refer to site-packages . 第三点是,在设置Python虚拟环境时,应使用python-home选项引用Python虚拟环境的根,而不要使用python-path引用site-packages For details on that see: 有关详细信息,请参见:

The fourth problem is how you are dealing with Django initialisation failing. 第四个问题是您如何处理Django初始化失败的问题。 There is no need to to use the try/except around creating the WSGI application object. 创建WSGI应用程序对象时,无需使用try / except。 What you should do if using mod_wsgi daemon mode is use a modern mod_wsgi version (not likely the ancient version your OS packages provide), and set the startup-timeout option on the WSGIDaemonProcess directive. 如果使用mod_wsgi守护程序模式,您应该做的是使用现代的mod_wsgi版本(不太可能是OS软件包提供的古老版本),并在WSGIDaemonProcess指令上设置startup-timeout选项。 That option will automatically force a restart of process if WSGI script file fails to load within a certain time. 如果WSGI脚本文件在特定时间内无法加载,则该选项将自动强制重新启动进程。 For details on that option see: 有关该选项的详细信息,请参见:

If you are not going to do that, you at least need to add a raise to the except part so that the exception is propagated back to mod_wsgi so it knows the WSGI script file couldn't be loaded. 如果您不打算这样做,则至少需要在except部分上加一个raise ,以便将异常传播回mod_wsgi,以便知道无法加载WSGI脚本文件。 if you don't, mod_wsgi thinks the WSGI script file was loaded fine, but then when it looks for application can't find it and so generates a 404 response and the error your see. 如果没有,则mod_wsgi认为WSGI脚本文件已正常加载,但是在寻找application时找不到该文件,因此会生成404响应和您看到的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM