Linix环境下 Gunicorn + Supervisor + Nginx 部署 Flask 项目、Celry定时任务

本文详细介绍如何在Flask项目中使用Gunicorn提升并发能力,结合Nginx实现优雅的反向代理,以及利用Supervisor进行进程管理和开机自启。更进一步,文章深入探讨了如何在Flask应用中集成Celery进行异步任务处理和定时任务调度,包括配置、执行和管理策略。

首先先把项目文件上传到服务器制定目录, 然后在终端用ssh username@ip的方式访问服务器,flask项目一般都是再虚拟环境下运行的,常规启动web服务都是先进入虚拟环境,再执行对应的启动文件。默认只启用单线程来处理web交互数据,一旦用户较多,就会出现阻塞导致页面卡住,很显然用户体验不佳,因此为了能够尽可能的提高服务器处理数据,希望能开启多个线程处理web交互,可以采用Gunicorn控制运行项目。

1.安装gunicorn

可以进入虚拟欢迎安装该插件

pip install gunicorn

如果安装该插件出现以下错误:

C -DLIBEV_EMBED=1 -DEV_COMMON= -DEV_CLEANUP_ENABLE=0 -DEV_EMBED_ENABLE=0 -DEV_PERIODIC_ENABLE=0 -DEV_USE_REALTIME=1 -DEV_USE_MONOTONIC=1 -DEV_USE_FLOOR=1 -I/usr/include/python3.6m -I/usr/include/python3.6m -I/tmp/pip-build-ju7ok6wv/gevent/deps -I/tmp/pip-build-ju7ok6wv/gevent/src/gevent/libev -I/tmp/pip-build-ju7ok6wv/gevent/deps/libev -Isrc/gevent -Isrc/gevent/libev -Isrc/gevent/resolver -I. -I/home/www/XXX/venv/include -I/usr/include/python3.6m -c src/gevent/libev/corecext.c -o build/temp.linux-x86_64-3.6/src/gevent/libev/corecext.o
    src/gevent/libev/corecext.c:94:20: fatal error: Python.h: No such file or directory
     #include "Python.h"
                        ^
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    
    ----------------------------------------
Command "/home/www/XXX/venv/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-ju7ok6wv/gevent/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-whw3baqy-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/www/XXX/venv/include/site/python3.6/gevent" failed with error code 1 in /tmp/pip-build-ju7ok6wv/gevent/
解决方案:
yum install python-devel
yum install libevent-devel
2.启动项目
  • 常规启动:
(venv) [root@docker test]# python manage.py runserver --host 0.0.0.0 --port 8000
  • 基于gunicorn管理启动项目
(venv) [root@docker test]# gunicorn -w 4 -b 0.0.0.0:8000 manage:app
[2020-06-02 22:23:54 -0400] [34828] [INFO] Starting gunicorn 20.0.4
[2020-06-02 22:23:54 -0400] [34828] [INFO] Listening at: http://0.0.0.0:8000 (34828)
[2020-06-02 22:23:54 -0400] [34828] [INFO] Using worker: sync
[2020-06-02 22:23:54 -0400] [34831] [INFO] Booting worker with pid: 34831
[2020-06-02 22:23:54 -0400] [34832] [INFO] Booting worker with pid: 34832
[2020-06-02 22:23:54 -0400] [34833] [INFO] Booting worker with pid: 34833
[2020-06-02 22:23:54 -0400] [34834] [INFO] Booting worker with pid: 34834

参数介绍:

  • -w:指定fork的worker进程数
  • -b:指定绑定的端口
  • manage:python文件名,启动文件
  • app:falsk实例化对象app

这样就做到了使用gunicorn管理启动flask项目
上面的这种方式一般都会指定一个端口开启flask服务,这也导致前台页面访问的URL也必须用这个端口,用户体验不佳,所以我们可以使用nginx反向代理,把我们自定义端口映射到80端口,也就是用户访问ip或者域名经过反向代理就可以映射到自定义端口,如上面开的是8000端口,用户访问时必须是ip:8000才能访问,经过反向代理直接访问ip即可。

3.使用nginx代理

安装nginx需先退出虚拟环境,执行以下命令:

安装nginx的源
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
安装nginx
yum install -y nginx

安装好nginx,直接去浏览器访问虚拟机或者服务器的ip地址即可访问nginx的默认欢迎页:welcome to nginx
接下来就需要做反向代理,把上面开的8000端口映射到80的配置!
查看nginx的安装路径:

[root@docker]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz

默认配置放在/etc/nginx目录下

[root@docker conf.d]# cd /etc/nginx
[root@docker nginx]# ls
conf.d          koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params  koi-win  modules     scgi_params  win-utf
[root@docker nginx]# cat nginx.conf 

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
   
   
    worker_connections  1024;
}


http {
   
   
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on; #开启gzip
    gzip_vary on;
    gzip_min_length 1k; #不压缩临界值,大于1k的才压缩,一般不用改
    gzip_buffers 4 16k;
    gzip_comp_level 6; #压缩级别,数字越大压缩的越好
    gzip_types  text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png image/x-icon;
    
    add_header X-Frame-Options sameorigin always;
    include /etc/nginx/conf.d/*.conf; # 指定自定义配置文件路径,一般不在这里直接改,而是以引入的方式加入配置 
}
修改默认配置文件(default.conf)
[root@docker nginx]# cd conf.d/
[root@docker conf.d]# ls
default.conf
[root@docker conf.d]# cat default.conf
server {
   
   
 listen 80;
 server_name localhost;
 location / {
   
   
	root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://localhost:8000;
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_connect_timeout 500s;
        proxy_read_timeout 500s;
        proxy_send_timeout 500s;
        proxy_set_header   Host    $host; # 为了能获取实际域名
 }    
}

这里的8000端口需要跟

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值