【问题标题】:Trouble getting simple Django web app working using Apache and mod_wsgi使用 Apache 和 mod_wsgi 使简单的 Django Web 应用程序无法正常工作
【发布时间】:2012-11-01 12:15:18
【问题描述】:

我一直坚持将我的 Django 项目移动到由 DreamHost 托管的生产 CentOS 服务器。安装了 Apache 2.2.23 和 Python 2.6.6。我已经安装并配置了 Django 1.4 和 mod_wsgi 3.4。当我通过键入重新启动 Apache(希望 Web 应用程序能够完美运行)时

sudo apachectl restart

我在 /usr/local/apache/logs/ 中查看 Apache 的 error_log 并看到:

[Mon Nov 12 18:05:24 2012] [info] Init: Seeding PRNG with 144 bytes of entropy
[Mon Nov 12 18:05:24 2012] [notice] SSL FIPS mode disabled
[Mon Nov 12 18:05:24 2012] [info] Loading certificate & private key of SSL-aware server
[Mon Nov 12 18:05:24 2012] [info] Init: Generating temporary RSA private keys (512/1024 bits)
[Mon Nov 12 18:05:24 2012] [info] Init: Generating temporary DH parameters (512/1024 bits)
[Mon Nov 12 18:05:24 2012] [info] Init: Initializing (virtual) servers for SSL
[Mon Nov 12 18:05:24 2012] [info] Configuring server for SSL protocol
[Mon Nov 12 18:05:24 2012] [warn] RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
[Mon Nov 12 18:05:24 2012] [info] mod_ssl/2.2.23 compiled against Server: Apache/2.2.23, Library: OpenSSL/1.0.0-fips
[Mon Nov 12 18:05:24 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/local/apache/bin/suexec)
[Mon Nov 12 18:05:24 2012] [info] Init: Seeding PRNG with 144 bytes of entropy
[Mon Nov 12 18:05:24 2012] [notice] SSL FIPS mode disabled
[Mon Nov 12 18:05:24 2012] [info] Loading certificate & private key of SSL-aware server
[Mon Nov 12 18:05:24 2012] [info] Init: Generating temporary RSA private keys (512/1024 bits)
[Mon Nov 12 18:05:24 2012] [info] Init: Generating temporary DH parameters (512/1024 bits)
[Mon Nov 12 18:05:24 2012] [info] Init: Initializing (virtual) servers for SSL
[Mon Nov 12 18:05:24 2012] [info] Configuring server for SSL protocol
[Mon Nov 12 18:05:24 2012] [warn] RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
[Mon Nov 12 18:05:24 2012] [info] mod_ssl/2.2.23 compiled against Server: Apache/2.2.23, Library: OpenSSL/1.0.0-fips
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30259): Starting process 'myproject.mysite.net' with uid=99, gid=99 and threads=1.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30259): Initializing Python.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30260): Initializing Python.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30261): Initializing Python.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30262): Initializing Python.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30263): Initializing Python.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30264): Initializing Python.
[Mon Nov 12 18:05:24 2012] [notice] Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/1.0.0-fips mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_wsgi/3.4 Python/2.6.6 configured -- resuming normal operations
[Mon Nov 12 18:05:24 2012] [info] Server built: Oct 21 2012 05:57:19
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30259): Attach interpreter ''.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30260): Attach interpreter ''.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30262): Attach interpreter ''.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30261): Attach interpreter ''.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30263): Attach interpreter ''.
[Mon Nov 12 18:05:24 2012] [info] mod_wsgi (pid=30264): Attach interpreter ''.
[Mon Nov 12 18:05:28 2012] [info] mod_wsgi (pid=30270): Initializing Python.
[Mon Nov 12 18:05:28 2012] [info] mod_wsgi (pid=30270): Attach interpreter ''.
[Mon Nov 12 18:07:50 2012] [info] mod_wsgi (pid=30311): Initializing Python.
[Mon Nov 12 18:07:50 2012] [info] mod_wsgi (pid=30311): Attach interpreter ''.
[Mon Nov 12 18:07:51 2012] [info] [client 24.143.104.168] (32)Broken pipe: core_output_filter: writing data to the network
[Mon Nov 12 18:10:44 2012] [info] [client 180.76.5.64] (104)Connection reset by peer: core_output_filter: writing data to the network
[Mon Nov 12 18:11:26 2012] [info] mod_wsgi (pid=30356): Initializing Python.
[Mon Nov 12 18:11:26 2012] [info] mod_wsgi (pid=30356): Attach interpreter ''.

我觉得这可能与 Python 路径有关。不过只是猜测。看起来 Python 一直在崩溃。

/home
  /myproject
    /public_html (server document root)
    /django_proj
      /myproject (django project root)
        settings.py
        other files and app directories for this django project
        /apache
          wsgi.py
          django.wsgi

这里是httpd.conf的相关部分:

<VirtualHost *:80>

    ServerName myproject.mysite.net
    ServerAdmin myemail@abc.com

    DocumentRoot /home/myproject/django_proj/myproject

    <Directory /home/myproject/django_proj/myproject>
    AllowOverride All
    Order allow,deny
    Allow from all
    </Directory>

    WSGIDaemonProcess myproject.mysite.net processes=1 threads=1 display-name=%{GROUP}
    WSGIProcessGroup myproject.mysite.net
    WSGIApplicationGroup %{GLOBAL}

    WSGIScriptAlias / /home/myproject/django_proj/myproject/apache/django.wsgi

    <Directory /home/myproject/django_proj/myproject/apache>
    Order allow,deny
    Allow from all
    </Directory>

</VirtualHost>

这是我的 wsgi.py:

import os

path = '/home/myproject/django_proj'
if path not in sys.path:
    sys.path.append(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

# This application object is used by the development server
# as well as any WSGI server configured to use this file.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

这是我的 django.wsgi:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

我还尝试将其托管为 URL“myproject.mysite.net”,而不仅仅是“mysite.net”。也不确定我是否做得对。

【问题讨论】:

  • 您似乎在wsgi.py 的顶部缺少import sys。如果这不是问题,您是否有任何可以粘贴的 python 日志文件?
  • 不幸的是,这并没有改变任何东西。我在哪里可以找到那些 python 日志文件?而且,为了访问 myproject.mysite.net 并查看应用程序,看起来我做的一切是否正确?

标签: django apache webserver centos mod-wsgi


【解决方案1】:

真正的问题是什么?当您提出请求时,您是否真的遇到了错误。

日志输出看起来完全正常。所有单独的进程都是因为 Python 也在 Apache 子工作进程中被初始化。设置:

WSGIRestrictEmbedded On

他们会离开,应该只看到一个进程。

有关该指令的注释,请参阅:

http://blog.dscpl.com.au/2009/11/save-on-memory-with-modwsgi-30.html

顺便说一句,使用 'processes=1 threads=1' 似乎是一个糟糕的选择。一次只能处理一个请求。

【讨论】:

  • 太棒了。我以为这是出了问题。但是,当我访问 myproject.mysite.net 时,几乎什么都没有。 mod_wsgi 没有收到请求吗?如果不是,我在哪里可以找到错误日志?
  • 另外,当我访问 myproject.mysite.net 时,我得到一个“/索引”页面,上面写着“Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/1.0.0 -fips mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_wsgi/3.4 Python/2.6.6 服务器在 feed.mynmi.net 端口 80"
  • 那么你甚至还没有启用你的 VirtualHost 定义,所以它被使用了。或者您对同一服务器/端口有多个 VirtualHost 定义,并且它正在使用另一个,因为它是首先找到的。在您的主要 Apache 配置中查看 LogLevel 到“信息”,如果正在使用该 VirtualHost,mod_wsgi 应该记录它正在启动一个守护进程组。如果没有这样的输出没有被读取,否则你有一个重复的 VirtualHost 定义,它隐藏了这个。
  • 原来存在与 VirtualHost 定义的冲突。感谢您的帮助!
猜你喜欢
  • 2013-06-19
  • 2014-02-24
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
相关资源
最近更新 更多