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
[[email protected] ~]# yum install python34.x86_64 python34-pip.noarch -y
2 安裝虛擬環境
[[email protected] ~]# pip3 install virtualenv
(1)建立虛擬目錄
[[email protected] ~]# virtualenv django
(2)進入虛擬目錄
[[email protected]~]# cd django/
(3)啟用虛擬環境
[[email protected] django]# source bin/activate
可以看到目錄地下有預設的幾個資料夾
(django) [[email protected] 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 16Nov 5 14:55 lib64 -> /root/django/lib
二、Django的安裝與專案建立以及配置
1 安裝Django,這裡使用1.11版本
(django) [[email protected] 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) [[email protected] django]# django-admin startproject mysite
(2)建立名為app01的應用
(django) [[email protected] django]# cd mysite/ (django) [[email protected] mysite]#python3 manage.py startapp app01
(3)可以看到mysite專案下有如下目錄
(django) [[email protected] 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) [[email protected] mysite]# python3 manage.py runserver Performing system checks... ……. Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
測試訪問
[[email protected] ~]# 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):
[[email protected] ~]# yum install uwsgi 檢視版本 [[email protected] ~]# uwsgi --version 2.0.17.1
2 安裝uwsgi-plugin-python3
注:外掛 Ubuntu不需要此步驟
[[email protected] ~]# 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 !!!
[[email protected] ~]# ln -sv /usr/lib64/uwsgi/python34_plugin.so /usr/lib64/uwsgi/python_plugin.so
3 啟動uwsgi
(django) [[email protected] mysite]# uwsgi --http-socket :8888 --plugin python --wsgi-file ./test.py
Ubuntu下:uwsgi --http :88888 --wsgi-file test.py
4 測試
[[email protected] ~]# 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) [[email protected] mysite]# uwsgi --http-socket :8000 --plugin python --module mysite.wsgi --py-autoreload=1
按下Ctrl +C 可終止程序
(2)啟動Django
(django) [[email protected] mysite]# uwsgi --http-socket :8000 --plugin python --module mysite.wsgi
2 在/etc/目錄下新建uwsgi_nginx.ini
uwsgi支援ini、xml等多種配置方式,本文以 ini 為例,新增如下配置
[[email protected] ~]# 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
[[email protected] ~]# uwsgi --ini /etc/uwsgi_nginx.ini
五、 配置nginx結合uWSGI
1 安裝Nginx
[[email protected] ~]# yum install nginx –y
2 新建Nginx配置檔案
[[email protected] ~]# 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
[[email protected] ~]# systemctl start nginx
此時以及可以測試訪問了
[[email protected] ~]# curl http://127.0.0.1/index/ Hello World!
用瀏覽器測試如下
六、nginx結合supervisor
1 安裝supervisor
[[email protected] ~]# yum install python34-setuptools.noarch [[email protected] ~]# yum install supervisor
2 生成配置檔案
通過命令生成supervisor的配支檔案
[[email protected] ~]# 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反向代理
[[email protected] ~]# 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.htmlhttps://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html