本文主要講解 nginx + uwsgi socket 的方式來部署 Django,比 Apache mod_wsgi 要復雜一些,但這是目前主流的方法。
推薦:使用Code Studio 雲端開發,新人更有免費一個月雲主機,可以用來實戰體驗本節的部署!
1. 運行開發服務器測試
1
2
|
cd
zqxt
# 進入項目 zqxt 目錄
python manage.py runserver
|
運行開發服務器測試,確保開發服務器下能正常打開網站。
2. 安裝 nginx 和 需要的包
2.1 安裝 nginx 等軟件
ubuntu / Linux Mint 等,下面簡寫為 (ubuntu):
1
|
sudo
apt-get
install
python-dev nginx
|
centos / Fedora/ redhat 等,下面簡寫為 (centos)
1
2
|
sudo
yum
install
epel-release
sudo
yum
install
python-devel nginx
|
2.2 安裝 supervisor, 一個專門用來管理進程的工具,我們用它來管理 uwsgi 進程
1
|
sudo
pip
install
supervisor
|
Ubuntu用戶 請直接看 3,以下是CentOS 注意事項:
CentOS下,如果不是非常懂 SELinux 和 iptables 的話,為了方便調試,可以先臨時關閉它們,如果發現部署了之后出不來結果,可以臨時關閉測試一下,這樣就知道是不是 SELinux 和 iptables 的問題
CentOS 7 iptables如何使用:http://stackoverflow.com/questions/24756240/
將 SELinux 設置為寬容模式,方便調試:
1
|
sudo
setenforce 0
|
防火牆相關的設置:
1
2
3
4
5
|
可以選擇臨時關閉防火牆
sudo
service iptables stop
或者開放一些需要的端口,比如 80
sudo
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
|
上面的兩條命令,如果是 CentOS 7 用
1
2
3
4
5
6
|
臨時關閉防火牆
sudo
systemctl stop firewalld
或者 開放需要的端口
sudo
firewall-cmd --zone=public --add-port=80
/tcp
--permanent
sudo
firewall-cmd --reload
|
備注:由於我還沒有用 最新版本的 Fedora ,新版 Fedora 需要用 dnf 來安裝包,有需求的同學自測,可以參考這里。
3. 使用 uwsgi 來部署
安裝 uwsgi
1
|
sudo
pip
install
uwsgi --upgrade
|
使用 uwsgi 運行項目
1
|
uwsgi --http :8001 --chdir
/path/to/project
--home=
/path/to/env
--module project.wsgi
|
這樣就可以跑了,--home 指定virtualenv 路徑,如果沒有可以去掉。project.wsgi 指的是 project/wsgi.py 文件
如果提示端口已經被占用:
1
2
|
probably another instance of uWSGI is running on the same address (:8002).
bind(): Address already in use [core/socket.c line 764]
|
這時可以把相關的進程 kill 掉:
按照端口進行查詢:
1
|
lsof
-i :8002
|
可以查出:
1
2
3
|
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
uwsgi 2208 tu 4u IPv4 0x53492abadb5c9659 0t0 TCP *:teradataordbms (LISTEN)
uwsgi 2209 tu 4u IPv4 0x53492abadb5c9659 0t0 TCP *:teradataordbms (LISTEN)
|
這時根據 PID 可以用下面的命令 kill 掉相關程序:
1
|
sudo
kill
-9 2208 2209
|
按照程序名稱查詢:
1
|
ps
aux |
grep
uwsgi
|
補充內容:
使用 gunicorn 代替 uwsgi 的方法
1
2
3
4
|
sudo
pip
install
gunicorn
在項目目錄下運行下面的命令進行測試:
gunicorn -w4 -b0.0.0.0:8001 zqxt.wsgi
|
-w 表示開啟多少個worker,-b 表示要使用的ip和port,我們這里用的是 8001,0.0.0.0代表監控電腦的所有 ip。
如果使用了 virtualenv 可以這樣
1
|
/path/to/env/bin/gunicorn
--chdir
/path/to/project
--pythonpath
/path/to/env/
-w4 -b0.0.0.0:8017 project.wsgi:application
|
用 --pythonpath 指定依賴包路徑,多個的時候用逗號,隔開,如:'/path/to/lib,/home/tu/lib'
4. 使用supervisor來管理進程
安裝 supervisor 軟件包
1
|
(sudo) pip install supervisor
|
生成 supervisor 默認配置文件,比如我們放在 /etc/supervisord.conf 路徑中:
1
|
(
sudo
) echo_supervisord_conf >
/etc/supervisord
.conf
|
打開 supervisor.conf 在最底部添加(每一行前面不要有空格,防止報錯):
1
2
3
4
5
6
7
|
[program:zqxt]
command=/path/to/uwsgi --http :8003 --chdir /path/to/zqxt --module zqxt.wsgi
directory=/path/to/zqxt
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
|
command 中寫上對應的命令,這樣,就可以用 supervisor 來管理了。
啟動 supervisor
1
|
(
sudo
) supervisord -c
/etc/supervisord
.conf
|
重啟 zqxt 程序(項目):
1
|
(
sudo
) supervisorctl -c
/etc/supervisord
.conf restart zqxt
|
啟動,停止,或重啟 supervisor 管理的某個程序 或 所有程序:
1
|
(
sudo
) supervisorctl -c
/etc/supervisord
.conf [start|stop|restart] [program-name|all]
|
以 uwsgi 為例,上面這樣使用一行命令太長了,我們使用 ini 配置文件來搞定,比如項目在 /home/tu/zqxt 這個位置,
在其中新建一個 uwsgi.ini 全路徑為 /home/tu/zqxt/uwsgi.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[uwsgi]
socket =
/home/tu/zqxt/zqxt
.sock
chdir =
/home/tu/zqxt
wsgi-
file
= zqxt
/wsgi
.py
touch
-reload =
/home/tu/zqxt/reload
processes = 2
threads = 4
chmod
-socket = 664
chown
-socket = tu:www-data
vacuum =
true
|
注意上面的 /home/tu/zqxt/zqxt.sock ,一會兒我們把它和 nginx 關聯起來。
在項目上新建一個空白的 reload 文件,只要 touch 一下這個文件(touch reload) 項目就會重啟。
注意:不建議把 sock 文件放在 /tmp 下,比如 /tmp/xxx.sock (不建議)!有些系統的臨時文件是 namespaced 的,進程只能看到自己的臨時文件,導致 nginx 找不到 uwsgi 的 socket 文件,訪問時顯示502,nginx 的 access log 中顯示 unix: /tmp/xxx.sock failed (2: No such file or directory),所以部署的時候建議用其它目錄來放 socket 文件,比如放在運行nginx用戶目錄中,也可以專門弄一個目錄來存放 sock 文件,比如 /tmp2/
1
2
|
sudo
mkdir
-p
/tmp2/
&&
sudo
chmod
777
/tmp2/
然后可以用
/tmp2/zqxt
.sock 這樣的路徑了
|
修改 supervisor 配置文件中的 command 一行:
1
2
3
4
|
[program:zqxt]
command=/path/to/uwsgi --ini /home/tu/zqxt/uwsgi.ini
directory=/path/to/zqxt
startsecs=0
|
然后重啟一下 supervisor:
1
2
3
|
(
sudo
) supervisorctl -c
/etc/supervisord
.conf restart zqxt
或者
(
sudo
) supervisorctl -c
/etc/supervisord
.conf restart all
|
5. 配置 Nginx
新建一個網站 zqxt
1
|
sudo
vim
/etc/nginx/sites-available/zqxt
.conf
|
寫入以下內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
server {
listen 80;
server_name www.ziqiangxuetang.com;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /path/to/project/media;
}
location /static {
alias /path/to/project/static;
}
location / {
uwsgi_pass unix:///home/tu/zqxt/zqxt.sock;
include /etc/nginx/uwsgi_params;
}
}
|
激活網站:
1
|
sudo
ln
-s
/etc/nginx/sites-available/zqxt
.conf
/etc/nginx/sites-enabled/zqxt
.conf
|
測試配置語法問題
1
|
sudo
service nginx configtest 或
/path/to/nginx
-t
|
重啟 nginx 服務器:
1
|
sudo
service nginx reload 或
sudo
service nginx restart 或
/path/to/nginx
-s reload
|
一些有用的參考教程:
Django 官網部署教程:
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/gunicorn/
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/uwsgi/
nginx 與 socket
防火牆:
centos 7 FireWalld: http://stackoverflow.com/questions/24756240/how-can-i-use-iptables-on-centos-7
ubuntu ufw 防火牆:http://wiki.ubuntu.org.cn/Ufw%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97
uwsgi ini 配置文件:http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html#configuring-uwsgi-to-run-with-a-ini-file