1、新建獨立運行環境,命名為env
1
2
3
4
5
6
|
[root@vultr ~] # mkdir projects # 測試的項目總目錄 [root@vultr ~] # pip3 install virtualenv [root@vultr ~] # cd projects [root@vultr projects] # virtualenv env --python=python3 --no-site-packages - - python:指定python版本 - - no - site - packages:不復制系統已安裝python包 |
2、激活虛擬環境
[root@vultr projects]# source env/bin/activate
執行后命令提示符前面會出現一個env,變成(env)[root@vultr opt]#,退出虛擬環境執行deactivate即可。
3、安裝項目依賴:
pip3 install, 在虛擬環境中安裝的包,不會對系統環境造成影響。
django項目配置
1、上傳django項目: hello項目
目錄結構:
1
2
3
4
|
hello / apps / hello / manage.py |
2、配置項目的數據庫信息:vi hello/hello/settings.py
如果是遠程服務器,需要修改setting.py文件中的allowed_hosts:
allowed_hosts = ['*']
3、數據遷移
(env)[root@vultr hello]# python3 manage.py makemigrations
(env)[root@vultr hello]# python3 manage.py migrate
4、收集靜態文件:vi hello/hello/settings.py
static_root = os.path.join(base_dir, "static")
:wq保存后,執行
(env)[root@vultr hello]# python3 manage.py collectstatic --noinput
5、用runserver啟動項目,看是否正常運行
(env)[root@vultr hello]# python3 manage.py runserver 0.0.0.0:8088
uwsgi配置
deactivate退出虛擬環境
1、安裝uwsgi
[root@vultr hello]# pip3 install uwsgi
2、命令行運行測試
在 項目目錄hello 下,執行以下命令:
[root@vultr hello]# uwsgi --http ip:端口 --home /root/env/ --file hello/wsgi.py --static-map=/static=static
--home:指定虛擬環境的目錄
wsgi.py:django創建項目時生成的文件
如果訪問url正常,說明python虛擬環境和uwsgi沒有問題.
3、使用ini配置文件來啟動uwsgi
我習慣性創建projects目錄,目錄結構如下:
1
2
3
4
5
6
7
|
/ root / projects / script / - - > 存放uwsgi相關的文件,例如uwsgi.ini, uwsgi.pid... hello / - - > 項目目錄 apps / - - > 應用程序目錄 hello / - - > settings.py等文件所在目錄 static / env / - - > 虛擬環境目錄 |
[root@vultr projects]# vi script/uwsgi.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
[uwsgi] # 項目目錄 chdir = / root / projects / hello / # 虛擬環境目錄 home = / root / projects / env / # 啟動uwsgi的用戶名和用戶組 uid = root gid = root # 指定項目的application module = hello.wsgi:application # 指定sock的文件路徑 socket = / root / projects / script / uwsgi.sock # 啟用主進程 master = true # 進程個數 workers = 5 pidfile = / root / projects / script / uwsgi.pid # 自動移除unix socket和pid文件當服務停止的時候 vacuum = true # 序列化接受的內容,如果可能的話 thunder - lock = true # 啟用線程 enable - threads = true # 設置自中斷時間 harakiri = 30 # 設置緩沖 post - buffering = 4096 # 設置日志目錄 daemonize = / root / projects / script / uwsgi.log |
4、后臺啟動停止uwsgi的命令
1
2
|
[root@vultr projects] # uwsgi --ini script/uwsgi.ini # 啟動 [root@vultr projects] # uwsgi --stop script/uwsgi.pid # 停止 |
nginx配置
1、 配置yum:
[root@vultr projects]# vi /etc/yum.repos.d/nginx.repo
1
2
3
4
5
6
7
|
[nginx] name = nginx repo # 下面這行centos根據你自己的操作系統修改比如:os/rehel # 6是你linux系統的版本,可以通過url查看路徑是否正確 baseurl = http: / / nginx.org / packages / centos / 6 / $basearch / gpgcheck = 0 enabled = 1 |
2、 安裝: yum -y install nginx
3、添加配置文件
[root@vultr projects]# vi /etc/nginx/conf.d/hello.conf # 名字是隨便起的,建議跟項目目錄一樣
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
server { listen 84 ; # 端口 server_name 10.129 . 205.183 ; # 域名 access_log / var / log / nginx / access.log main; charset utf - 8 ; gzip on; gzip_types text / plain application / x - javascript text / css text / javascript application / x - httpd - php application / json text / json image / jpeg image / gif image / png application / octet - stream; error_page 404 / 404.html ; error_page 500 502 503 504 / 50x .html; # 指定項目路徑uwsgi location / { include uwsgi_params; # 加載nginx和uwsgi的通信協議模塊 uwsgi_connect_timeout 30 ; # 超時時間 uwsgi_pass unix: / root / projects / script / uwsgi.sock; } # 指定靜態文件路徑 location / static / { alias / root / projects / hello / static / ; index index.html index.htm; } } |
4、啟動與停止nginx
檢查uwsgi是否啟動了
1
2
3
4
5
6
7
8
9
|
[root@vultr projects] # ps -ef | grep uwsgi root 2299 1 0 06 : 22 ? 00 : 00 : 00 uwsgi - - ini script / uwsgi.ini root 2301 2299 0 06 : 22 ? 00 : 00 : 00 uwsgi - - ini script / uwsgi.ini root 2302 2299 0 06 : 22 ? 00 : 00 : 00 uwsgi - - ini script / uwsgi.ini root 2303 2299 0 06 : 22 ? 00 : 00 : 00 uwsgi - - ini script / uwsgi.ini root 2304 2299 0 06 : 22 ? 00 : 00 : 00 uwsgi - - ini script / uwsgi.ini root 2305 2299 0 06 : 22 ? 00 : 00 : 00 uwsgi - - ini script / uwsgi.ini root 2306 2299 0 06 : 22 ? 00 : 00 : 00 uwsgi - - ini script / uwsgi.ini root 2361 2016 0 06 : 32 pts / 1 00 : 00 : 00 grep uwsgi |
啟動nginx
[root@vultr projects]# /etc/init.d/nginx start
訪問url,見證奇跡的時刻到了,然后...
ok,報錯了,莫慌。度娘查了502是服務器錯誤,然而前面測試了django+uwsgi沒問題,所以最有可能是在nginx出錯了。
來,我們查看一下nginx的錯誤日志文件,日志文件在哪呢???
1
2
3
|
[root@vultr projects] # find / -name nginx.conf / etc / nginx / nginx.conf [root@vultr projects] # vi /etc/nginx/nginx.conf |
error_log參數就是錯誤日志文件了,讓我們再打開error.log文件,找到最后一條記錄:
2019/05/12 06:41:43 [crit] 1514#1514: *2 connect() to unix:/root/projects/script/uwsgi.sock failed (13: permission denied) while connecting to upstream, ...(后面省略)
從 failed (13: permission denied) while connecting to upstream 可以看出是沒有權限???原因是我貪圖方便, 直接把項目文件以及uwsgi文件放在了/root/目錄下 !!!
好,修改路徑,先停止nginx和uwsgi,再修改路徑/root/projects/更改為/opt/projects/:
1
2
3
4
5
6
|
[root@vultr projects] # uwsgi --stop script/uwsgi.pid [root@vultr projects] # /etc/init.d/nginx stop 停止 nginx: [確定] [root@vultr projects] # cd .. [root@vultr ~] # mv projects /opt/ [root@vultr ~] # cd /opt/projects/ |
然后將script/uwsgi.ini和/etc/nginx/conf.d/hello.conf中關于路徑的都修改過來,修改好后,再次啟動uwsgi和nginx:
1
2
3
|
[root@vultr projects] # uwsgi --ini script/uwsgi.ini [uwsgi] getting ini configuration from script / uwsgi.ini [root@vultr projects] # /etc/init.d/nginx start |
正在啟動 nginx: [確定]
再次訪問url, 訪問正常。
多項目部署
利用virtualenv可以在服務器上配置多個python運行環境,因此根據nginx、uwsgi、virtualenv可以實現一個服務器上運行多個項目,且互不干擾。
首先我們先來了解一下nginx+uwsgi通信原理。
請求首先交給nginx,如果是靜態內容nginx就直接處理了,如果是動態內容就將請求交給uwsgi服務器,nginx和uwsgi之間是通過socket來通信的,通信協議就是/etc/nginx/conf.d/hello.conf里配置的uwsgi_params文件。
那么,現在我們來梳理一下,nginx是怎么知道uwsgi在哪里?通過什么和uwsgi做socket通信,回看/etc/nginx/conf.d/hello.conf文件:
原來是根據uwsgi_pass指定了nginx與uwsgi通信的socket文件路徑,看到這,就知道好辦了,一個項目配置一個uwsgi.ini文件和nginx.conf里的一個server,那既然需要部署多個項目,那就是多個uwsgi.ini和nginx.conf里的多個server。
好的,我們開始測試:
1、配置虛擬環境以及測試用runserver運行django項目是否正常。 我的目錄結構是:
1
2
3
4
5
6
7
|
opt / projects / hello / - - > 第一個django項目 env / - - > 第一個django項目的虛擬環境 world / - - > 第二個django項目 env_1 / - - > 第二個django項目的虛擬環境 script / - - > uwsig.ini等文件存放 |
2、配置world項目的uwsgi_world.ini文件
[root@vultr projects]# vi script/uwsgi_w.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
[uwsgi] # 項目目錄 chdir = / opt / projects / world / # 虛擬環境目錄 home = / opt / projects / env_1 / # 啟動uwsgi的用戶名和用戶組 uid = root gid = root # 指定項目的application module = world.wsgi:application # 指定sock的文件路徑 socket = / opt / projects / script / uwsgi_w.sock # 啟用主進程 master = true # 進程個數 workers = 5 pidfile = / opt / projects / script / uwsgi_w.pid # 自動移除unix socket和pid文件當服務停止的時候 vacuum = true # 序列化接受的內容,如果可能的話 thunder - lock = true # 啟用線程 enable - threads = true # 設置自中斷時間 harakiri = 30 # 設置緩沖 post - buffering = 4096 # 設置日志目錄 daemonize = / opt / projects / script / uwsgi_w.log |
3、配置nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# 可以分開多個配置文件,這里我放在同一個配置文件里 [root@vultr projects] # vi /etc/nginx/conf.d/hello.conf server { listen 84 ; # 端口,請注意端口 server_name 10.129 . 205.183 ; # 域名 access_log / var / log / nginx / access.log main; charset utf - 8 ; gzip on; gzip_types text / plain application / x - javascript text / css text / javascript application / x - httpd - php application / json text / json image / jpeg image / gif image / png application / octet - stream; error_page 404 / 404.html ; error_page 500 502 503 504 / 50x .html; # 指定項目路徑uwsgi location / { include uwsgi_params; # 加載nginx和uwsgi的通信協議模塊 uwsgi_connect_timeout 30 ; # 超時時間 uwsgi_pass unix: / opt / projects / script / uwsgi.sock; } # 指定靜態文件路徑 location / static / { alias / opt / projects / hello / static / ; index index.html index.htm; } } server { listen 86 ; # 端口,請注意端口 server_name 10.129 . 205.183 ; # 域名 access_log / var / log / nginx / access.log main; charset utf - 8 ; gzip on; gzip_types text / plain application / x - javascript text / css text / javascript application / x - httpd - php application / json text / json image / jpeg image / gif image / png application / octet - stream; error_page 404 / 404.html ; error_page 500 502 503 504 / 50x .html; # 指定項目路徑uwsgi location / { include uwsgi_params; # 加載nginx和uwsgi的通信協議模塊 uwsgi_connect_timeout 30 ; # 超時時間 uwsgi_pass unix: / opt / projects / script / uwsgi_w.sock; } # 指定靜態文件路徑 location / static / { alias / opt / projects / world / static / ; index index.html index.htm; } } |
4、啟動uwsgi和nginx,訪問兩個端口的url。 ok,訪問正常。
總結
以上所述是小編給大家介紹的python開發之nginx+uwsgi+virtualenv多項目部署教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!原文鏈接:https://juejin.im/post/5cd82e2c518825688d0aaec0