使用nginx+Gunicorn+Flask将Flask应用部署到服务器上

前言: 

在本地开发了一个flask项目,如何将其部署到互联网上?
目前有2种比较常见的方法:

  • Ningx+uwsgi
  • Ningx+Gunicorn

对于第一种,因为服务器上对应uwsgi版本的不同,或者环境的差异,作者花了很长时间都没调试成功。
使用第一种,可能会遇到的坑

  • 使用python3但是安装了python2版本的uwsgi
  • uwsgi默认链接服务器上的python2.7 
  • uwsgi安装后不能读入

转念一想,uwsgi这么坑,有没有可以替代它的。于是发现了Ningx+Gunicorn来部署Flask项目。可以说相当方便了。
之后会考虑上传到git实现自动部署,本地修改push到git上自动更新。

具体步骤

1.你需要拥有一个服务器,作者是通过SecureCRT远程连接上服务器的。

2.服务器上是默认有python2.7 和 python3.5的。但是没有pip

所以我们需要安装python3.5的pip

 sudo apt-get install python3.5-dev python3.5-pip python3.5-virtualenv

  上面如果安装不成功,百度如何安装python3.5 pip
  安装好后使用: 

pip3 install virtualenv 


3.构建项目独立环境(这个步骤理解为将这个项目所需的一切(python)相关基础内容,都放到一个单独的容器中去,这样改变这个容器中的
python相关配置,不会影响到服务器。)


将项目传入服务器:
假设目录结构如下   

/root/flaskweb/app.py  
  • 主要使用Ningx+Gunicorn配置flaskweb那么app.py需要做如下配置:
if __name__ == '__main__':
    from werkzeug.contrib.fixers import ProxyFix
    app.wsgi_app = ProxyFix(app.wsgi_app)
    app.run()

进入项目目录:

cd /root/flaskweb

创建环境:(作者用的是anaconda的python,如你用的是自带python3.5 这里路径改为你python3.5的路径)

不清楚路径可以用命令 whereis python3.5

virtualenv -p /root/anaconda3/bin/python3.6 venv

进入环境:

. venv/bin/activate

安装所需的python模块:

venv/bin/pip install flask

venv/bin/pip install pymysql

venv/bin/pip install gunicorn

测试Gunicorn:

gunicorn -w 4 -b 127.0.0.1:5000 app:app

其中

  • -w 4 表示4个进程 
  • -b 127.0.0.1:5000 表示flask应用使用5000端口,开放5000用于ningx连接
  • app:app 前者代表启动程序文件名, 后者为实例化对象命名即 app = Flask(__name__)

如果程序启动了,没有报错信息代表配置成功。ctrl+c退出。

4.nginx

1.安装

sudo apt-get install nginx

2.配置参数:

命令行输入 如果报错 查询下你的nginx安装目录

echo "server {
    listen 8080;                #你想服务器的端口
    server_name 你的服务器地址;  #例如 198.198.22.22
 
    location / {
        proxy_pass http://127.0.0.1:5000; #这个是Gunicorn与Ningx通信的端口。和Gunicorn的配置相同
	access_log /root/flaskweb/access.log;
	error_log  /root/flaskweb/error.log;
    }
  }" > /etc/nginx/conf.d/default.conf

启动服务:

建议从新链接一下服务器,按如此步骤运行:

cd /root/flaskweb
. venv/bin/activate
gunicorn -w 4 -b 127.0.0.1:5000 app:app

启动后 开启另一个服务器链接,输入:

sudo service nginx start

这样你便可以在互联网上   ‘你的服务器地址’:8080  访问到你的flask项目了。

后记

nginx命令

安装: sudo apt-get install nginx
启动:sudo service nginx start
重启:sudo service nginx restart
停止:sudo service nginx stop
测试:sudo service nginx configtest

查看端口使用情况:


使用命令:ps -aux | grep 端口

使用命令:netstat -apn

更新ubuntu官方源文件:

首先得进入 源文件的目录下。--百度

如果要换成163.或者阿里巴巴的,将echo "xx" xx内容替换成163源信息即可。

echo "#deb cdrom:[Ubuntu 16.04.1 LTS _Xenial Xerus_ - Release amd64 (20160719)]/ xenial main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## universe WILL NOT receive any review or updates from the Ubuntu security
## team.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial universe
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe" > sources.list

猜你喜欢

转载自blog.csdn.net/qq_41664845/article/details/81408682
今日推荐