2012-06-08 61 views

回答

48

我目前有私人服務器,一個shell帳戶和一點運氣。因此,這裏是我做的:

  1. SSH到你的主機升級蟒蛇

    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版本安裝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 
    
  4. 安裝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 
    
  5. 安裝Django和其他一切

    pip install django 
    pip install .... 
    pip install .... 
    pip install .... 
    
  6. 創建項目

    cd $HOME/<site>/ 
    python $HOME/<site>/env/bin/django-admin.py startproject project 
    
  7. HOME/<site>/創建passenger_wsgi.py有以下內容

    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上蟒新版本將不再返回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

    尼斯指導,感謝分享。 –

    +4

    這將是巨大的,如果有人能寫自動化腳本 –

    +0

    我們(0,'$ HOME//env/project') 正在獲取導入您的passenger_wsgi.py文件時發生錯誤,即使在/ project後面也沒有創建任何項目目錄) –

    1

    目前Dreamhost的更新服務器到Ubuntu 12.04,和我有一個錯誤:

    Import Error: <path-to-python>/_io.so undefined symbol: PyUnicodeUCS2_Decode 
    
    編譯自定義python並運行「python ez_setup」後編譯爲

    。PY」

    的解決方案是編譯--enable-unicode的= UCS4蟒蛇在步驟1

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

    謝謝。我應該更新我的答案 –

    +1

    *不知道*也應該說,當前默認的python版本在「快樂託管」== 2.7.3,但Django仍舊舊,所以我使用你的步驟來創建virtualenv。它工作正常!謝謝! –

    相關問題