uwsgi+django
-
創建新的虛擬環境,且解決crm的環境依賴
-
在虛擬環境下安裝uwsgi
pip3 install uwsgi -
學習uwsgi命令,如何啟動python應用
啟動python web文件
創建一個test.py寫入如下代碼def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3用uwsgi啟動一個python web文件
指定8000端口啟動 http服務
指定wsgi文件
uwsgi --http :8000 --wsgi-file test.py -
用uwsgi啟動django項目
uwsgi --http :9000 --module Alibab_crm.wsgiuwsgi加上熱加載命令
uwsgi --http :8000 --module Alibab_crm.wsgi --py-autoreload=1使用uwsgi配置文件去啟動項目
-
手動創建uwsgi.ini 配置文件
(alicrm) [root@s16ds Alibab_crm]# cat uwsgi.ini# mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) #指定django的項目目錄,第一層 chdir = /opt/Alibab_crm # Django's wsgi file #找到django的wsgi文件 #這里需要寫項目的第二層目錄Alibab_crm module = Alibab_crm.wsgi # the virtualenv (full path) #填寫虛擬環境的絕對路徑 home = /root/Envs/alicrm # process-related settings # master master = true # maximum number of worker processes processes = 5 # the socket (use the full path to be safe #指定socket協議,運行django,只能與nginx結合時使用 #指定socket協議,運行django,只能與nginx結合時使用 #指定socket協議,運行django,只能與nginx結合時使用 socket = 0.0.0.0:8000 #如果你沒用nginx,只想自己啟動一個http界面,用這個 #http = 0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true -
通過配置文件啟動uwsgi
uwsgi --ini uwsgi.ini
-
-
收集django crm的靜態文件
編輯crm的settings.py配置文件
寫入如下代碼# 定義django的靜態資源根目錄,便於用命令收集資源,存放的地兒 STATIC_ROOT="/opt/crm_static" STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]用命令收集靜態文件
python3 manage.py collectstatic -
配置nginx,反響代理django服務器,且解析靜態文件
proxy_pass 僅僅是請求轉發的參數,與uwsgi結合,還有跟高級的協議參數
修改nginx配置文件如下
server { listen 80; server_name s16chiji.com; location / { #root /opt/s16chiji; #index index.html; #使用uwsgi_pass 轉發基於uwsgi協議的一個請求 uwsgi_pass 192.168.15.71:8000; include /opt/nginx112/conf/uwsgi_params; } #配置一個url的入口,告訴django靜態文件在哪里去找 #當請求url是 s16chiji.com/static/的時候 #就進行別名,nginx去/opt/crm_static下尋找js文件 location /static { alias /opt/crm_static/; } #通過這個參數,定義錯誤頁面的文件 ,當狀態碼是 404 400 401 時,返回40x.html頁面 error_page 404 401 400 403 /40x.html; } -
此時nginx結合uwsgi 已經完成
-
記住這里推出虛擬環境,使用物理環境去運行
配置supervisor工具,管理django后台
- 這個東西只能用python2去實現
-
下載supervisor
easy_install supervisor -
配置supervisor的配置文件,編寫django任務
echo_supervisord_conf > /etc/supervisor.conf -
編寫運行django的任務
vim /etc/supervisor.conf
在最底行寫入如下代碼[program:s16alicrm] command=/root/Envs/alicrm/bin/uwsgi --ini /opt/Alibab_crm/uwsgi.ini autorestart=true stopasgroup=true killasgroup=true -
啟動suopersivod這個程序
啟動服務端 supervisord -c /etc/supervisor.conf 通過客戶端命令查看任務 supervisorctl -c /etc/supervisor.conf -
學習supervisor管理命令
[root@s16ds alicrm]# supervisorctl -c /etc/supervisor.conf s16alicrm RUNNING pid 5293, uptime 0:03:03 supervisor> stop all #停止所有任務 supervisor> start all #啟動s所有任務 supervisor> status s16alicrm
