【Python】Nginx+uwsgi+Django部署程式碼

小亮520cl發表於2017-02-22

Nginx+uwsgi+Django部署程式碼

安裝uwsgi

1.安裝pip

可以參考這篇文章:


2.安裝uwsgi

pip install uwsgi

3 測試uwsgi

  1. 在你的機器上寫一個test.py

  2. # test.py
  3. def application(env, start_response):
  4.     start_response('200 OK', [('Content-Type','text/html')])
  5.     return "Hello World"

然後執行shell命令:

uwsgi --http :8001 --wsgi-file test.py

訪問網頁:

看在網頁上是否有Hello World


4.編寫uwsgi啟動配置檔案

  1. [root@host-192-168-1-56 devops]# more my.ini
  2. [uwsgi]
  3. socket=:8001
  4. chdir=/svn/devops/devops                     ###專案目錄
  5. pythonapth=/usr/bin/python
  6. processes=4
  7. env=DJANGO_SETTINGS_MODULE=devops.settings   ###指定 專案名.setting
  8. module=devops.wsgi                           ###指定wsgi.py配置檔案,該配置檔案和setting在一個目錄下
  9. threads=0
  10. master=true
  11. daemonize=/tmp/uwsgi.log
  12. pidfile=/tmp/uwsgi.pid
  13. python-autoreload=true
  14. buffer-size=51200

5.編寫wsgi配置檔案

  1. [root@host-192-168-1-56 devops]# more devops/wsgi.py
  2. """
  3. WSGI config for devops project.

  4. It exposes the WSGI callable as a module-level variable named ``application``.

  5. For more information on this file, see

  6. """

  7. import os

  8. from django.core.wsgi import get_wsgi_application

  9. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "devops.settings")

  10. application = get_wsgi_application()

6.配置nginx
  1. 配置檔案
  2. [root@host-192-168-1-56 devops]# more /usr/local/nginx/conf/vhost/blog.haodai.com.conf 
    upstream django {
        server 127.0.0.1:8001; # for a web port socket (we'll use this first)
    }


    server {
        listen      8099;
        server_name devops.XXX.net;
        charset     utf-8;
        root /svn/devops/devops;
        client_max_body_size 75M;   # adjust to taste




        location /static {
            alias /svn/devops/devops/static;            ---nginx呼叫專案的靜態檔案
            expires 20d;
        }


        location / {
            uwsgi_pass  django;                        ---反向代理,簡單寫法直接寫成uwsgi_pass  127.0.0.1:8001即可
            include uwsgi_params;
        }


    access_log /home/wwwlogs/devops.log access;
    }


7 啟動,測試
  1. 啟動uwsgi和nginx
  2. [root@host-192-168-1-56 devops]# /usr/local/python/bin/uwsgi my.ini
  3. [root@host-192-168-1-56 devops]# nginx -s reload
  4. 測試:


參考連結:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2134101/,如需轉載,請註明出處,否則將追究法律責任。

相關文章