centos7下部署flask+Nginx+Gunicorn
步骤如下:
1.下载gunicorn
直接使用命令:
# pip3 install gunicorn
同时把flask也下载了:
# pip3 install flask
注:没有使用pip,因为选的是华为云服务器centos7,自带的python为2.7版本,但是同样有python 3.6版本,需要使用python3来唤起,所以我就直接用的pip3。
2.将gunicorn加入到app.run()中
我在/tmp/flask_test下新建了app.py作为入口函数。
(其实我是直接在Windows下写好代码在传输到服务器上,flask_test目录下同时还有目录static,templates):代码为:
from flask import Flask,request
from flask.templating import render_template
app = Flask(__name__)
@app.route('/',methods=['GET','POST'])
def test():
if request.method =='GET':
print('hello flask')
return render_template('test.html')
if request.method == 'POST':
return {'1':'hahah','2':'xixixixi'}
if __name__ == '__main__':
app.run(debug='True',host='0.0.0.0')
其中test.html,该文件在templates目录下
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<body>
<h1>你好</h1>
</body>
</html>
3.python虚拟环境
使用命令进行安装:
pip3 install virtualenv
对于要操作的项目,可以创建一个虚拟环境目录,比如:
mkdir flaskproject
cd flaskproject
virtualenv flaskprojectenv
进行激活:
source flaskprojectenv/bin/activate
注:退出虚拟环境使用的命令为:deactivate
3.使用命令启动gunicorn
方式一:
另开一个服务器连接会话,进入到app.py所在目录下,输入命令:
# gunicorn app:app
(第一个app指的是app.py的文件名)
注意:一定要在app.py的路径下
这个时候,我们就可以在之前的会话(输入了命令source flaskprojectenv/bin/activate
的会话下)输入:
curl http://127.0.0.1:8000
检查本地服务器是否运行,可以看到弹出test.html的内容。
方式二:
该方式是为了处理高并发,所以打开多个进程和修改监听端口的方式:
gunicorn -w 4 -b 127.0.0.1:8000 入口文件名:app
这样我们本地服务器就已经运行起来了,但是远程并不能访问。
4.下载Nginx
1.再开个会话,下载nginx包
# wget http://nginx.org/download/nginx-1.10.1.tar.gz
2.复制到安装目录
# cp nginx-1.10.1.tar.gz /usr/local/
3.cd进入到/usr/local/,然后进行解压
# tar -zxvf nginx-1.10.1.tar.gz
4.网上教程直接说这里就可以启动nginx了,但是,/usr/local下并没有nginx文件夹,只有nginx-1.10.1,所以要先执行安装步骤为:
进入到nginx-1.10.1目录下,输入命令:
# ./configure
# make
# make install
make中可能遇到的问题
- ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre= option.
解决办法:安装pcre-devel
# yum -y install pcre-devel
- ./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib= option.
解决办法:安装zlib-devel
# yum install -y zlib-devel
下载完后,再输入./configure 再执行make 和make install 即可。
安装完成后,最好查看一下是否成功
# whereis nginx
这样我们就知道了nginx所在目录为/usr/local/nginx
我们cd进入到nginx目录,再输入sbin/nginx
即可启动nginx
配置nginx对应到项目:
先本地测试 curl http://192.168.0.245/ 这里的ip地址是本机的ip地址,然后就会有nginx的html
这样就说明了本地是安装成功的。
下面进行防火墙和端口的设置,
cd进入到/usr/local/nginx/conf/,输入命令:
vi nginx.conf
找到server,修改为:
server {
listen 80;
server_name xxx.xxx.xxx.xxx; #对应到自己服务器的公网ip或者域名
#charset koi8-r;
#access_log logs/host.access.log main;
root /tmp/flask_test; #自己项目所在位置
location / {
#gunicorn和nginx交互的端口
proxy_pass http://127.0.0.1:8000;
# 请求转发到多个gunicorn服务器
# proxy_pass http://flask;
# 设置请求头,并将头信息传递给服务器端
proxy_set_header Host $host;
# 设置请求头,传递原始请求ip给 gunicorn 服务器
proxy_set_header X-Real-IP $remote_addr;
}
修改之后,重新启动nginx,即可在Windows下通过外网访问到该网页。
即输入服务器公网ip或者域名即可访问网页。
重启命令为:在nginx目录下输入sbin/nginx -s reload
msql8.0的安装
- 安装:
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
sudo yum --enablerepo=mysql80-community install mysql-community-server
- 启动mysql服务
sudo service mysqld start
4.查看root初始的密码,mysql8.0必须要你修改之后才能进行数据库操作
cat /var/log/mysqld.log | grep 'temporary password'
5.复制初始默认密码,进入mysql数据库mysql -uroot -p
6.修改密码:要求大小写,数字,符号
ALTER USER 'root'@'localhost' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY 'xxxxxxxx';
- 然后就可以导入.sql文件存储数据库,步骤为:
进入数据库,
create database db1;
use db1;
source .sql文件位置
nohup
使用命令:这样保证关闭会话之后,服务器还能运行这个进程
nohup gunicorn -w 4 -b 127.0.0.1:8000 app:app >>/var/log/flask.log 2>&1 &
下方会提示该进程pid,想要杀死可以使用kill -9 进程号
但是如果关闭了该session的话,就不怎么好找了,我现在知道的办法就是重启,然后打开nginx,重新跑nohup