先安裝 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
安裝 nginx: https://www.cnblogs.com/EasonJim/p/7806879.html
部署虛擬環境
現在非虛擬環境下安裝好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 = 1 autorestart = true #自動重啓 stopasgroup = true killasgroup = true user = ubuntu stdout_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