檢測是否已安裝virtualenv
virtualenv --version若沒有顯示版本號則表示沒有安裝virtualenv 安裝virtualenv
pip install virtualenv創建虛擬環境 路徑可自選,也可放置項目目錄下 venv為虛擬環境名,可自定義
virtualenv venv進入虛擬環境(需在venv當前目錄下操作)
source /venv/bin/activate# 退出虛擬環境命令deactivate安裝項目相關依賴 (此操作需要提前提前生成requirements.txt依賴文件) 插入簡單說下django依賴文件如何生成
生成依賴文件pip freeze > requirements.txt安裝依賴
pip install -r requirements.txt如何查看依賴列表
pip list至于為什么要用虛擬環境來部署,其中的原因若有疑問就自行谷歌吧。不僅僅是部署,每新建一個項目,都建議使用虛擬環境,好處大大的。
下面就要開始正式的部署了,這篇教程以Ubuntu為例 整個部署結構如下: the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django nginx做反向代理處理靜態文件,減輕服務器負載 同時也可以配置多臺服務器做負載均衡(此篇博客不超扯) uwsgi處理后臺請求 ps:后面寫些系列博客介紹下uwsgi和nginx等部署所需 ps:畢竟光講怎么用,不說原理真是耍流氓
虛擬環境下安裝uwsgi
pip install uwsgi# 若安裝失敗,可能是python依賴庫沒有安裝# 執行以下命令apt-get install python-dev編寫測試腳本 在項目目錄下添加test.py腳本,添加如下內容
# test.pydef application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2用test.py測試uwsgi
uwsgi --http :8000 --wsgi-file test.py# 若跑不起來,可能需要添加參數# 具體原因這里不深究,歷史遺留# 感興趣的同學可自行谷歌uwsgi --plugin python,http --http :8000 --wsgi-file test.py瀏覽器訪問localhost:8000查看頁面是否顯示hello world 若正常顯示,則說明如下環節正常拉通 the web client <-> uWSGI <-> Python 接著先測試以下django項目自身能否跑通
python manage.py runserver 0.0.0.0:8000確認沒問題后,用uwsgi拉通django
uwsgi --http :8000 --module mysite.wsgi此處wsgi命名方式:項目名.wsgi 此wsgi文件可在項目目錄中得主目錄下找到 確認正常運行,說明如下環節正常拉通
the web client <-> uWSGI <-> Django安裝nginx
apt-get install nginx測試nginx能否正常運行,啟動nginx
/etc/init.d/nginx start# 或者service nginx start訪問瀏覽器80端口 打開localhost:80 若顯示Welcome to nginx!則說明nginx正常運行 說明如下環節正常拉通
the web client <-> the web servernginx默認占用80端口 同時訪問公網ip,默認訪問得端口也是80 即可以直接訪問localhost,不需要加80端口號 ps:贈送一些常用nginx命令
# 重啟nginxservice nginx restart# 查看nginx運行狀態service nginx status# 停止nginx服務service nginx stop編寫nginx配置文件(ubuntu下,centos路徑則不同) nginx相關配置存放在/etc/nginx 先將uwsgi_params文件復制到項目目錄下 uwsgi_params可在/etc/nginx下找到
cp /etc/nginx/uwsgi_params /path/to/your/PRoject然后在項目目錄下新建mysite_nginx.conf 配置文件名,自己記得住就好,保證可讀性 填入以下內容
# myproject_nginx.conf# the upstream component nginx needs to connect toupstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8001; # for a web port socket (we'll use this first)}# configuration of the serverserver { # the port your site will be served on listen 8000; # the domain name it will serve for server_name .example.com; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /path/to/your/mysite/media; # your Django project's media files - amend as required } location /static { alias /path/to/your/mysite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed }}寫好配置文件后,需要將此配置文件軟鏈接至nginx配置文件目錄下 配置文件則存放在/etc/nginx/sites-enabled
ln -s /path/to/your/mysite_nginx.conf /etc/nginx/sites-enabled/# 舉例conf文件路徑為/home/test/mysite/mysite_nginx.conf# 命令就這么寫ln -s /home/test/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/接下來拉通部署下靜態文件 在django項目得的setting文件中,添加下面一行內容:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")運行python命令
python manage.py collectstatic此命令會將項目中得所有靜態文件全部匯總到static目錄下 原因就是為了方便給nginx做反向代理時,可以快速得找到所請求得靜態文件 現在可以測試以下nginx是否能夠正常訪問靜態文件 重啟以下nginx
service nginx restart任意在瀏覽器訪問一個media里面得圖片文件(沒圖片,就自己加一個) 比如訪問localhost:8000/media/test.png 若是正常顯示,那么接下來得路就好走多了。 然而,意外往往時會發生的,訪問不到的就根據報錯來定位問題 同時借助nginx日志,來查看問題的原因 nginx日志路徑/var/log/nginx/error.log 一般情況下,由于權限問題導致訪問失敗的可能性最大 這時候就需要修改項目所在目錄的權限 所以項目部署的時候不要把代碼放到/root等權限敏感的目錄下 ok,往下走 這時候就需要拉通nginx和uwsgi了 接著用test.py測試一把
uwsgi --socket :8001 --wsgi-file test.py訪問8000端口,沒有問題的話,說明以下環節也拉通了 the web client <-> the web server <-> the socket <-> uWSGI <-> Python 這時候舉例萬里長征真的就幾步路了 我們之前nginx使用的時tcp sokect轉發請求,也就是使用端口轉發 現在,換成unix socket轉發 unix socket相對tcp socket速度更快,節省端口資源 然而要是做負載均衡的話,就需要利用不同的端口轉發請求至處理服務器了 不多說,修改nginx配置文件
server unix:///path/to/your/mysite/mysite.sock; # for a file socket# server 127.0.0.1:8001; # for a web port socket (we'll use this first)然后重啟nginx 啟動uwsgi
uwsgi --socket mysite.sock --wsgi-file test.py訪問8000端口,看到hello world,那就是勝利的曙光 然而這里往往是會保權限錯誤所以,出現權限問題,就試試下面兩個命令 其實就加個兩個權限參數
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666# 或者uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664搞定這個問題后,就可以拉通整個部署結構了(這個權限最保險^_^)
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666好了,其實整個部署過程到這就可以結束了 但是在命令中加各種參數是不太好的一個編碼習慣 還是寫到配置文件里面去 在項目目錄下新建mysite_uwsgi.ini
# mysite_uwsgi.ini file[uwsgi]# Django-related settings# the base directory (full path)chdir = /path/to/your/project# Django's wsgi filemodule = project.wsgi# the virtualenv (full path)home = /path/to/virtualenv# process-related settings# mastermaster = true# maximum number of worker processes# 進程數設置與cpu核數相同,保證并行性能processes = 10# the socket (use the full path to be safesocket = /path/to/your/project/mysite.sock# ... with appropriate permissions - may be needed# chmod-socket = 664# clear environment on exitvacuum = true然后用簡單的uwsgi命令,就能完成部署了
uwsgi --ini mysite_uwsgi.inips:附贈一些相關項目狀態查看命令
# 查看某端口占用情況(80端口為例)lsof -i:80# 查看uwsgi或者nginx進程ps -ef | grep uwsgi# 停止uwsgi主進程pkill uwsgi# 殺掉某進程只需要知道pid既可kill pid
|
新聞熱點
疑難解答