【问题标题】:Unable to stop apache using system version of python 2.7.6 instead of virtualenv version无法使用系统版本的 python 2.7.6 而不是 virtualenv 版本来停止 apache
【发布时间】:2016-08-21 03:32:43
【问题描述】:

我已经重新编译了python2.7.11,使用:

./configure
make
make altinstall
sudo ln -s /usr/local/bin/python2.7 /usr/local/bin/python
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo /usr/local/bin/python
sudo /usr/local/bin/easy_install pip
sudo pip install virtualenv
sudo pip install virtualenvwrapper
source /usr/local/bin/virtualenvwrapper.sh

我已经安装了 django 和所有依赖项,检查 pip 是否位于我的新 python 版本中

我卸载了 mod_wsgi 的打包版本并 我已通过以下方式将 mod_wsgi 链接到新版本的 python:

./configure --with-python=/usr/local/bin/python
make install

当我重新启动 apache 时,2.7.6 和 2.7.11 似乎都已启动:

[Wed Apr 27 01:46:21.085703 2016] [wsgi:warn] [pid 18410:tid 140116611360640] mod_wsgi: Compiled for Python/2.7.11.
[Wed Apr 27 01:46:21.085775 2016] [wsgi:warn] [pid 18410:tid 140116611360640] mod_wsgi: Runtime using Python/2.7.6.
[Wed Apr 27 01:46:21.086641 2016] [mpm_worker:notice] [pid 18410:tid 140116611360640] AH00292: Apache/2.4.7 (Ubuntu) OpenSSL/1.0.1f mod_wsgi/4.5.2 Python/2.7.6 configured -- resuming normal operations

我的 httpd.conf 看起来像:

WSGIPythonHome /home/vagrant/.virtualenvs/django_2.7.11_env
<VirtualHost *:80>
  ServerName server.dev
  ServerAlias www.server.dev

  WSGIDaemonProcess server.dev python-path=/var/www/app:/home/vagrant/.virtualenvs/django_2.7.11_env/lib/python2.7/site-packages
  WSGIProcessGroup server.dev

  ## Vhost docroot
  DocumentRoot "/var/www/app"
  Alias "/sitemap.xml" "/var/www/app/static/sitemap.xml"
  <Directory /var/www/app/static>
      Require all granted
  </Directory>


    <Directory /var/www/wsgi-scripts>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

  ## Logging
  ErrorLog "/var/log/apache2/av_app_nma_api_error.log"
  ServerSignature Off
  CustomLog "/var/log/apache2/av_app_nma_api_access.log" combined

  WSGIScriptAlias / /var/www/wsgi-scripts/wsgi.py
</VirtualHost>

我能够运行“hello world”wsgi 应用程序,即使似乎使用了两个版本的 python。但是,当我使用 django 应用程序时,我最终会遇到一个似乎与多个版本相关的 ImportError。

[Wed Apr 27 02:34:42.956988 2016] [wsgi:info] [pid 20244:tid 140656494913280] mod_wsgi (pid=20244): Adding '/home/vagrant/.virtualenvs/django_2.7.11_env/lib/python2.7/site-packages' to path.
[Wed Apr 27 02:34:42.957356 2016] [wsgi:info] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070] mod_wsgi (pid=20244, process='server.dev', application='server.dev|'): Loading WSGI script '/var/www/wsgi-scripts/wsgi.py'.
[Wed Apr 27 02:34:42.995218 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070] mod_wsgi (pid=20244): Target WSGI script '/var/www/wsgi-scripts/wsgi.py' cannot be loaded as Python module.
[Wed Apr 27 02:34:42.995328 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070] mod_wsgi (pid=20244): Exception occurred processing WSGI script '/var/www/wsgi-scripts/wsgi.py'.
[Wed Apr 27 02:34:42.995376 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070] Traceback (most recent call last):
[Wed Apr 27 02:34:42.995419 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]   File "/var/www/wsgi-scripts/wsgi.py", line 18, in <module>
[Wed Apr 27 02:34:42.995945 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]     from django.core.wsgi import get_wsgi_application
[Wed Apr 27 02:34:42.995998 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]   File "/home/vagrant/.virtualenvs/django_2.7.11_env/lib/python2.7/site-packages/django/core/wsgi.py", line 2, in <module>
[Wed Apr 27 02:34:42.996068 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]     from django.core.handlers.wsgi import WSGIHandler
[Wed Apr 27 02:34:42.996109 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]   File "/home/vagrant/.virtualenvs/django_2.7.11_env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 10, in <module>
[Wed Apr 27 02:34:42.996209 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]     from django import http
[Wed Apr 27 02:34:42.996249 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]   File "/home/vagrant/.virtualenvs/django_2.7.11_env/lib/python2.7/site-packages/django/http/__init__.py", line 2, in <module>
[Wed Apr 27 02:34:42.996316 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]     from django.http.request import (
[Wed Apr 27 02:34:42.996355 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]   File "/home/vagrant/.virtualenvs/django_2.7.11_env/lib/python2.7/site-packages/django/http/request.py", line 10, in <module>
[Wed Apr 27 02:34:42.996499 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]     from django.core import signing
[Wed Apr 27 02:34:42.996560 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]   File "/home/vagrant/.virtualenvs/django_2.7.11_env/lib/python2.7/site-packages/django/core/signing.py", line 48, in <module>
[Wed Apr 27 02:34:42.996663 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]     from django.utils.crypto import constant_time_compare, salted_hmac
[Wed Apr 27 02:34:42.996705 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]   File "/home/vagrant/.virtualenvs/django_2.7.11_env/lib/python2.7/site-packages/django/utils/crypto.py", line 8, in <module>
[Wed Apr 27 02:34:42.996792 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]     import hmac
[Wed Apr 27 02:34:42.996830 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]   File "/usr/local/lib/python2.7/hmac.py", line 8, in <module>
[Wed Apr 27 02:34:42.997049 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070]     from operator import _compare_digest as compare_digest
[Wed Apr 27 02:34:42.997102 2016] [wsgi:error] [pid 20244:tid 140656494913280] [remote 192.168.56.1:1070] ImportError: cannot import name _compare_digest

回复@Graham:make 命令失败并显示以下内容:

/usr/bin/ld: /usr/local/lib/libpython2.7.a(abstract.o): relocation R_X86_64_32S against `_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libpython2.7.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
apxs:Error: Command failed with rc=65536
.
make: *** [src/server/mod_wsgi.la] Error 1

【问题讨论】:

    标签: linux apache python-2.7 ubuntu mod-wsgi


    【解决方案1】:

    试试:

    make distclean
    ./configure --with-python=/usr/local/bin/python
    LD_RUN_PATH=/usr/local/lib make
    sudo make install
    

    LD_RUN_PATH 环境变量在构建时将包含正确库的目录路径嵌入到mod_wsgi.so 文件的查找路径中。

    【讨论】:

    • make 命令失败。我已将错误添加到问题中
    • 您的 Python 安装未正确安装并且缺少共享库。请参阅 modwsgi.readthedocs.org/en/develop/user-guides/… 并重新安装 Python 安装,这次使用 Python 的 configure 的 --enable-shared 选项。另请阅读blog.dscpl.com.au/2015/06/…,其中提供了有关在安装 Python 以匹配标准配置时如何正确配置 Python 的一般建议。
    • 请注意,我现在可以运行了,非常感谢...但是当我使用 --enabled-shared 编译 python 时,没有加载 2.7.11 库,即 /usr/local/bin/ python -V 给出标准 2.6.6。您在博客中描述的方法(首先使用 --enable-shared 编译,然后在没有 --enable-shared 的情况下进行 altbininstall 解决了这个问题。这可能是两者都安装在发行版中的原因吗?还是我在做其他事情错了吗?
    • 安装 Python 时可能还需要设置 LD_RUN_PATH。尽管我认为它应该将其作为其构建脚本的一部分自己完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2010-12-04
    • 2014-12-04
    相关资源
    最近更新 更多