nginx实战:flaks + uwgsi + nginx部署
一:创建一个flask_one.py的文件
from flask import Flask app = Flask(__name__) @app.route("/flask_one") def index(): return "hello flask_one" if __name__ == "__main__": app.run(debug=True,port=10010)
python3 flask_one.py
[root@VM_0_13_centos flask_one]# python3 flaks_one.py * Serving Flask app "flaks_one" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Running on http://127.0.0.1:10010/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 231-868-597
二:创建一个flask_two.py的文件
from flask import Flask app = Flask(__name__) @app.route("/flask_two") def index(): return "hello flask_two" if __name__ == "__main__": app.run(debug=True,port=10011)
python3 flask_two.py
[root@VM_0_13_centos flask_one]# python3 flaks_two.py * Serving Flask app "flaks_two" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Running on http://127.0.0.1:10011/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 231-868-597
需求:外部只想暴露80接口,不想暴露10010和10011内部端口,通过www.xxxxx/flask_one和 www.xxxxx/flask_two即可以访问到这两个flask接口
目前访问云服务器的公网ip,显示如下:nginx监听了所有的80端口
pip3 install uwsgi
在项目文件根目录新建配置文件uwsgi.ini(uwsgi支持多种配置文件格式, xml, ini, json等)
vim uwsgi.ini
写入配置内容如下:
[uwsgi] socket = 127.0.0.1:10010 # 监听的端口 plugins = python3 # 执行命令 chidr = /zyy/flask_test/flask_one # 执行的路径 wsgi-file = flask_one.py # 执行的文件名 callable = app # 程序变量名
启动 uwsgi
uwsgi --ini uwsgi.ini 在当前路径下
[root@VM_0_13_centos flask_one]# uwsgi --ini uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini open("./python3_plugin.so"): No such file or directory [core/utils.c line 3724] !!! UNABLE to load uWSGI plugin: ./python3_plugin.so: cannot open shared object file: No such file or directory !!! *** Starting uWSGI 2.0.18 (64bit) on [Sat Apr 25 22:17:22 2020] *** compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-39) on 25 April 2020 14:08:44 os: Linux-3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 nodename: VM_0_13_centos machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 1 current working directory: /zyy/flask_test/flask_one detected binary path: /usr/local/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 7270 your memory page size is 4096 bytes detected max file descriptor number: 100001 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uwsgi socket 0 bound to TCP address 127.0.0.1:10010 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: 3.7.0 (default, Apr 2 2020, 16:56:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x1c66900 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 *** failed to open python file flask_one.py unable to load app 0 (mountpoint='') (callable not found or import error) *** no app loaded. going in full dynamic mode *** 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: 28386, cores: 1)
报错原因:找不到 flask_one.py,名字为,flaks_one.py
指定python3的路径 修改 uwsgi.ini
[uwsgi] socket = 127.0.0.1:3001 # uwsgi服务监听的ip和端口 chdir = /zyy/flask_test/flask_one # 执行文件的目录 wsgi-file = /zyy/flask_test/flask_one/flaks_one.py # 执行文件的绝对路径 callable = app # 变量名 stats = 127.0.0.1:10010
然后启动 uwsgi uwsgi.ini
[root@VM_0_13_centos flask_one]# uwsgi uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini *** Starting uWSGI 2.0.18 (64bit) on [Sat Apr 25 22:47:29 2020] *** compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-39) on 25 April 2020 14:08:44 os: Linux-3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 nodename: VM_0_13_centos machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 1 current working directory: /zyy/flask_test/flask_one detected binary path: /usr/local/bin/uwsgi uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** chdir() to /zyy/flask_test/flask_one your processes number limit is 7270 your memory page size is 4096 bytes detected max file descriptor number: 100001 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uwsgi socket 0 bound to TCP address 127.0.0.1:3001 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: 3.7.0 (default, Apr 2 2020, 16:56:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x20fe870 uWSGI running as root, you can use --uid/--gid/--chroot options [root@VM_0_13_centos flask_one]# uwsgi uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini *** Starting uWSGI 2.0.18 (64bit) on [Sat Apr 25 22:53:05 2020] *** compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-39) on 25 April 2020 14:08:44 os: Linux-3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 nodename: VM_0_13_centos machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 1 current working directory: /zyy/flask_test/flask_one detected binary path: /usr/local/bin/uwsgi uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** chdir() to /zyy/flask_test/flask_one your processes number limit is 7270 your memory page size is 4096 bytes detected max file descriptor number: 100001 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uwsgi socket 0 bound to TCP address 127.0.0.1:3001 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: 3.7.0 (default, Apr 2 2020, 16:56:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x1132870 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 145840 bytes (142 KB) for 1 cores *** Operational MODE: single process *** WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1132870 pid: 2589 (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 master process (pid: 2589) spawned uWSGI worker 1 (pid: 2590, cores: 1) *** Stats server enabled on 127.0.0.1:10010 fd: 9 ***
修改nginx.conf
location /flask_one { # 表示只要是以 /flask_one为后缀的请求都转发给uwsgi服务器进行处理 #root html; #fastcgi_pass 127.0.0.1:10010; #fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; include uwsgi_params; uwsgi_pass 127.0.0.1:3001; # uwsgi服务器监听的ip和端口 }
访问网站
一个flask + uwsgi + nginx 的简单部署就完成了
但是这样有一个问题,如果uwsgi崩了,后台就启动停止了,这在线上运行是不被允许的,因此使用 python的进程管理工具,对uwsgi.ini 进行持续监控,停止了就自动重启
在 /home/conf.d/下,新建 flask_uwsgi.ini
[program:flask_test] command= uwsgi uwsgi.ini directory=/zyy/flask_test/flask_one/ autorestart=true autostart=true environment=ASPNETCORE_ENVIRONMENT=Production user=root stopsignal=INT startsecs=10 stopwaitsecs = 600 priority=15
然后重启 supervisord 和 supervisorctl
需求:在flask_one.py里面新增一个视图函数
from flask import Flask app = Flask(__name__) @app.route("/flask_one") def index(): return "hello flask_one" @app.route("/flask_one/haha") def haha(): return "haha" if __name__ == "__main__": app.run(debug=True,port=10010)
无法正常访问后
说明nginx的配置需要进行修改
修改default.conf
location ^~/flask_one { # 以flask_one开头的所有url都进行匹配 #root html; #fastcgi_pass 127.0.0.1:10010; #fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; include uwsgi_params; uwsgi_pass 127.0.0.1:3001; }
再次访问 /flask_one/haha,显示 The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
访问 /flask_one 正常
原因是修改代码后,uwsgi没有重新启动
然后访问,就可以了
# TODO