ubuntu環境部署專案
先安裝 apt-get中 需要安裝的包,然後再安裝 pip中的包
apt-get中需要安裝的包:
sudo apt-get install python3.6-dev
sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install redis-server
部署虛擬環境
現在非虛擬環境下安裝好python3.6
sudo pip install virtualenv #安裝虛擬環境需要的包
virtualenv -p /usr/bin/python3.6 venv #建立帶有python3.6的虛擬環境(自動包含pip對應版本)
virtualenv -p /usr/bin/python2.7 venv #建立帶有python2.7的虛擬環境(自動包含pip對應版本,安裝supervisor時需要)
cd venv #進入到虛擬環境的目錄
source bin/activate #進入虛擬環境 成功後,命令列開頭 有 (venv)
安裝pip的包
pip install -r file.txt #安裝pip相關包,先進入虛擬環境
安裝supervisor
./bin/pip2.7 install supervisor #通過pip2.7安裝 superviosr(因為supervisor不支援python3)
echo_supervisord_conf > supervisord.conf #生成superviosr配置檔案 https://blog.csdn.net/wr166/article/details/79051725 在python2的環境下用supervisor來執行python3的web專案
vi supervisor.conf #開啟後,輸入相關配置
supervisord -c supervisord.conf #通過配置檔案啟動supervisor服務
deactivate #退出虛擬環境 # https://blog.csdn.net/charlie_heng/article/details/60573688 supervisor 在python3下的簡易解決方案
防火牆/埠 開啟和關閉
開啟防火牆: ufw enable
關閉防火牆:ufw disable
開啟防火牆的埠 :ufw allow 埠號; 如:ufw allow 5000
關閉防火牆的埠:ufw deny 埠號;
重啟防火牆:ufw reload
檢視防火牆埠的狀態: ufw status
測試遠端主機的埠是否開啟: telnet 192.168.1.103 80
啟動服務並外網可以訪問:
supervisor 中的配置檔案:
[program:fws] #專案名directory= /home/ubuntu/fws #專案位置environment = PATH='home/ubuntu/venv/bin' #環境command= /home/ubuntu/venv/bin/python3.6 run.py #執行命令autostart = true #自動啟動startsecs = 1autorestart = true #自動重啟stopasgroup = true killasgroup = trueuser = ubuntustdout_logfile = /home/ubuntu/fws/logs/supevisor.log #輸出日誌stderr_logfile = /home/ubuntu/fws/logs/supevisor_err.log #錯誤日誌
run.py檔案內容:
from fws import app
if __name__ == '__main__':
app.run(host='10.104.136.123',port=5000) #host為內網地址,埠號要檢查外網是否可以訪問
nginx配置內容:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main escape=json '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_body"';
access_log /home/ubuntu/fws/logs/nginx.log main;
error_log /home/ubuntu/fws/logs/nginx_err.log;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
location / {
proxy_pass http://10.104.136.123:5000;
proxy_set_header X-Real-Ip $remote_addr;
}
location ^~ /index.html {
alias /home/web/fws/fws/static/dist/;
}
location /static {
alias /home/web/fws/fws/static/;
}
}
}
然後啟動 supervisor,和nginx即可
其他相關命令記錄
pip list --format freeze # pip 列表
pip install -r file.txt #pip批量安裝
pip -V #檢視對應的python編譯版本
whereis python3.6 #檢視安裝路徑
https://www.cnblogs.com/yjlch1016/p/8641910.html Ubuntu怎樣安裝Python3.6,pip
https://blog.csdn.net/San_South/article/details/80715682 Ubuntu16.04上pip報錯ModuleNotFoundError: No module named 'pip._internal'
https://blog.csdn.net/wangtaoking1/article/details/51554959 安裝Python mysqlclient出現“OSError: mysql_config not found”錯誤
https://blog.csdn.net/meteor_s/article/details/79115360 Error記錄--ImportError: No module named apt_pkg
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 需要根據python版本安裝對應的python-dev包,如apt-get install pyton3.6-dev