一, 背景 前臺Vue項目構建部署到nginx服務器上,接下來部署後臺,不再使用python managy.py runserver 0.0.0.0:10082測試方式,而是使用 uwsgi 啓動 django 二, 部署流程 [在虛擬環境中] 0. . /root/venv/bin/activate 1. settings.py STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATICFILES_DIRS = ( os.path.join(BASE_DIR, 't3static'), ) 2. python manage.py collectstatic 3. pip3.6 install uwsgi 4. 與manage.py 同級創建 uwsgi.ini 文件 [uwsgi] # 用戶自定義變量,方便後面引用 project = orange username = root base = /opt/gits # 表示需要操作的目錄,也就是項目的目錄 chdir = %(base)/%(project) wsgi-file = webhook/wsgi.py # python 虛擬環境 home = /root/venv/ module = %(project).wsgi:application master = true processes = 20 uid = %(username) gid = %(username) socket = /var/run/%(project).sock pidfile = /var/run/%(project).pid chown-socket = %(username):%(username) chmod-socket = 666 vacuum = true # 記錄日誌,不要忘記創建目錄及權限,注意這會導致進程爲後臺進程,即不可使用supervisor管理 daemonize = %(base)/log/%(project).log #當python源文件變化時會自動加載,一般開發環境使用 py-autoreload =1 harakiri = 600 5. 啓動 uwsgi uwsgi --ini uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini [uwsgi-static] added mapping for /static => /opt/git/orange/static 6. 配置nginx , 使用nginx做uwsgi代理 /usr/local/nginx/conf/nginx.conf include /usr/local/nginx/conf/conf.d/*.cnf; /usr/local/nginx/conf/conf.d/uwsgi_orange.cnf server { listen 10082 default_server; server_name _; client_max_body_size 50m; client_header_buffer_size 10m; large_client_header_buffers 4 10m; location / { root /opt/gits/orange; #index index.html; uwsgi_send_timeout 1800s; # 指定向uWSGI傳送請求的超時時間,完成握手後向uWSGI傳送請求的超時時間。 uwsgi_connect_timeout 1800s; # 指定連接到後端uWSGI的超時時間。 uwsgi_read_timeout 1800s; # 指定接收uWSGI應答的超時時間,完成握手後接收uWSGI應答的超時時間。 #uwsgi_pass 127.0.0.1:8000; uwsgi_pass unix:///var/run/orange.sock; include /usr/local/nginx/conf/uwsgi_params; } location /static { alias /opt/gits/orange/static; } #通過這個參數,定義錯誤頁面的文件 ,當狀態碼是 404 400 401 時,返回40x.html頁面 error_page 404 401 400 403 /40x.html; } /usr/local/nginx/sbin/nginx -s reload 三,測試驗證後臺應用 ps aux | grep uwsgi # 有進程存在 打開瀏覽器 http://192.168.89.133:10082 網頁存在,登錄沒有問題,則成功 四,封裝啓動腳本 /usr/local/bin/restart_orange.sh #!/bin/bash prj_base='/opt/gits/orange/' PY_VENV=/root/venv uwsgi_ini=/opt/gits/orange/uwsgi.ini #判斷用戶是否爲root if [ `whoami` != "root" ]; then echo "only root user can run !!!" echo "運行uwsgi的用戶必須爲root" exit 1 fi ps aux |egrep ${uwsgi_ini} |grep -v 'grep' if [ $? -eq 0 ];then ps aux |egrep ${uwsgi_ini} |grep -v 'grep' |awk '{print $2}' |xargs kill -9 if [ $? -eq 0 ];then echo "殺死uwsgi成功" else echo "殺死uwsgi失敗" exit 6 fi else echo "沒有uwsgi在運行" fi source ${PY_VENV}/bin/activate cd ${prj_base} chmod 777 ${prj_base}manage.py #${prj_base}manage.py collectstatic &>/dev/null #if [ $? -eq 0 ];then # echo "靜態文件收集成功" #else # echo "靜態文件收集失敗,請手工收集" #fi uwsgi --uid root --gid root --ini ${uwsgi_ini} if [ $? -eq 0 ];then echo "uwsgi啓動成功" fi chmod +x restart_orange.sh restart_orange.sh 殺死uwsgi成功 [uWSGI] getting INI configuration from /opt/gits/orange/uwsgi.ini uwsgi啓動成功 ps aux | grep /opt/gits/orange/uwsgi.ini root 94529 0.7 1.0 357048 41548 ? S 20:41 0:00 uwsgi --uid root --gid root --ini /opt/gits/orange/uwsgi.ini root 94534 0.1 0.9 357304 34996 ? Sl 20:41 0:00 uwsgi --uid root --gid root --ini /opt/gits/orange/uwsgi.ini root 94535 0.1 0.9 357304 34988 ? Sl 20:41 0:00 uwsgi --uid root --gid root --ini /opt/gits/orange/uwsgi.ini ... ... 五,Vue前端項目使用nginx代理的uwsgi提供的Django後端服務 前端Vue因爲要訪問後端nginx代理的uwsgi接口,所以需要修改 nginx 配置,打通前後端接口連通性。 vim /usr/local/nginx/conf/conf.d/orange_v1.cnf server { listen 80; server_name www.orange.com; location / { try_files $uri $uri/ /index.html; root /usr/local/nginx/html/orange_v1; index index.html index.htm; } location /api { proxy_pass http://192.168.89.133:10082; } } /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful /usr/local/nginx/sbin/nginx -s reload 六,全局性測試 因爲已經做了前端域名解析服務,所以,瀏覽器打開直接訪問服務對應的域名即可,http://www.orange.com 輸入錯誤用戶名和密碼,有錯誤提示,輸入正確的,登錄後沒有問題,則驗證通過。
雲計算之VUE-Django開發
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.