在Dreamhost上使用virtualenv更新新的Django和Python 2.7*(with passenger)

问题描述

我目前有私人服务器,一个外壳帐户和一点运气。所以这是我的工作:

  1. SSH到你的主机以升级python
 cd ~
 mkdir tmp
 cd tmp
 wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
 tar zxvf Python-2.7.3.tgz
 cd Python-2.7.3
 ./configure --enable-shared --prefix=$HOME/Python27 --enable-unicode=ucs4
 make
 make install
  1. 配置系统以使用我们的新Python。打开〜/ .bashrc并添加以下行
 export PATH="$HOME/Python27/bin:$PATH"
 export LD_LIBRARY_PATH=$HOME/Python27/lib

 #save it and run
 source ~/.bashrc

你现在可以使用检查你的python版本 which python

  1. 安装easy_install,pip
cd ~/tmp
wget http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py
easy_install pip
# Or even shorter
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
  1. 安装 virtualenv
 pip install virtualenv
 virtualenv $HOME/<site>/env
 #Switch to virtualenv
 source $HOME/<site>/env/bin/activate
  1. 你还可以将env路径添加到 bashrc
 export PATH="$HOME/<site>/env/bin/:$PATH"
 source ~/.bashrc
  1. 安装django和其他所有东西
 pip install django
 pip install ....
 pip install ....
 pip install ....
  1. 建立专案
 cd $HOME/<site>/
 python $HOME/<site>/env/bin/django-admin.py startproject project
  1. 创建passenger_wsgi.py于HOME//与下列内容
 import sys, os
 cwd = os.getcwd()
 sys.path.append(cwd)
 sys.path.append(cwd + '/project')  #You must add your project here or 500

 #Switch to new python
 #You may try to replace $HOME with your actual path
 if sys.version < "2.7.3": os.execl("$HOME/<site>/env/bin/python",
     "python2.7.3", *sys.argv)

 sys.path.insert(0,'$HOME/<site>/env/bin')
 sys.path.insert(0,'$HOME/<site>/env/lib/python2.7/site-packages/django')
 sys.path.insert(0,'$HOME/<site>/env/lib/python2.7/site-packages')

 os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
 import django.core.handlers.wsgi
 application = django.core.handlers.wsgi.WsgiHandler()

或者这样

import sys, os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

sys.path.append(os.path.join(BASE_DIR))  #You must add your project here or 500

#Switch to new python
#You may try to replace $HOME with your actual path
PYTHON_PATH = os.path.join(BASE_DIR, 'env', 'bin', 'python')
if sys.executable != PYTHON_PATH:
    os.execl(PYTHON_PATH, "python2.7.12", *sys.argv)

如果你使用的是django 1.7,请将最后两行替换为

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

享受:D Dreamhost上的新版本python将不再返回,sys.executable因此你这是我的passenger_wsgi版本

import sys, os

VIRTUAL_ENV_PYTHON = 'venv-python'  # Python > 2.7.6 dreamhost not return sys.executable
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

def is_venv_python():
    if len(sys.argv) > 0:
        last_item = sys.argv[len(sys.argv)-1]
        if last_item == VIRTUAL_ENV_PYTHON:
            return True
    return False

sys.path.append(os.path.join(BASE_DIR))  #You must add your project here or 500

#Switch to new python

PYTHON_PATH = os.path.join(BASE_DIR, 'env', 'bin', 'python')
if not is_venv_python():
    os.execl(PYTHON_PATH, "python2.7.12", *sys.argv + [VIRTUAL_ENV_PYTHON])

sys.path.insert(0, os.path.join(BASE_DIR, 'env', 'bin'))
sys.path.insert(0, os.path.join(
    BASE_DIR, 'env', 'lib', 'python2.7', 'site-packages'
))

解决方法

Dreamhost是小型项目的理想主机。而且它也是Django友好的托管。除python和Django版本外,其他所有功能都过时了。好吧,这是一整天的工作,要弄清楚如何在dreamhost上更新Python 2.7.3,Django 1.4,我真的想与任何发现它的人分享