詳解Ubuntu環境下部署Django+uwsgi+nginx總結
前言
這是我在搭建Django項目時候的過程,拿來總結記錄,以備不時之需。
項目采用nginx+uwsgi的搭配方式。
項目依賴包采用 requirements.txt 文件管理的方式。
本地準備工作
確認項目能夠運行起來,沒有 bug
將當前環境的包導出 pip freeze > requirements.txt
將項目上傳到服務器上的 /srv 目錄下。這里以 git 的形式為例, 打開終端, 依次輸入如下命令:
$ git init$ git remote add origin xxx.git # 替換成你的項目git地址$ git add .$ git commit -m ’first commit’$ git pull origin master --allow-unrelated-histories$ git push origin master
部署項目到服務器
安裝python
安裝好項目用到的 python 。
$ sudo apt install python$ sudo apt install python-pip$ pip install --upgrade pip
安裝 virtualenv 以及 virutalenvwrapper ,并創建虛擬環境。
$ pip install virtualenv$ pip install virtualenvwrapper$ sudo apt install vim
編輯文件 ~/.bashrc
$ vim ~/.bashrc# 添加如下2行代碼export WORKON_HOME=$HOME/.virtualenvssource /usr/local/bin/virtualenvwrapper.sh# 保存文件,讓文件成效$ source ~/.bashrc
安裝git:
$ sudo apt install git# 為了方便XShell或者CRT連接服務器,建議安裝OpenSSH$ sudo apt install openssh-server openssh-client$ service ssh restart
安裝MySQL
$ sudo apt install mysql-server mysql-client$ sudo apt-get install libmysqld-dev
測試配置
安裝依賴包,進入虛擬環境 workon *** ,進入項目根目錄,執行命令 pip install -r requirements.txt 創建數據庫,新打開一個終端,登錄數據庫, mysql -uroot -p , 創建相應的數據庫 CREATE DATABASE IF NOT EXISTS my_db default charset utf8mb4; 遷移數據, python manage.py migrate 收集靜態文件, python manage.py collectstatic 啟動服務器,執行 python manage.py runserver 0.0.0.0:8000 ,然后在你自己電腦上,在瀏覽器中輸入 http://<your server ip>:8000 ,訪問下網站所有頁面,確保所有頁面都沒有錯誤。注意:
設置 ALLOW_HOST 為你的域名或 ip 地址。 設置 DEBUG=False 。安裝uwsgi
uwsgi 是一個應用服務器,非靜態文件的網絡請求就必須通過他完成,他也可以充當靜態文件服務器,但不是他的強項。
uwsgi 是使用 python 編寫的,因此通過 pip install uwsgi 就可以了。( uwsgi 必須安裝在系統級別的 Python 環境中,不要安裝到虛擬環境中)。
命令行啟動 uwsgi :
$ uwsgi --http :8000 --module test.wsgi --vritualenv=/root/.virtualenvs/django-env-py36
如果能夠在瀏覽器中訪問到測試的頁面,說明uwsgi可以加載項目了。
配置文件方式啟動 uwsgi :
在項目的根路徑下面,創建一個文件 djangotest.ini ,填寫以下代碼:
[uwsgi]# Django相關的配置# 必須全部為絕對路徑# 項目的路徑chdir=/srv/djangotest# Django的wsgi文件module=djangotest.wsgi# Python虛擬環境的路徑home=/root/.virtualenvs/django-env-py36# 進程相關的設置# 主進程master=true# 最大數量的工作進程processes=10# socket文件路徑,絕對路徑socket=/srv/djangotest/djangotest.sock# 設置socket的權限chmod-socket=666# 退出的時候是否清理環境vacuum=true
然后使用命令uwsgi --ini djangotest.ini,看下是否還能啟動這個項目。
安裝nginx
nginx 是一個 web 服務器。用來加載靜態文件和接收 http 請求的。
通過命令 sudo apt install nginx 即可安裝。
nginx 常用命令:
啟動nginx:service nginx start 關閉nginx:service nginx stop 重啟nginx:service nginx restart收集靜態文件:
靜態文件應該讓 nginx 來處理,而不是讓 django 來做。
首先確保你的 settings.py 文件中有一個 STATIC_ROOT 配置,這個配置應該指定你的靜態文件要放在哪個目錄下。
那么我們可以執行以下命令: python manage.py collectstatic 來收集所有靜態文件(已經執行過請忽略)。
編寫nginx配置文件,在 /etc/nginx/conf.d 目錄下,新建一個文件 djangotest.conf ,然后將以下代碼貼進去:
upstream djangotest { server unix:///srv/djangotest/djangotest.sock; }# 配置服務器server { # 監聽的端口號 listen 80; # 域名 server_name 192.168.0.101; charset utf-8; # 最大的文件上傳尺寸 client_max_body_size 75M; # 靜態文件訪問的url location /static { # 靜態文件地址 alias /srv/djangotest/static_dist; } # 最后,發送所有非靜態文件請求到django服務器 location / { uwsgi_pass djangotest; # uwsgi_params文件地址 include /etc/nginx/uwsgi_params; }}
測試配置文件: service nginx configtest 。注意:每次修改完配置需要重啟 nginx : service nginx restart
使用supervisor
讓supervisor管理uwsgi,可以在uwsgi發生意外的情況下,自動重啟。
安裝 supervisor :在系統級別的python環境下 pip install supervisor 。
在項目根目錄下創建一個文件 my_supervisor.conf 。編寫內容:
# supervisor的程序名字[program:mysite]# supervisor執行的命令command=uwsgi --ini zlkt_uwsgi.ini# 項目的目錄directory = /srv/djangotest # 開始的時候等待多少秒startsecs=0# 停止的時候等待多少秒stopwaitsecs=0 # 自動開始autostart=true# 程序掛了后自動重啟autorestart=true# 輸出的log文件stdout_logfile=/srv/djangotest/log/supervisord.log# 輸出的錯誤文件stderr_logfile=/srv/djangotest/log/supervisord.err[supervisord]# log的級別loglevel=info# 使用supervisorctl的配置[supervisorctl]# 使用supervisorctl登錄的地址和端口號serverurl = http://127.0.0.1:9001# 登錄supervisorctl的用戶名和密碼username = adminpassword = 123[inet_http_server]# supervisor的服務器port = :9001# 用戶名和密碼username = adminpassword = 123[rpcinterface:supervisor]supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
運行 supervisor ,執行 supervisord -c my_supervisor.conf 。
進入 supervisor 管理控制臺, supervisorctl -c my_supervisor.conf
supervisor 管理控制臺常用命令
# 查看狀態status# 啟動程序start program_name# 重新啟動程序restart program_name# 關閉程序stop program_name# 重新加載配置文件reload# 退出控制臺quit
到此這篇關于Ubuntu環境下部署Django+uwsgi+nginx總結的文章就介紹到這了,更多相關Ubuntu環境下部署Django+uwsgi+nginx總結內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: