使用WSGI

问题描述

我正在尝试将旧的Django应用程序(1.7)部署到Debian上的Apache2服务器。我终于可以在./manage.py runserver上运行该应用程序,但是在使mod-wsgi正常工作方面没有成功。我从来没有用Python编写过服务器端应用程序,所以我可能会遗漏一些明显的东西。

这些是我遵循的步骤

  1. 我尝试过的一切都是使用/usr/bin/python,它是2.7-我知道,我应该使用virtuelenv,但是我不想给整个事情增加更多的复杂性(因为我不熟悉)
  2. mod-wsgi安装了apt-get install libapache2-mod-wsgi
  3. 将以下配置添加到/etc/apache2/mods-enabled/wsgi.conf
  4. 重新启动Apache2
<IfModule mod_wsgi.c>
    WsgiScriptAlias / /home/rosasystem.pl/public_html/rosasystem/rosasystem/wsgi.py
    WsgiPythonHome /usr/bin/python
    WsgiPythonPath /home/rosasystem.pl/public_html/rosasystem
    LogLevel warn

    <Directory /home/rosasystem.pl/public_html/rosasystem/rosasystem>
        <Files wsgi.py>
            Order deny,allow
            Require all granted
        </Files>
    </Directory>
</IfModule>

此后,我一直在error.log中收到此错误消息-我搜索过的大多数内容都表明mod-wsgi与系统/ virtualenv Python版本之间存在Python版本差异。这没有任何意义,因为所有内容都是Python 2.7。

[Tue Oct 06 15:13:42.946626 2020] [mpm_event:notice] [pid 17059:tid 139874785088640] AH00489: Apache/2.4.38 (Debian) mod_fcgid/2.3.9 OpenSSL/1.1.1g mod_wsgi/4.6.5 Python/2.7 configured -- resuming normal operations
[Tue Oct 06 15:13:42.946650 2020] [core:notice] [pid 17059:tid 139874785088640] AH00094: Command line: '/usr/sbin/apache2'
[Tue Oct 06 15:13:42.951277 2020] [wsgi:warn] [pid 17061:tid 139874785088640] mod_wsgi (pid=17061): Python home /usr/bin/python is not a directory. Python interpreter may not be able to be initialized correctly. Verify the supplied path.
[Tue Oct 06 15:13:42.951531 2020] [wsgi:warn] [pid 17062:tid 139874785088640] mod_wsgi (pid=17062): Python home /usr/bin/python is not a directory. Python interpreter may not be able to be initialized correctly. Verify the supplied path.
ImportError: No module named site

在这一点上,我很困。我可以尝试什么新东西?

解决方法

site-packages路径应添加到WSGIPythonPath,仅获取适用于您系统的正确路径

<IfModule mod_wsgi.c>
    WSGIScriptAlias / /home/rosasystem.pl/public_html/rosasystem/rosasystem/wsgi.py
    WSGIPythonHome /usr/bin/python
    WSGIPythonPath '/home/rosasystem.pl/public_html/rosasystem;/usr/lib/python2.7/dist-packages'
    LogLevel warn

    <Directory /home/rosasystem.pl/public_html/rosasystem/rosasystem>
        <Files wsgi.py>
            Order deny,allow
            Require all granted
        </Files>
    </Directory>
</IfModule>