Django + Apache + Virtualenv + WSGI(无法更改主要解释器)

时间:2014-08-06 06:03:05

标签: python django apache virtualenv wsgi

我有使用python 2.7.3的virtualenv和任何网站包。 在这个服务器上 - python 2.6.6。我需要python 2.7.3作为主要解释器

wsgi.py文件:

import os
import sys
import site


# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/aaodegov/virtual_envs/basic_env_2/lib/python2.7/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/mts_report/mts_report_project')
sys.path.append('/var/www/mts_report/mts_report_project/mts_report_project')

os.environ['DJANGO_SETTINGS_MODULE'] = 'mts_report_project.settings.local'

# Activate your virtual env
activate_env="/home/aaodegov/virtual_envs/basic_env_2/bin/activate_this.py"
execfile(activate_env, dict(__file__=activate_env))

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

apache config:

<VirtualHost *:80>
        ServerAdmin mail
        ServerName server_name
        ServerAlias alias_name
        DocumentRoot /var/www/mts_report/mts_report_project
        WSGIDaemonProcess mts_report processes=1 maximum-requests=1 threads=1 python-path=/home/aaodegov/virtual_envs/basic_env_2/lib/python2.7:/home/aaodegov/virtual_envs/basic_env_2/lib/python2.7/site-packages
        WSGIProcessGroup mts_report
        WSGIScriptAlias / var/www/mts_report/mts_report_project/wsgi.py

        Alias /static/ /var/www/mts_report/mts_report_project/static/
        Alias /home/aaodegov/virtual_envs/basic_env_2/lib/python2.7/site-packages/admin/media/
        <Directory /var/www/mts_report/mts_report_project>
            Options +Indexes
            allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/servicescripts_error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/servicescripts_access.log combined
        <Location "/static/">
            Options -Indexes
        </Location>
</VirtualHost>

检查python版本(在调试django模式下):

Python Executable:  /usr/bin/python
Python Version: 2.6.6
Python Path:    
['/home/aaodegov/virtual_envs/basic_env_2/lib/python2.7',
 '/home/aaodegov/virtual_envs/basic_env_2/lib/python2.7/site-packages',
 '/usr/local/lib/python2.6/dist-packages/pip-1.5.6-py2.6.egg',
 '/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages',
 '/usr/lib/pymodules/python2.6',
 '/var/www/mts_report/mts_report_project',
 '/var/www/mts_report/mts_report_project/mts_report_project']

因此,Django使用来自virtualenv的网站包,但主要的解释器来自系统。 如何使用virtualenv的解释器(2.7.3)? THX。

2 个答案:

答案 0 :(得分:2)

昨天问题解决了。 对遇到类似问题的人有用。 mod_vsgi未针对python 2.7编译。 谢谢大家的答案。

首先,安装apache2-threaded-dev 然后,使用--enable-shared 编译PYTHON。这很重要。

./configure --enable-shared --prefix=/usr/local/python-2.7.3
make
make install
ln -s /usr/local/python-2.7.3/bin/python2.7 /usr/bin/python2.7.3

未找到libpython2.7.so.1.0 问题:

ln -s /usr/local/python-2.7.3/lib/libpython2.7.so.1.0 /usr/lib/
ln -s /usr/local/python-2.7.3/lib/libpython2.7.so /usr/

使用python2.7.3编译MOD_WSGI

./configure --with-python=/usr/bin/python2.7.3
make
make install

重启apache:

service apache2 restart

答案 1 :(得分:-1)

在apache config中添加开头

WSGIPythonHome PATH_TO_YOUR_2.7.3_PYTHON

<VirtualHost *:80>
...

请注意,您可能还需要添加类似

的内容
WSGISocketPrefix ../../var/run/wsgi

同时更改

python-path=/home/aaodegov/virtual_envs/basic_env_2/lib/python2.7:/home/aaodegov/virtual_envs/basic_env_2/lib/python2.7/site-packages

python-path=/home/aaodegov/virtual_envs/basic_env_2/lib/python2.7/site-packages

并且您不需要在wsgi.py中引用virtualenv