環境部署
-
在所在項目生成相關包的requirements.txt文件
pip freeze > requirements.txt
-
在雲主機上設置python虛擬環境,直接安裝
virtualenvwrapper
即可,https://virtualenvwrapper.readthedocs.io/en/latest/需要注意這裏的坑,需要使用pip3,不然後面配置python3找到這個腳本就會出現錯誤。
-
找到 virtualenvwrapper.sh的位置,添加環境變量
-
配置啓動文件
vim ~/.bashrc
,編輯完source ~/.bashrc
Add three lines to your shell startup file (
.bashrc
,.profile
, etc.) to set the location where the virtual environments should live, the location of your development project directories, and the location of the script installed with this package:- 我們只需設置虛擬環境應駐留的位置與此軟件包一起安裝的腳本的位置即可
- 我們只需設置虛擬環境應駐留的位置與此軟件包一起安裝的腳本的位置即可
-
workon
查看有沒有虛擬環境,
mkvirtual + 環境名
創建環境
deactivate : 退出虛擬環境
workon + 環境名:進入虛擬環境
- 進入虛擬環境後,根據requirements.txt安裝項目所需的環境
(cov) [root@chenxiaojian 疫情]# pip3 install -r requirements.txt
開發模式部署
-
項目是一個flask項目,將app.run(host=“0.0.0.0”,port=8000)指定所有主機和端口訪問
-
開放雲服務器的8000端口
-
主機ip:8000進行訪問
生產模式部署
-
安裝nginx,進行反向代理(也就是我們訪問ip地址,默認是80端口,然後nginx反向代理到本機的8000端口,也就是我們的web項目。)
yum -y install nginx
-
安裝gunicorn,進入虛擬環境中安裝
pip3 install gunicorn
-
用gunicorn啓動app.py(第一個app指的是app.py,第二個app指的是flask實例的對象)
(cov) [root@chenxiaojian 疫情]# gunicorn -b 127.0.0.1:8000 -D app:app (cov) [root@chenxiaojian 疫情]# ps aux | grep gunicorn root 1444 0.3 0.8 222220 16640 ? S 09:54 0:00 /root/.virtualenvs/cov/bin/python /root/.virtualenvs/cov/bin/gunicorn -b 127.0.0.1:8000 -D app:app root 1525 0.0 0.0 112728 976 pts/0 R+ 09:54 0:00 grep --color=auto gunicorn root 14988 0.0 0.8 222476 16664 ? S 09:45 0:00 /root/.virtualenvs/cov/bin/python /root/.virtualenvs/cov/bin/gunicorn -b 127.0.0.1 -D app:app root 14991 0.2 1.5 247864 29384 ? S 09:45 0:01 /root/.virtualenvs/cov/bin/python /root/.virtualenvs/cov/bin/gunicorn -b 127.0.0.1 -D app:app (cov) [root@chenxiaojian 疫情]#
啓動nginx服務失敗,查看錯誤,顯示端口80被佔用,netstat -ltunp
查看,但是我的端口明明被nginx佔用了,只能查看進程,並kill。
-
/etc/nginx/nginx.conf
需要注意:gunicorn啓動的端口號要和nginx配置的端口號要一樣8000.
(server_name:填寫自己的ip或者域名)
查看nginx配置是否正常
(cov) [root@chenxiaojian 疫情]# nginx -t -c /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
-
訪問ip或者域名成功!