寫在前面關于配置網絡IP
1、查看當前 IP地址及網卡名等信息
ifconfig
2、在netplan中配置ip
用Vim打開配置文件
sudo vim /etc/netplan/50-cloud-init.yaml
配置完畢后用
sudu netplan apply
具體操作請見另一篇關于《Ubuntu 18.04 Server 配置靜態IP》
一、安裝及配置virtualenv
sudo apt install virtualenv
二、安裝pip
Ubuntu server 18 默認已經安裝了python3,所以直接安裝pip3
sudo apt install python3-pip
升級pip
pip3 install --upgrade pip
三、建立virtualenv
默認已經安裝了python3.6,所以指定。virtualenv默認是對應python2,如果不加參數,會報錯。
virtualenv -p /usr/bin/python3.6 venv
激活虛擬環境
source venv/bin/activate
此外,退出虛擬環境的命令為:
deactivate
四、安裝flask
用pip安裝flask,指令如下:
pip install flask
五、安裝Nginx
sudo apt install nginx
安裝即可使用,默認頁面80端口
六、安裝gunicorn
pip install gunicorn
七、安裝FTP
1、安裝FTP Server
sudo apt install vsftpd
安裝即可使用,系統的用戶即可登錄
2、開啟可寫權限
先備份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文件,進行配置打開“寫”權限。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、重啟服務
sudo service vsftpd restart
4、小插曲
重啟后,用FileZilla連接,總是出現“嘗試連接“ECONNREFUSED - 連接被服務器拒絕”失敗。”的錯誤。一開始以為自己配置文件時候出了錯誤,后來查了網上的資料,發現在FTP客戶端上,更改FileZilla的傳輸協議為 SFTP ,發現居然就OK了。就此不提。
八、測試Flask
1、新建Web根目錄
新建了一個wwwroot
目錄,我習慣就把他和venv平行了。
2、新建項目
新建了一個helloflask項目,上傳至wwwroot下
3、運行gunnicorn
gunicorn -w 4 -b 0.0.0.0:8080 main:app
# helloflask是啟動文件名,也就是main.py,app是程序中的應用程序名(app = Flask(__name__))
九、配置Nginx
Nginx默認的安裝目錄為:/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系統下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、 導出配置文件
隨便導到什么地方都可以的,但我習慣把它導導了venv下。
echo_supervisord_conf > supervisor.conf # 生成 supervisor 默認配置文件
3、編輯配置文件
vim supervisor.conf
在文件的最后增加以下信息:
注意FlaskApp``main``app
和路徑等,記得要和調整成實際情況
[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的狀態
supervisorctl -c supervisor.conf reload 重新載入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname] 啟動指定/所有 supervisor管理的程序進程
supervisorctl -c supervisor.conf stop [all]|[appname] 關閉指定/所有 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
其他命令
有時候會出現服務占用的莫名問題,我自己也沒搞清楚,但查了資料,可以運行下述命令斷開連接,然后再運行即可。
sudo unlink /tmp/supervisor.sock
sudo unlink /var/run/supervisor.sock
十、設置為開機自啟動
目前每次重啟服務器后,還需要
1、激活venv
2、運行supervisor
我想著能夠開機自動啟動,直接運行該多好啊。
不過還沒學會,容我偷會兒懶。
等我學會了再和大家分享。
—— 最后更新于2020.03.28 北京