Flask Linux部署(HelloTest):Flask+Nginx+Gunicorn+Supervisor
云服务器部署Java web项目(JDK, Tomcat, MySQL)
目录
- 目录
- 打开要部署的项目
- 创建 python 虚拟环境
- 安装 python web 框架 —flask
- scipy error
- mysqldb error
- Gunicorn 部署 Flask
- Supervisor管理
- 若是遇到问题:Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting
- 配置 Nginx
这篇博客仅为部署流程,想了解nginx的作用的话推荐一个博客
,废话不多说,直接上教程。
打开要部署的项目
cd /home/jianxiang/pycharmSpace/customs_flask/
创建 python 虚拟环境
virtualenv 是 python 的一个神器。用来在一个系统中创建不同的 python 隔离环境。相互之间还不会影响,使用简单到令人发指。(我的工作路径是 /home/jianxiang/pycharmSpace/customs_flask/)
virtualenv venv
source venv/bin/activate //激活虚拟环境
效果如图所示:
安装 python web 框架 —flask
pip install flask
启动我们的项目:
python app.py
这时候会提示因为缺少一些module报错,那么接下来就是pip install *
scipy error
注意在安装scipy的时候可能会报错如下:
numpy.distutils.system_info.NotFoundError: no lapack/blas resources found
这个时候要作如下操作:
1、scipy需要依赖numpy包,先pip install numpy
2、用ls /usr/lib | grep blas
查看,缺少了一些libopenblas系统库(缺少libopenblas库),用sudo apt-get install libopenblas-dev
安装该库,安装后查看如下:
3、pip install scipy
一下,还是不行的话,执行sudo apt-get install liblapack-dev
4、pip install scipy
一下,还是不行的话,执行sudo apt-get install libatlas-dev
5、 这时pip install scipy
一下就可以了。
mysqldb error
ImportError: No module named 'MySQLdb'
解决方案:
pip install MySQL-python
接着继续pip install *
安装缺少的module,最后pip list
一下:
这时候再次启动我们的项目:
python app.py
访问http://precision:5000/task/,就可以显示网站了:
Gunicorn 部署 Flask
安装 gunicorn
pip install gunicorn
pip 是一个重要的工具,python 用来管理包。还有一个最佳生产就是每次使用 pip 安装的库,都写入一个 requirement 文件里面,既能知道自己安装了什么库,也方便别人部署时,安装相应的库。
pip freeze > requirements.txt
以后每次 pip 安装了新的库的时候,都需freeze 一次。
当我们安装好 gunicorn 之后,需要用 gunicorn 启动 flask,注意 flask 里面的name里面的代码启动了 app.run(),这个含义是用 flask 自带的服务器启动 app。
gunicorn -w8 --threads 12 -b0.0.0.0:5000 app:app
其中 gunicorn 的部署中,,-w 表示开启多少个 worker,-b 表示 gunicorn 开发的访问地址。
想要结束 gunicorn 只需执行 pkill gunicorn,有时候还的 ps 找到 pid 进程号才能 kill。可是这对于一个开发来说,太过于繁琐,因此出现了另外一个神器—supervisor,一个专门用来管理进程的工具,还可以管理系统的工具进程。
Supervisor管理
安装 supervisor
pip install supervisor
生成 supervisor 默认配置文件
echo_supervisord_conf > supervisor.conf
修改 supervisor 配置文件,添加 gunicorn 进程管理
vim supervisor.conf
修改为如下:
#这几行解注释(即删除每行的第一个;),同时将127.0.0.1改为0.0.0.0
[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0: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://0.0.0.0: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
创建存放log的文件夹
mkdir log
在文件最后加入如下:这里的[program:app],app为主程序的文件名。
注意路径,我这里为:/home/jianxiang/pycharmSpace/customs_flask
[program:app]
command=/home/jianxiang/pycharmSpace/customs_flask/venv/bin/gunicorn -w8 --threads 12 -b0.0.0.0:5000 app:app
directory=/home/jianxiang/pycharmSpace/customs_flask
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=true
stdout_logfile=/home/jianxiang/pycharmSpace/customs_flask/log/gunicorn.log
stderr_logfile=/home/jianxiang/pycharmSpace/customs_flask/log/gunicorn.err
现在可以使用 supervsior 启动 gunicorn啦。
运行命令
supervisord -c supervisor.conf
supervisorctl -c supervisor.conf status #察看supervisor的状态
supervisorctl -c supervisor.conf reload #重新载入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname] #启动指定/所有
访问 http://precision:5000/task/ 可以看见gunciron 启动的返回的页面了
这时候在执行命令:
supervisorctl -c supervisor.conf reload 重新载入 配置文件
supervisorctl -c supervisor.conf status 察看supervisor的状态
supervisorctl -c supervisor.conf start all
访问 http://127.0.0.1:9001 就可以得到 supervisor的web管理界面如下:(用户名密码为上述配置文件的user 123)
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管理的程序进程
若是遇到问题:Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting
请将配置文件复制到编辑器里,重新echo_supervisord_conf > supervisor.conf
生成新的配置文件,再对之进行修改vim supervisor.conf
(即将刚才复制到编辑器里的复制过来)。
实在不行的话就重新连接服务器,试一下supervisor的基本使用命令,我就是搞来搞去就好了,,,(说实话刚这个我试了二十多遍,然后写博客也在不停试错,程序员总是这样)
配置 Nginx
采用 apt-get方式安装最简单。运行 sudo apt-get install nginx。安装好的nginx的二进制文件放在 /usr/sbin/文件夹下面。而nginx的配置文件放在 /etc/nginx下面。
使用 supervisor 来管理 nginx。这里需要注意一个问题,linux的权限问题。nginx是sudo的方式安装,启动的适合也是 root用户,那么我们现在也需要用 root用户启动supervisor。增加下面的配置文件
[program:nginx]
command=/usr/sbin/nginx
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/ubuntu/flask/myproject-master/log/nginx.log
stderr_logfile=/home/ubuntu/flask/myproject-master/log/nginx.err
同样是执行上面执行过的命令:
supervisorctl -c supervisor.conf reload 重新载入 配置文件
supervisorctl -c supervisor.conf status 察看supervisor的状态
supervisorctl -c supervisor.conf start all
这时候你可能会看到如下界面(nginx是exited):
这时需要在nginx配置文件中修改如下:(注意路径)
/etc/nginx/sites-available/default
server {
listen 5001;
server_name localhost;
root /home/jianxiang/pycharmSpace/customs_flask;
access_log /home/jianxiang/pycharmSpace/customs_flask/log/nginx.log;
error_log /home/jianxiang/pycharmSpace/customs_flask/log/nginx.err;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header Host $host:9003;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
这时就可以通过访问http://precision:5001访问网站。