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
安装 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