nginx+uWSGI+django+virtualenv+supervisor部署發布web項目
Nginx (engine x) 是一個高性能的HTTP和反向代理服務,也是一個IMAP/POP3/SMTP服務。
wsgi是PythonWeb服務器網關接口(Web Server Gateway Interface),WSGI是作為Web服務器與Web應用程序或應用框架之間的一種低級別的接口,以提升可移植Web應用開發的共同點。WSGI是基於現存的[[CGI]]標準而設計的。
Django是一個開放源代碼的Web應用框架,由Python寫成。
Virtualenv指的的Python的虛擬環境
supervisor管理進程,是通過fork/exec的方式將這些被管理的進程當作supervisor的子進程來啟動,所以我們只需要將要管理進程的可執行文件的路徑添加到supervisor的配置文件中就好了。此時被管理進程被視為supervisor的子進程,若該子進程異常中斷,則父進程可以準確的獲取子進程異常中斷的信息,通過在配置文件中設置autostart=ture,可以實現對異常中斷的子進程的自動重啟。
整個請求和響應流程如下:
一、Python環境的準備
說明,系統為centos 7.3
1 安裝python3
安裝python 3.4和對應版本的pip
[root@node1 ~]# yum install python34.x86_64 python34-pip.noarch -y
2 安裝虛擬環境
[root@node1 ~]# pip3 install virtualenv
(1)創建虛擬目錄
[root@node1 ~]# virtualenv django
(2)進入虛擬目錄
[root@node1 ~]# cd django/
(3)啟用虛擬環境
[root@node1 django]# source bin/activate
可以看到目錄地下有默認的幾個文件夾
(django) [root@node1 django]# ll total 0 drwxr-xr-x 3 root root 330 Nov 5 15:00 bin drwxr-xr-x 2 root root 24 Nov 5 14:55 include drwxr-xr-x 3 root root 23 Nov 5 14:55 lib lrwxrwxrwx 1 root root 16 Nov 5 14:55 lib64 -> /root/django/lib
二、Django的安裝與項目創建以及配置
1 安裝Django,這裏使用1.11版本
(django) [root@node1 django]# pip3 install django==1.11 Collecting django==1.11 …………… Installing collected packages: pytz, django Successfully installed django-1.11 pytz-2018.7
2 創建項目和應用
(1)創建名為mysite的項目
(django) [root@node1 django]# django-admin startproject mysite
(2)創建名為app01的應用
(django) [root@node1 django]# cd mysite/
(django) [root@node1 mysite]#python3 manage.py startapp app01
(3)可以看到mysite項目下有如下目錄
(django) [root@node1 mysite]# ll total 4 drwxr-xr-x 3 root root 123 Nov 5 15:07 app01 -rwxr-xr-x 1 root root 804 Nov 5 15:04 manage.py drwxr-xr-x 3 root root 93 Nov 5 15:07 mysite
3 修改默認配置文件
(1)修改Django配置文件mysite/settings.py
ALLOWED_HOSTS = [‘*‘] 註冊app01 INSTALLED_APPS = [ ‘app01.apps.App01Config‘, .... ] 靜態文件目錄 STATIC_URL = ‘/static/‘ STATICFILES_DIRS=[ os.path.join(BASE_DIR,"static"), ]
(2)修改路由匹配規則mysite/urls.py
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^index/‘, views.index), ]
(3)新建功能,修改app01/views.py
from django.shortcuts import HttpResponse def index(request): return HttpResponse(‘Hello World!‘)
4 啟動Django
(django) [root@node1 mysite]# python3 manage.py runserver Performing system checks... ……. Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
測試訪問
[root@node1 ~]# curl http://127.0.0.1:8000/index/ Hello World!
三、uwsgi的安裝與配置
1 由於使用pip3 install uwsgi 安裝會報我不能解決的錯誤
在ubuntu上不會有這個錯誤
Command "/usr/bin/python3.4 -u -c "import setuptools, tokenize; __file__=‘/tmp/pip-install-k0ebn5sl/uwsgi/setup.py‘; f=getattr(tokenize, ‘open‘, open)(__file__); code=f.read().replace(‘\r\n‘, ‘\n‘); f.close(); exec(compile(code, __file__, ‘exec‘))" \ install --record /tmp/pip-record-eekd779j/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-k0ebn5sl/uwsgi/
故采用以下安裝方式(僅限centos):
[root@node1 ~]# yum install uwsgi 查看版本 [root@node1 ~]# uwsgi --version 2.0.17.1
2 安裝uwsgi-plugin-python3
註:插件 Ubuntu不需要此步驟
[root@node1 ~]# yum install -y uwsgi-plugin-python34.x86_64
為解決啟動報錯:
!!! UNABLE to load uWSGI plugin: /usr/lib64/uwsgi/python_plugin.so: cannot open shared object file: No such file or directory !!!
[root@node1 ~]# ln -sv /usr/lib64/uwsgi/python34_plugin.so /usr/lib64/uwsgi/python_plugin.so
3 啟動uwsgi
(django) [root@node1 mysite]# uwsgi --http-socket :8888 --plugin python --wsgi-file ./test.py
Ubuntu下:uwsgi --http :88888 --wsgi-file test.py
4 測試
[root@node1 ~]# curl http://127.0.0.1:8888/index/ This is uwsgi test
test.py文件內容如下:
def application(env, start_response): start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)]) return [b"This is uwsgi test"]
5 報錯解決
(1)invalid request block size: 21573 (max 4096)...skip
buffer-size = 65536
(2)-- unavailable modifier requested: 0 --
-- unavailable modifier requested: 0 –
plugins = python #指定插件
四、Django與uwsgi的結合
1 熱啟動Django
(1)uWsgi熱加載python程序(僅做測試使用)
(django) [root@node1 mysite]# uwsgi --http-socket :8000 --plugin python --module mysite.wsgi --py-autoreload=1
按下Ctrl +C 可終止進程
(2)啟動Django
(django) [root@node1 mysite]# uwsgi --http-socket :8000 --plugin python --module mysite.wsgi
2 在/etc/目錄下新建uwsgi_nginx.ini
uwsgi支持ini、xml等多種配置方式,本文以 ini 為例,添加如下配置
[root@node1 ~]# cat /etc/uwsgi_nginx.ini [uwsgi] # Django-related settings # the base directory (full path) #Django項目的目錄 chdir = /root/django/mysite # Django‘s wsgi file module = mysite.wsgi # the virtualenv (full path) #虛擬目錄的路徑 home = /root/django # process-related settings # master master = true # maximum number of worker processes #啟動進程數,處理器個數 processes = 1 # threads = 4 #線程個數 # the socket (use the full path to be safe #指定套接字 socket = 0.0.0.0:8000 # ... with appropriate permissions - may be needed #權限設置,要是提示權限不夠,可以改為666 # chmod-socket = 664 # clear environment on exit #退出時清除環境 vacuum = true #指定插件,不然會報錯:-- unavailable modifier requested: 0 -- plugins = python
3 指定配置文件並啟動uwsgi
[root@node1 ~]# uwsgi --ini /etc/uwsgi_nginx.ini
五、 配置nginx結合uWSGI
1 安裝Nginx
[root@node1 ~]# yum install nginx –y
2 新建Nginx配置文件
[root@node1 ~]# vim /etc/nginx/conf.d/djang.conf server { listen 80; server_name 192.168.15.128; #指定本機ip或者配置你的域名 location / { include /etc/nginx/uwsgi_params; uwsgi_pass 0.0.0.0:8000; } location /static { #配置靜態文件目錄 alias /root/django/mysite/static; } location /media { alias /root/django/mysite/media; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
3 啟動nginx
[root@node1 ~]# systemctl start nginx
此時以及可以測試訪問了
[root@node1 ~]# curl http://127.0.0.1/index/ Hello World!
用瀏覽器測試如下
六、nginx結合supervisor
1 安裝supervisor
[root@node1 ~]# yum install python34-setuptools.noarch [root@node1 ~]# yum install supervisor
2 生成配置文件
通過命令生成supervisor的配支文件
[root@node1 ~]# echo_supervisord_conf > /etc/supervisord.conf
3 編輯配置文件/etc/supervisord.conf
再最後添加如下內容:
[program:mysite] command= /usr/sbin/uwsgi --uwsgi-socket 0.0.0.0:8000 --plugin python--chdir /root/django/mysite --home=/root/django --module mysite.wsgi directory=/root/django/mysite startsecs=0 stopwaitsecs=0 autostart=true autorestart=true
說明:
# [program:mysite] 指定程序名稱
# /usr/sbin/uwsgi 指定uwsgi的位置,
#--chdir /root/django/mysite 指定項目路徑
# --home=/root/django 指定尋虛擬目錄
4 啟動supervisor
最後啟動supervisor,完成uWSGI啟動django,nginx反向代理
[root@node1 ~]# supervisord -c /etc/supervisord.conf
此時配置完成!!至此,所有配置均已完成
操作說明:
supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
(1)啟動mysite程序
supervisorctl -c /etxc/supervisord.conf start mysite
(2)重啟mysite程序
supervisorctl -c /etc/supervisord.conf restart mysite
(3)停止mysite程序
supervisorctl -c /etc/supervisord.conf stop mysite
(4)更新新的配置到supervisord
supervisorctl update
(5)重新啟動配置中的所有程序
supervisorctl reload
參考:
https://www.cnblogs.com/pyyu/p/9481344.html
https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html
https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
nginx+uWSGI+django+virtualenv+supervisor部署發布web項目