本文主要是介绍centso上边部署flask项目 ngnix+uwsgi,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
---|app
----------|__init__.py
----------|views.py
---|logs
---|myenv
---|run.py
---|uwsgi.ini
一、安装uwsgi
pip install uwsgi
二、添加uwsgi配置文件
在根目录下添加uwsgi.ini,内容如下:
- [uwsgi]
- socket = 127.0.0.1:8001
- pythonpath = /home/rs/myproject
- module = run
- callable = app
- processes = 4
- threads = 2
各参数介绍:
socket:通讯端口,外界可以通过127.0.0.1:8001访问,相当于我们在本地运行flask,并通过127.0.0.1:5000访问;并负责与nginx通信。
pythonpath:项目目录。
module:启动文件的文件名,我们可以在本地用python run.py启动flask项目。
callable:程序内启用的application变量名。
processes:处理器个数。
threads:线程数。
注意:最好不要在配置文件写中文注释,别问我为什么。
三、启动uwsgi
uwsgi uwsgi.ini
四、安装nginx
yum install nginx
五、修改nginx配置文件
配置文件的路径不尽相同,我的在/etc/nginx/nginx.conf。
修改如下(只修改标红部分即可):
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 服务器公网ip;
root /usr/share/nginx/html;
#root /var/www/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001;
uwsgi_param UWSGI_PYHOME /home/rs/myproject/myenv;
uwsgi_param UWSGI_CHDIR /home/rs/myproject;
uwsgi_param UWSGI_SCRIPT run:app;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
六、启动nginx
执行nginx命令。启动后,在浏览器输入http://ip即可查看部署是否成功。
附:
流程(原理):web请求--->nginx代理--->uwsgi代理--->python项目
uwsgi启动:
uwsgi XX.ini
uwsgi停止:
sudo killall -9 uwsgi
nginx启动:
nginx
nginx停止:
nginx -s stop
这篇关于centso上边部署flask项目 ngnix+uwsgi的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!