寫在前面關(guān)于配置網(wǎng)絡(luò)IP
1、查看當(dāng)前 IP地址及網(wǎng)卡名等信息
ifconfig
2盒至、在netplan中配置ip
用Vim打開配置文件
sudo vim /etc/netplan/50-cloud-init.yaml
配置完畢后用
sudu netplan apply
具體操作請見另一篇關(guān)于《Ubuntu 18.04 Server 配置靜態(tài)IP》
一、安裝及配置virtualenv
sudo apt install virtualenv
二乡小、安裝pip
Ubuntu server 18 默認(rèn)已經(jīng)安裝了python3拾碌,所以直接安裝pip3
sudo apt install python3-pip
升級pip
pip3 install --upgrade pip
三、建立virtualenv
默認(rèn)已經(jīng)安裝了python3.6耐量,所以指定。virtualenv默認(rèn)是對應(yīng)python2滤港,如果不加參數(shù)廊蜒,會報錯。
virtualenv -p /usr/bin/python3.6 venv
激活虛擬環(huán)境
source venv/bin/activate
此外溅漾,退出虛擬環(huán)境的命令為:
deactivate
四山叮、安裝flask
用pip安裝flask,指令如下:
pip install flask
五添履、安裝Nginx
sudo apt install nginx
安裝即可使用屁倔,默認(rèn)頁面80端口
六、安裝gunicorn
pip install gunicorn
七暮胧、安裝FTP
1锐借、安裝FTP Server
sudo apt install vsftpd
安裝即可使用,系統(tǒng)的用戶即可登錄
2往衷、開啟可寫權(quán)限
先備份vsftpd.conf钞翔,然后以管理員身份用vim打開vsftpd.conf。
不同版本的vsftpd的vsftpd.conf目錄不同炼绘,在本版本就在/etc/下
sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.bak
sudo vim /etc/vsftpd.conf
vim文本編輯器打開了conf文件嗅战,進行配置打開“寫”權(quán)限。vim操作參看我的另一篇《Vim常用操作》俺亮,就此不提。
# Uncomment this to allow local users to log in.
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES #取消前面的#號
3疟呐、重啟服務(wù)
sudo service vsftpd restart
4脚曾、小插曲
重啟后,用FileZilla連接启具,總是出現(xiàn)“嘗試連接“ECONNREFUSED - 連接被服務(wù)器拒絕”失敗本讥。”的錯誤。一開始以為自己配置文件時候出了錯誤拷沸,后來查了網(wǎng)上的資料色查,發(fā)現(xiàn)在FTP客戶端上,更改FileZilla的傳輸協(xié)議為 SFTP ,發(fā)現(xiàn)居然就OK了撞芍。就此不提秧了。
八、測試Flask
1序无、新建Web根目錄
新建了一個wwwroot
目錄验毡,我習(xí)慣就把他和venv平行了。
2帝嗡、新建項目
新建了一個helloflask項目晶通,上傳至wwwroot下
3、運行g(shù)unnicorn
gunicorn -w 4 -b 0.0.0.0:8080 main:app
# helloflask是啟動文件名哟玷,也就是main.py狮辽,app是程序中的應(yīng)用程序名(app = Flask(__name__))
九、配置Nginx
Nginx默認(rèn)的安裝目錄為:/etc/nginx/
編輯配置文件conf
/etc/nginx/sites-available/default
在此之前最好備份一下巢寡,操作指令如下
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak
然后用vim編輯它隘竭。
sudo vim /etc/nginx/sites-available/default
在local中增加
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Ubantu系統(tǒng)下Nginx啟動和停止的命令
sudo service nginx start
sudo service nginx stop
重新啟動之后,就可以用http://127.0.0.1/
訪問到gunicorn的http://127.0.0.1:8080
了讼渊。
九动看、Supervisor
1、安裝supervisor
pip install supervisor
2爪幻、 導(dǎo)出配置文件
隨便導(dǎo)到什么地方都可以的菱皆,但我習(xí)慣把它導(dǎo)導(dǎo)了venv下。
echo_supervisord_conf > supervisor.conf # 生成 supervisor 默認(rèn)配置文件
3挨稿、編輯配置文件
vim supervisor.conf
在文件的最后增加以下信息:
注意FlaskApp``main``app
和路徑等仇轻,記得要和調(diào)整成實際情況
[program:FlaskApp]
command=/home/ubuntu/venv/bin/gunicorn -w 4 -b 0.0.0.0:8080 app:app ; supervisor啟動命令
directory=/home/ubuntu/wwwroot/ ; 項目的文件夾路徑
startsecs=0 ; 啟動時間
stopwaitsecs=0 ; 終止等待時間
autostart=true ; 是否自動啟動
autorestart=true ; 是否自動重啟
stdout_logfile=/home/ubuntu/venv/log/gunicorn.log ; log 日志,記得要建立log目錄
stderr_logfile=/home/ubuntu/venv/log/gunicorn.err ; 錯誤日志,記得要建立log目錄
4 奶甘、啟動Supervisor
supervisord -c supervisor.conf
supervisor的基本使用命令
supervisord -c supervisor.conf 通過配置文件啟動supervisor
supervisorctl -c supervisor.conf status 察看supervisor的狀態(tài)
supervisorctl -c supervisor.conf reload 重新載入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname] 啟動指定/所有 supervisor管理的程序進程
supervisorctl -c supervisor.conf stop [all]|[appname] 關(guān)閉指定/所有 supervisor管理的程序進程
supervisor 還有一個web的管理界面篷店,可以激活。具體如下:
[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
username=user ; (default is no username (open server))
password=123 ; (default is no password (open server))
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
username=user ; should be same as http_username if set
password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
其他命令
有時候會出現(xiàn)服務(wù)占用的莫名問題臭家,我自己也沒搞清楚疲陕,但查了資料,可以運行下述命令斷開連接钉赁,然后再運行即可蹄殃。
sudo unlink /tmp/supervisor.sock
sudo unlink /var/run/supervisor.sock
十、設(shè)置為開機自啟動
目前每次重啟服務(wù)器后你踩,還需要
1诅岩、激活venv
2讳苦、運行supervisor
我想著能夠開機自動啟動,直接運行該多好啊吩谦。
不過還沒學(xué)會鸳谜,容我偷會兒懶。
等我學(xué)會了再和大家分享式廷。
—— 最后更新于2020.03.28 北京