使用Dreamhost上的virtualenv更新新的Django和Python 2.7。*(带乘客)

时间:2012-06-08 17:52:23

标签: python django passenger dreamhost

Dreamhost是小型项目的绝佳主机。它也是Django友好的托管。除了python和Django版本之外的一切都有点过时了。好吧,这是一整天的工作,想弄清楚如何在dreamhost上更新Python 2.7.3,Django 1.4,我真的想与找到它的人分享

2 个答案:

答案 0 :(得分:48)

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

  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
    
  2. 配置系统以使用我们的新Python。打开〜/ .bashrc并添加以下行

     export PATH="$HOME/Python27/bin:$PATH"
     export LD_LIBRARY_PATH=$HOME/Python27/lib
    
     #save it and run
     source ~/.bashrc
    

    您现在可以使用which python

  3. 检查您的python版本
  4. 安装easy_installpip

    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
    
  5. 安装virtualenv

     pip install virtualenv
     virtualenv $HOME/<site>/env
     #Switch to virtualenv
     source $HOME/<site>/env/bin/activate
    

    您还可以将env路径添加到bashrc

     export PATH="$HOME/<site>/env/bin/:$PATH"
     source ~/.bashrc
    
  6. 安装django及其他所有内容

     pip install django
     pip install ....
     pip install ....
     pip install ....
    
  7. 创建项目

     cd $HOME/<site>/
     python $HOME/<site>/env/bin/django-admin.py startproject project
    
  8. 使用以下内容在passenger_wsgi.py中创建HOME/<site>/

     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()
    
  9. 或者这样

    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()
    
    1. 享受:D

    2. 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'
      ))
      

答案 1 :(得分:1)

目前Dreamhost已将服务器更新为Ubuntu 12.04,我遇到了错误:

Import Error: <path-to-python>/_io.so undefined symbol: PyUnicodeUCS2_Decode

编译自定义python并运行“python ez_setup.py”

之后

解决方案是在步骤1使用--enable-unicode = ucs4编译python

./configure --enable-shared --prefix=$HOME/Python27 --enable-unicode=ucs4