在Dreamhost(带乘客)上使用virtualenv更新新的Django和Python 2.7。* [英] Update new Django and Python 2.7.* with virtualenv on Dreamhost (with passenger)

查看:112
本文介绍了在Dreamhost(带乘客)上使用virtualenv更新新的Django和Python 2.7。*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dreamhost是小项目的好主人。这也是Django友好主办。一切都很好,除了python和Django版本是有点过时了。好吧,这是一整天的工作,找出如何更新Python 2.7.3,Django 1.4在dreamhost,我真的想分享谁找到谁

Dreamhost is a great host for small project. And it's also Django friendly hosting. Everything good except python and Django version is a little bit out of date. Well it's a whole day of work to figure out how to update Python 2.7.3, Django 1.4 on dreamhost and I really want to share with whoever finding it

推荐答案

我目前拥有私人服务器,一个shell帐号和一些运气。所以这里是我做的:

I currently have private server, a shell account and a bit of luck. So here is what I do:


  1. SSH到您的主机升级python

  1. SSH to your host to upgrade 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


  • 配置系统以使用我们的新Python。打开〜/ .bashrc并添加以下行

  • Configure system to use our new Python. Open ~/.bashrc and add the following line

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

    您现在可以使用检查您的python版本,其中python

    安装 easy_install pip

    cd ~/tmp
    wget http://peak.telecommunity.com/dist/ez_setup.py
    python ez_setup.py
    easy_install pip
    


  • 安装 virtualenv

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

    您还可以添加env路径到 bashrc

    you can also add env path to bashrc

     export PATH="$HOME/<site>/env/bin/:$PATH"
     source ~/.bashrc
    


  • 安装django和e其他东西

  • Install django and everything else

     pip install django
     pip install ....
     pip install ....
     pip install ....
    


  • 创建项目

  • Create project

     cd $HOME/<site>/
     python $HOME/<site>/env/bin/django-admin.py startproject project
    


  • 创建 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()
    


  • 或这种方式

    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,符合

    If you are using django 1.7, replace the last two line with

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




    1. 享受:D






    Dreamhost上的新版本的python将不再返回 sys.executable ,所以你这个是我的版本的passenger_wsgi


    New version of python on Dreamhost will no longer return sys.executable so you this is my version of 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(带乘客)上使用virtualenv更新新的Django和Python 2.7。*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆