nginx,uwsgi發布web服務器


 

復制代碼
1.單機啟動django項目,性能低,默認使用wsgiref模塊,性能低的wsgi協議

python3 manager.py runserver 0.0.0.0:8000   > wsgiref模塊中

2.高並發啟動django,django是沒有這個功能的,而uWSGI模塊,遵循uwsgi協議,支持多進程處理django請求

uwsgi  通過他,啟動你的django,而不再是python3 manager.py runserver 0.0.0.0:8000


3.公司中一般用 nginx + uwsgi + django + virtualenv  + supervisord(進程管理工具)


搭建筆記:
    1.確定依賴組件是否安裝
    yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
    
    

    
    
nginx 正向代理,反向代理的概念




用戶阿段,去訪問mycrm.com:80 ,他想直接從80端口,找到hello視圖,也就是mycrm.com:80/hello 
實現手段就是,阿段去訪問 mycrm.com:80 這個nginx服務,並且讓nginx,把hello這個請求,丟給后端的 uwsgi+django程序處理

1.基礎環境准備好
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

2.准備好python3環境

3.准備好virtualenv 

4.安裝uWSGI
    1.激活虛擬環境
    source /opt/all_venv/venv2/bin/activate
    
    2.安裝uWSGI
    (venv2) [root@s13linux ~ 05:18:21]$pip3 install uwsgi
    
    3.檢查uwsgi版本
        (venv) [root@slave 192.168.11.64 /opt]$uwsgi --version
        2.0.17.1
        #檢查uwsgi python版本
        uwsgi --python-version
    
    4.運行一個簡單的uwsgi服務器
        1.創建一個test.py文件,寫入內容
        def application(env, start_response):
            start_response('200 OK', [('Content-Type','text/html')])
            return [b"Hello World"] # python3
            
        2.然后用uwsgi命令啟動
        uwsgi --http :8000 --wsgi-file test.py
            參數解釋
            http :8000: 使用http協議,端口8000
            wsgi-file test.py: 加載指定的文件,test.py
        
    5.用uwsgi運行你的django項目(測試使用)
        1.准備好mysite,自己寫好MTV視圖函數  /hello 
    
    先確保你在項目文件夾下,例如/opt/mysite/底下
    
    uwsgi --http :8088 --module mysite.wsgi --py-autoreload=1 
        參數解析
            --http 啟動在8088端口,--module 指定項目文件夾路徑  --py-autoreload是熱加載程序
    
    6.配置nginx反向代理uwsgi+django!!!!(此步重要!!!)
        1.首先kill殺掉nginx進程
        2.配置nginx.conf,通過此步才能生效!!
            填入重要兩個參數,根據自己目錄結構配置,uwsgi_pass通過這個參數,nginx才能轉發請求給后端0.0.0.0:9000的應用
            include  /opt/nginx112/conf/uwsgi_params;
            uwsgi_pass 0.0.0.0:9000;
        --------------------------分割線--------------------------------------------------------
             server {
                    listen       80;
                    server_name  mycrm.com;
                    location / {
                        include  /opt/nginx112/conf/uwsgi_params;
                        uwsgi_pass 0.0.0.0:9000;
                        root   html;
                        index  index.html index.htm;
                        #deny 10.0.0.1;
        }
        
        配置nginx.conf之后,啟動nginx服務,等待配置啟動uwsgi+django 
uwsgi --uwsgi 0.0.0.0:9000 --chdir=/opt/mysit --home=opt/all_venv/venv2/ --module=mysite.wsgi # 這條命令也是在項目目錄下 如 /home/perfey
7.配置supervisor進程管理工具 1.通過python2的包管理工具easy_install安裝 yum install python-setuptools easy_install supervisor 2.通過命令生成supervisor的配支文件 echo_supervisord_conf > /etc/supervisord.conf 3.寫入/etc/supervisord.conf配置信息(參數根據自己環境填寫) [program:my_crm] command=/opt/all_venv/venv2/bin/uwsgi(uwsgi的絕對路徑) --uwsgi 0.0.0.0:9000(端口號應與nginx配置中的端口號一致) --chdir=/opt/s13crm(項目目錄) --home=/opt/all_venv/venv2/(虛擬環境目錄) --module=s13crm.wsgi(項目中和項目同名文件夾下的wsgi.py文件,使用.連接文件夾名和文件,注意不是 /) directory=/opt/s13crm (項目目錄) startsecs=0 stopwaitsecs=0 autostart=true autorestart=true 8.啟動supervi服務,(同時啟動uwsgi+django服務) 最后啟動supervisor,完成uWSGI啟動django,nginx反向代理 supervisord -c /etc/supervisord.conf #啟動supervisor supervisorctl -c /etc/supervisord.conf restart my #重啟my項目 supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all] 9.此時訪問網站mycrm.com ,查看是否可以通過80端口,訪問到django應用,完成項目發布。 由於nginx的高並發性能,配合uwsgi的多進程性能,可以達到一個線上的django應用發布!!! 內容博客地址: https://www.cnblogs.com/pyyu/p/9481344.html
復制代碼

 

 

 

 

大家都學過了django,用django寫了各種功能,寫了bbs項目,寫了路飛學城。

咱們都知道django是一個web框架,方便我們快速開發web程序,http請求的動態數據就是由web框架來提供處理的。

前面超哥也對nginx簡單的介紹了,本文將nginx、WSGI、uwsgi、uWSGI、django這幾個關系梳理一下。

wsgi    全稱web server gateway interface,wsgi不是服務器,也不是python模塊,只是一種協議,描述web server如何和web application通信的規則。
運行在wsgi上的web框架有bottle,flask,django
uwsgi    和wsgi一樣是通信協議,是uWSGI服務器的單獨協議,用於定義傳輸信息的類型
uWSGI    是一個web服務器,實現了WSGI協議,uwsgi協議。a
nginx    web服務器,更加安全,更好的處理處理靜態資源,緩存功能,負載均衡,因此nginx的強勁性能,配合uWSGI服務器會更加安全,性能有保障。
django 高級的python web框架,用於快速開發,解決web開發的大部分麻煩,程序員可以更專注業務邏輯,無須重新造輪子

邏輯圖

web服務器

傳統的c/s架構,請求的過程是
客戶端 > 服務器 
服務器 > 客戶端
服務器就是:1.接收請求 2.處理請求 3.返回響應

web框架層

HTTP的動態數據交給web框架,例如django遵循MTV模式處理請求。
HTTp協議使用url定位資源,urls.py將路由請求交給views視圖處理,然后返回一個結果,完成一次請求。
web框架使用者只需要處理業務的邏輯即可。

如果將一次通信轉化為“對話”的過程

Nginx:hello wsgi,我剛收到一個請求,你准備下然后讓django來處理吧

WSGI:好的nginx,我馬上設置環境變量,然后把請求交給django

Django:謝謝WSGI,我處理完請求馬上給你響應結果

WSGI:好的,我在等着

Django:搞定啦,麻煩wsgi吧響應結果傳遞給nginx

WSGI:太棒了,nginx,響應結果請收好,已經按照要求傳遞給你了

nginx:好滴。我把響應交給用戶。合作愉快

Django Nginx+uwsgi 安裝配置

在前面的章節中我們使用 python manage.py runserver 來運行服務器。這只適用測試環境中使用。

正式發布的服務,需要一個可以穩定而持續的服務器。

基礎開發環境配置

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

提前安裝好python3環境

https://www.cnblogs.com/pyyu/p/7402145.html

virtualenv

請確保你的虛擬環境正常工作
https://www.cnblogs.com/pyyu/p/9015317.html

安裝django1.11

pip3 install django==1.11
#創建django項目mysite
django-admin startproject mysite
#創建app01
python3 manage.py startapp app01

mysite/settings.py

#settings.py設置
ALLOWED_HOSTS = ['*']
install app01

mysite/urls.py

from app01 import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^hello_django/', views.hello),
]

app01/views.py

復制代碼
from django.shortcuts import render,HttpResponse

# Create your views here.
def hello(request):
    print('request is :',request)
    return HttpResponse('django is ok ')
復制代碼

安裝uWSGI

復制代碼
進入虛擬環境venv,安裝uwsgi
(venv) [root@slave 192.168.11.64 /opt]$pip3 install uwsgi
檢查uwsgi版本
(venv) [root@slave 192.168.11.64 /opt]$uwsgi --version
2.0.17.1
#檢查uwsgi python版本
uwsgi --python-version
復制代碼

運行簡單的uWSGI

復制代碼
#啟動一個python
uwsgi --http :8000 --wsgi-file test.py
  • http :8000: 使用http協議,端口8000
  • wsgi-file test.py: 加載指定的文件,test.py
#test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
復制代碼

uWsgi熱加載python程序

復制代碼
在啟動命令后面加上參數
uwsgi --http :8088 --module mysite.wsgi --py-autoreload=1 
#發布命令
command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi
#此時修改django代碼,uWSGI會自動加載django程序,頁面生效
復制代碼

運行django程序

#mysite/wsgi.py  確保找到這個文件
uwsgi --http :8000 --module mysite.wsgi
  • module mysite.wsgi: 加載指定的wsgi模塊

uwsgi配置文件

復制代碼
uwsgi支持ini、xml等多種配置方式,本文以 ini 為例, 在/etc/目錄下新建uwsgi_nginx.ini,添加如下配置:

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /opt/mysite
# Django's wsgi file
module          = mysite.wsgi
# the virtualenv (full path)
home            = /opt/venv
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 1
# the socket (use the full path to be safe
socket          = 0.0.0.0:8000
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
復制代碼

指定配置文件啟動命令

uwsgi --ini  /etc/uwsgi_nginx.ini

配置nginx結合uWSGI

配置nginx.conf

復制代碼
worker_processes  1;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
   #nginx反向代理uwsgi server { listen
80; server_name 192.168.11.64; location / { include /opt/nginx1-12/conf/uwsgi_params; uwsgi_pass 0.0.0.0:8000; root html; index index.html index.htm; }
     #nginx處理靜態頁面資源
    
location /static{
        alias /opt/nginx1-12/static;   
}
     #nginx處理媒體資源
     
location /media{
        alias /opt/nginx1-12/media;   
         }
error_page 500 502 503 504 /50x.html;
 location = /50x.html { root html; } } }
復制代碼

配置完啟動nginx

supervisor

supervisor 是基於 python 的任務管理工具,用來自動運行各種后台任務,當然你也能直接利用 nohup 命令使任務自動后台運行,但如果要重啟任務,每次都自己手動 kill 掉任務進程,這樣很繁瑣,而且一旦程序錯誤導致進程退出的話,系統也無法自動重載任務。

這里超哥要配置基於virtualenv的supervisor

由於supervisor在python3下無法使用,因此只能用python2去下載!!!!!!

#注意此時已經退出虛擬環境了!!!!!
yum install python-setuptools
easy_install supervisor

通過命令生成supervisor的配支文件

echo_supervisord_conf > /etc/supervisord.conf

然后再/etc/supervisord.conf末尾添加上如下代碼!!!!!!

復制代碼
[program:my]
#command=/opt/venv/bin/uwsgi --ini  /etc/uwsgi_nginx.ini  #這里是結合virtualenv的命令 和supervisor的精髓!!!!
command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi
#--home指的是虛擬環境目錄 --module找到 mysite/wsgi.py
directory
=/opt/mysite startsecs=0 stopwaitsecs=0 autostart=true autorestart=true
復制代碼

最后啟動supervisor,完成uWSGI啟動django,nginx反向代理

supervisord -c /etc/supervisord.conf #啟動supervisor
supervisorctl -c /etxc/supervisord.conf restart my #重啟my項目
supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]

 重新加載supervisor

復制代碼
一、添加好配置文件后

二、更新新的配置到supervisord    

supervisorctl update
三、重新啟動配置中的所有程序

supervisorctl reload
四、啟動某個進程(program_name=你配置中寫的程序名稱)

supervisorctl start program_name
五、查看正在守候的進程

supervisorctl
六、停止某一進程 (program_name=你配置中寫的程序名稱)

pervisorctl stop program_name
七、重啟某一進程 (program_name=你配置中寫的程序名稱)

supervisorctl restart program_name
八、停止全部進程

supervisorctl stop all
注意:顯示用stop停止掉的進程,用reload或者update都不會自動重啟。
復制代碼

 django的靜態文件與nginx配置

mysite/settings.py

STATIC_ROOT='/opt/nginx1-12/static'
STATIC_URL = '/static/'
STATICFILES_DIRS=[
    os.path.join(BASE_DIR,"static"),
]

上述的參數STATIC_ROOT用在哪?

通過python3 manage.py collectstatic 收集所有你使用的靜態文件保存到STATIC_ROOT!

STATIC_ROOT 文件夾 是用來將所有STATICFILES_DIRS中所有文件夾中的文件,以及各app中static中的文件都復制過來
# 把這些文件放到一起是為了用nginx等部署的時候更方便


 

Vue + 路飛項目發布:推薦博客https://www.cnblogs.com/pyyu/p/10160874.html

 


免責聲明!

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



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