nginx+uWSGI+django+virtualenv+supervisor發布web服務器
uwsgi+django
1.創建新的虛擬環境,且解決crm的環境依賴
mkvirtualenv crm
workon crm
2.在虛擬環境下安裝uwsgi
pip3 install uwsgi
3.學習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
4.用uwsgi啟動django項目
使用uwsgi配置文件去啟動項目
1.手動創建uwsgi.ini 配置文件
找個地方創建 uwsgi.ini
[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
2.通過配置文件啟動uwsgi (--py-autoreload=1 熱加載,後臺改動文件 實時更新)
uwsgi --ini uwsgi.ini --py-autoreload=1
5.收集django crm的靜態文件
編輯crm的settings.py配置文件
寫入如下代碼
#定義django的靜態資源根目錄,便於用命令收集資源,存放的地兒
STATIC_ROOT="/opt/crm_static" STATIC_URL = ‘/static/‘ STATICFILES_DIRS = [ os.path.join(BASE_DIR, ‘static‘)
進入虛擬環境
workon crm
用命令收集靜態文件
python3 manage.py collectstatic
6.配置nginx,反響代理django服務器,且解析靜態文件
proxy_pass 僅僅是請求轉發的參數,與uwsgi結合,還有跟高級的協議參數
修改nginx配置文件如下
server { listen 80; server_name niuli.xyz; location / { uwsgi_pass 公網ip:8000; include /opt/nginx1-12/conf/uwsgi_params; } #配置一個url的入口,告訴django靜態文件在哪裏去找 #當請求url是 niuli.xyz/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; }
7.此時nginx結合uwsgi 已經完成
niuli.xyz
8.記住這裏推出虛擬環境,使用物理環境去運行
8.記住這裏推出虛擬環境,使用物理環境去運行
8.記住這裏推出虛擬環境,使用物理環境去運行
8.記住這裏推出虛擬環境,使用物理環境去運行
8.記住這裏推出虛擬環境,使用物理環境去運行
8.記住這裏推出虛擬環境,使用物理環境去運行
8.記住這裏推出虛擬環境,使用物理環境去運行
配置supervisor工具,管理django後臺
這個東西只能用python2去實現
1.下載supervisor
easy_install supervisor
2.配置supervisor的配置文件,編寫django任務
echo_supervisord_conf > /etc/supervisor.conf
3.編寫運行django的任務
vim /etc/supervisor.conf
在最底行寫入如下代碼
[program:crm]
# command=虛擬環境中的uwsgi --ini /項目中的uwsgi配置文件 熱加載
command=/root/Envs/alicrm/bin/uwsgi --ini /opt/Alibab_crm/uwsgi.ini --py-autoreload=1
autorestart=true
stopasgroup=true
killasgroup=true
4.啟動suopersivod這個程序
啟動服務端 supervisord -c /etc/supervisor.conf 通過客戶端命令查看任務 supervisorctl -c /etc/supervisor.conf
5.學習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
nginx+uWSGI+django+virtualenv+supervisor發布web服務器