【前置條件】html
建立1個flask-demo,生成requirement.txt文件(下載好gunicorn),上傳至git。建立demo參照:建立一個flask api-demo(響應體顯示中文)java
git地址:https://github.com/wangju003/flaskDemo.gitpython
先在本地試試用gunicorn啓動falsklinux
在terminal中執行gunicorn命令經過wsgi文件啓動flask:nginx
gunicorn -w 4 -b 127.0.0.1:5000 wsgi:application
啓動效果:git
經過git將demo clone至centOS,項目目錄建議放在 /home/www/(放在哪裏沒必要糾結,由於最終會經過jenkins自動佈署Job,因此項目會存放在/.jenkins目錄下。這裏只作測試gunicorn+nginx啓動falsk)
github
【正式配置】web
環境:centOS7flask
tips:若是安裝其它環境,須要java環境,不須要特別安裝jdk,使用自帶的openJDK就能夠。openJDK不能知足條件時,再安裝jdk,且無須自本身配置環境變量(配置過,有印象是這樣的)vim
step1:安裝python3,pip3,python虛擬環境,我這些在以前環境佈署之已經安裝好,這裏再也不綴述。
具體搭建過程,參照:
centOS安裝python3 以及解決 導入ssl包出錯的問題
step2:
1 [root@67 flaskDemo]# mkvirtualenv flaskApi #建立虛擬環境 3 (flaskApi) [root@67 flaskDemo]# pip install -r requirements.txt#安裝依賴
里程碑,經過gunicorn運行flask:
首先查看一下服務器的ip地址,記錄下來:10.2.1.92
1 (flaskApi) [root@67 flaskDemo]# ifconfig 2 ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 3 inet 10.2.1.92 netmask 255.255.255.0 broadcast 10.2.1.255
經過gunicorn運行wsgi.py文件啓動flask,ip地址用上一步查詢獲得的10.2.1.92,端口號指定爲5000(自定義)
1 (flaskApi) [root@67 flaskDemo]# ls 2 app manage.py requirements.txt settings.py wsgi.py 3 (flaskApi) [root@67 flaskDemo]# gunicorn -w 4 -b 10.2.1.92:5000 wsgi:application
啓動後:
(flaskApi) [root@67 flaskDemo]# gunicorn -w 4 -b 10.2.1.92:5000 wsgi:application [2019-09-17 14:18:31 +0800] [8945] [INFO] Starting gunicorn 19.9.0 [2019-09-17 14:18:31 +0800] [8945] [INFO] Listening at: http://10.2.1.92:5000 (8945) [2019-09-17 14:18:31 +0800] [8945] [INFO] Using worker: sync [2019-09-17 14:18:31 +0800] [8948] [INFO] Booting worker with pid: 8948 [2019-09-17 14:18:31 +0800] [8949] [INFO] Booting worker with pid: 8949 [2019-09-17 14:18:31 +0800] [8950] [INFO] Booting worker with pid: 8950 [2019-09-17 14:18:31 +0800] [8952] [INFO] Booting worker with pid: 8952
打開網址驗證一下:此時應該能夠展現響應內容了:
step3:安裝nginx
yum -y install nginx
遇到的問題:
提示沒有可用的軟件包
解決辦法:
Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟件包
啓動nginx
(flaskApi) [root@67 flaskDemo]# systemctl start nginx
遇到的問題:
1 Redirecting to /bin/systemctl start nginx.service
2 Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
解決辦法:
centos7 ngxin啓動失敗:Job for nginx.service failed(80端口被佔用的解決辦法)
中止 nginx 服務
(flaskApi) [root@67 flaskDemo]# systemctl stop nginx
重啓 nginx 服務
systemctl restart nginx
平滑啓動:
nginx配置改動了,能夠從新加載而不用先關閉再打開
nginx -s reload
啓動後 ,在瀏覽器中 輸入服務器的 ip 地址,就能夠看到--記得加上指定nginx啓動的端口號(我修改了nginx啓動端口爲8001)
http://10.2.1.92:8001
到這裏 yum
安裝 nginx
就完成了
step4:配置nginx
1 server { 2 listen 80 default_server; 3 listen [::]:80 default_server; 4 server_name gogotest.com; 5 root /usr/share/nginx/html; 6 7 # Load configuration files for the default server block. 8 include /etc/nginx/default.d/*.conf; 9 10 location / { 11 proxy_pass http:/http://10.2.1.92:5000; 12 proxy_set_header Host $host; 13 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 14 }
里程碑:
配置好後打開本地瀏覽器,輸入域名,應該就能訪問了
step5:安裝superversion
step6:配置superversion,注意command後的參數是,虛擬環境xxx,gunicorn的啓動命令
里程碑:使用superversion啓動gunicorn,輸入配置地址能夠正常訪問
step7:安裝fabric(不必定要配置吧?是否是能夠用jenkins-webhoo提交代碼就自動佈署呢)能夠體驗一下看看
里程碑:
首先修改respone內容,而後在本地執行命令:fab deploy
而後訪問測試網址,查看響應內容是否是更新了
明天從這裏開始吧
https://pdf-lib.org/Home/Details/8536
如今的問題是在centos上經過gunicorn啓動服務後,外網不能訪問
如今試一下uwsgi的方式
https://www.jianshu.com/p/be2b587a900e
問題:
Failed to stop iptalbes.service: Unit iptalbes.service not loaded.
https://blog.csdn.net/c233728461/article/details/52679558
參考文檔:
https://www.jianshu.com/p/da28ec28ef4b
https://www.cnblogs.com/mqxs/p/8706665.html(ip地址要寫服務器地址的坑我踩過了)
[root@67 nginx]# nginx -t nginx: [emerg] unexpected "}" in /etc/nginx/nginx.conf:51 nginx: configuration file /etc/nginx/nginx.conf test failed [root@67 nginx]#
linux什麼編輯器能夠顯示行號
再次測試:
[root@67 nginx]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful