linux 项目部署部署

 
(1)应用环境
 
mkdir myapp
cd myapp
新建run.py 脚本
from flask import Flask
app = Flask(__name__)
app.route('/')
def index():
return 'hello world':q
if __name__ =='__main__':
app.run()
 
 
 
(2)gunicorn配置
  • 安装gunicorn
pip install gunicorn
  • 启动gunicorn
gunicorn -D -w 3 -b 0.0.0.0:8000 run:app
D 表示后台运行
w 表示有3 个 工作线程(感觉有些类似 nginx 的 master-worker 模型)
b 指定ip 和端口
这里采用本机访问, 主要是为了使用nginx 进行代理, 方便管理 ,可以用0.0.0.0来让其他主机访问,但是记得要看后面的防火墙端口,记得开启这里的8000
run表存放 写着全局变量 app 的那个工程文件
在我们的这个工程中, 即包含 init.py 的那个文件
 
(3)supervisor配置
  • 安装:
pip install supervisor
  • 进到工程目录,在项目目录添加supervisor的配置文件,
echo_supervisord_conf > supervisord.conf
  • 会生成一个配置文件
vi supervisord.conf
添加信息:
[program:myapp] directory =/clh/myapps command =/clh/myapps/myenv/bin/gunicorn -w 4 -b 0.0.0.0:8000 run:app
myapp 自己起的项目名
directory运行项目所在的工程文件
command ,gunicorn 的启动命令
  • 配置文件启动
supervisord -c supervisor.conf
  • 查看supervisor状态
supervisorctl -c supervisor.conf status
 
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管理的程序进程
 
 
(4)nginx配置
 
  • nginx安装
apt-get install nginx
 
  • 查看nginx进程
ps aux|grep nginx
 
  • 首先先备份配置
sudo cp /etc/nginx/sites-available/default default.bak
 
  • 修改配置
sudo vi /etc/nginx/sites-available/default
Ctrl + O 保存
Ctrl + X 退出
把配置改为
server { listen 80; server_name 115.159.146.108; # 这是HOST机器的外部域名,用地址也行 location / { proxy_pass http://0.0.0.0:8000; # 这里是指向 gunicorn host 的服务地址 } }
这样启动之后80端口就可以转发到8000端口了
  • 检查配置
nginx -t
若出现
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
则配置成功
杀nginx进程
重启nginx
sudo service nginx restart
在已经运行了Gunicorn的前提下,在浏览器访问115.159.146.108就会出现Hello World!了
 
Ubuntu下Supervisor安装、配置和使用: https://my.oschina.net/u/2396236/blog/1594853
posted @ 2019-10-15 14:49  yjy888  阅读(182)  评论(0编辑  收藏  举报