Centos 7 使用 nginx+uwsgi 部署flask应用

原文地址

一、安装软件包

1、增加第三方扩展包软件源

yum install epel-release

2、为了管理和安装python和uwsgi需要以下软件包

yum install python-pip python-devel gcc nginx

3、建Python虚拟环境

#安装虚拟工具
pip install virtualenv
#新建文件径
mkdir ~/myproject

cd ~/myproject
#建虚拟环境
virtualenv myprojectenv
#启用虚拟环境
source myprojectenv/bin/activate

二、搭建Flask应用

1、安装flask \ uwsgi

pip install flask uwsgi

vi myapp.py

2、示例应用代码

from flask import Flask
application = Flask(__name__)

@application.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    application.run(host='0.0.0.0')

3、测试代码运行是否正常

(venv) [root@localhost myproject]# python myapp.py
 * Serving Flask app "myapp" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

4、创建 WSGI 接入点

新建一个wsgi.py文件。告诉 uWSGI 服务器怎么和我们的应用交互。

from myapp import application

if __name__ == "__main__":
    application.run()

三、配置uWSGI

1、测试uWSGI是否正常,也可以用浏览器打开测试

(venv) [root@localhost myproject]# uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi
*** Starting uWSGI 2.0.18 (64bit) on [Tue Apr  9 09:50:06 2019] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 09 April 2019 13:39:07
os: Linux-3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018
nodename: localhost.localdomain
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /home/lubaohui/myproject
detected binary path: /home/lubaohui/myproject/venv/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7257
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 0.0.0.0:8000 fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 2.7.5 (default, Oct 30 2018, 23:45:53)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1d238a0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1d238a0 pid: 5658 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 5658, cores: 1)

2、创建uWSGI配置文件myapp.ini

***文件开头 [uwsgi] ,指明了uWSGI服务将会引用此处配置。 module引用 wsgi.py 文件, 引用时去掉后缀。

***当我们测试时,发现uWSGI需要使用一个网络接口处理连接。但无论如何我们将使用Ngnix处理实际的客户的连接,Nginx会将请求转发给uWSGI。这些组件在同一台电脑上工作,但我们更愿意使用Unix socket,因为它更快更安全。我们调用myapp.sock,并将其置于同一文件路径下。

***之后给Nginx用户组uWSGI进程的660权限,以便组内用户可以读写socket相应信息。为了进程结束时清空socket,所以增加了vacuum选项。

***最后一个设置 die-on-term 选项。System启动和 uWSGI 对不同的进程信号有不同的处理方式,该设置让系统两个组件识别信号时执行正确的行为动作。

[uwsgi]
module = wsgi

master = true
processes = 5

socket = myapp.sock
chmod-socket = 660
vacuum = true

die-on-term = true

***你可以注意到了我们没有指明protocol(像命令行那样)。因为 uWSGI 默认使用 uwsgi protocol,这是一个更快的二进制服务器通信协议。 Nginx 内置了此协议功能,比强制使用HTTP效率更高。

3、创建系统服务文件

下一步需要处理系统服务文件,该文件允许CENTOS系统初始化时自动启动uWSGI服务。/etc/systemd/system 创建以 .service为后缀的myapp.service文件。

[Unit]
Description=uWSGI instance to serve myapp
After=network.target

unit部分:After告诉系统初始化时,只有网络组件正常运行后,才启动这个服务。

[Service]部分指明了进程所属用户和组。授权进程所属用户相应的文件对应权限。授权Nginx用户组权限,以实现和uWSGI进程通信。

 

[Unit]
Description=uWSGI instance to serve myapp
After=network.target

[Service]
User=user
Group=nginx
WorkingDirectory=/home/lubaohui/myproject
Environment="PATH=/home/user/lubaohui/myprojectenv/bin"
ExecStart=/home/lubaohui/myproject/myprojectenv/bin/uwsgi --ini myapp.ini

[Install]
WantedBy=multi-user.target

启动这个服务,并允许系统启动时自动加载。([Install]部分负责这个工作)

[root@localhost system]# systemctl enable myapp
Created symlink from /etc/systemd/system/multi-user.target.wants/myapp.service to /etc/systemd/system/myapp.service.
[root@localhost system]#

三、配置 Nginx 代理 Requests请求

uWSGI应用服务已经运行,当前只是在等待项目路径下socket文件发来的请求了。我们需要配置Nginx用来将网页请求传递给使用uswgi protocol的socket。

编辑文件:

/etc/nginx/nginx.conf
server {
    listen 80;
    server_name 192.168.56.101;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/user/myproject/myapp.sock;
    }
}
usermod -a -G user nginx
chmod 710 /home/user
nginx -t
sudo systemctl start nginx
sudo systemctl enable nginx

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值