學習VirtualEnv和Nginx+uwsgi用於django項目部署


以下敘述中用到的操作系統:Linux CentOS 6.X。

最近幾天了解一下VirtualEnv,Apache+Daemon mode,Nginx+uwsgi的概念,並且在項目中實驗性部署了一下(目前我們的Django項目都使用Apache+mod_wsgi部署的,聽說使用Nginx+uwsgi效率更高一些)。以下都是事后記錄的安裝部署過程,可能存在遺忘細節的地方。

  1. VirtualEnv

VirtualEnv的作用:創建隔離的Python環境,解決模塊或庫的版本沖突或依賴。

在實際開發過程中,不同項目可能使用不同版本的庫,例如,我們使用的Web框架Django,老項目使用的老的版本1.2.3,新項目使用的是1.4或1.5版本。那時我使用了最笨辦法,即修改Django目錄以切換,幸好沒有使用太多多版本庫,不然累死。

    • 安裝步驟(網上能找到很多安裝VirtualEnv的URL,參考http://www.cnblogs.com/kym/archive/2011/12/29/2306428.html):
      • easy_install virtualenv #安裝
      • virtualenv <myvenv>  #創建虛擬環境
      • 切換到虛擬環境<myvenv>目錄運行source ./bin/activate #激活虛擬環境
      • 可以在虛擬環境安裝使用包,例如安裝Django:easy_install Django
      • 離開虛擬環境,使用命令:deactivate
    • 應用虛擬環境

   目前我們使用Apache+mod_wsgi部署Django項目的,項目可以通過以下方式使用虛擬環境:

      WSGI作為Apache子進程運行

      在httpd.conf中,可以VirtualHost上一行添加:WSGIPythonPath <myvenv path>/lib/python2.7/site-packages/.

                 WSGI作為獨立daemon進程運行(參考http://modwsgi.readthedocs.org/en/latest/configuration-directives/WSGIDaemonProcess.html

          在WSGIDaemonProcess指令后添加:python-path=<myvenv path>/lib/python2.7/site-packages。

          BTW:這種方式部署的時候,需要在httpd.conf中添加WSGISocketPrefix <full path>,否則web請求可能會503失敗 (參考http://modwsgi.readthedocs.org/en/latest/configuration-directives/WSGISocketPrefix.html

    1. nginx && uwsgi

      nginx可以作為WEB Server,反向代理,負載均衡等服務。

      

      • 安裝參考

          nginx:http://wiki.nginx.org/Install,我選擇的源碼安裝,安裝后沒有自啟動的服務(網上有方法為nginx創建服務

          uwsgi:http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html#installing-uwsgi-with-python-support

      • 為Django項目的配置

    uwsgi的配置<project path>/uwsgi_XXX.ini如下(使用的是unix socke file方式進程通訊,在nginx也需要配相同路徑文件)。注:如果要使用VirtualEnv虛擬環境,請設置home為虛擬環境路徑。

    # uwsgi_XXX.ini file
    [uwsgi]
    
    # Django-related settings
    # the base directory (full path)
    chdir           = /var/www/<project path>
    # Django's wsgi file
    module          = <project name>.wsgi
    # the virtualenv (full path)
    home = <virtualenv path>
    
    uid=apache
    gid=apache
    
    # process-related settings
    # master
    master          = true
    # maximum number of worker processes
    processes       = 10
    # the socket (use the full path to be safe
    socket = /var/www/<project path>/<project name>.sock
    # ... with appropriate permissions - may be needed
    # chmod-socket    = 664
    # clear environment on exit
    vacuum          = true
    
    daemonize = /var/log/uwsgi.log
    stats = 0.0.0.0:9191
    
    pidfile=/var/www/<project path>/uwsgi.pid

    另外需要在<project path>/<project name>/目錄下創建文件wsgi.py。注:請記住添加PYTHON_EGG_CACHE,不然會報權限錯誤。

    import os
    os.environ['PYTHON_EGG_CACHE'] = '/<a path>/.python-eggs/' #為了防止Permission denied的web請求訪問錯誤。
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "maps.settings")
    
    # This application object is used by any WSGI server configured to use this
    # file. This includes Django's development server, if the WSGI_APPLICATION
    # setting points here.
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()

    操作uwsgi進程(切換到<project path>目錄下):

    • 啟動:uwsgi --ini uwsgi_XXX.ini
    • 停止:uwsgi --stop uwsgi.pid
    • reload: uwsgi --reload uwsgi.pid

    #------------------------------------------------------------------------------------

    nginx的配置如下(由於uwsgi配置使用unix socket file進程通訊的)

    user  apache; # 指定用戶
    worker_processes  2;#啟動2進程
    
    pid        logs/nginx.pid; #指定pid文件
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        keepalive_timeout  65;
    
        #gzip  on;
        upstream django {
        server unix:///var/www/<項目path>/XXX.sock; # for a file socket
        #server 127.0.0.1:8001; # for a web port socket (we'll use this first)
        }
    
        server {
            listen       8050;# listen port
            server_name  localhost;
    
            charset utf-8;
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    location /media  {
            alias /var/www/<項目path>/media;
        }
    location / {
            uwsgi_pass  django;
            include     uwsgi_params; # the uwsgi_params file you installed
        }

    操作nginx進程:

    • 啟動:/usr/local/nginx/sbin/nginx
    • 停止:/usr/local/nginx/sbin/nginx -s stop

     

    That's all.

     


    免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM