一、安装pyton3.6.5
执行 shell脚本 安装
测试是否成功
测试 ssl模块是否正常加入,没报错就是正常
测试pip3是否成功
二、创建虚拟环境
创建一个项目文件夹,然后创建一个虚拟环境。创建完成后项目文件夹中会有一个 venv
文件夹:
mkdir /usr/local/project cd /usr/local/project python3 -m venv venv
进入虚拟环境
. venv/bin/activate
三、安装flask、uwsgi
pip3 install Flask -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple
创建 helloworld.py 启动Flask web应用
fromflaskimportFlask, jsonify, requestapp=Flask(__name__) route("/",methods=["GET"]) .defindexFun(): return"index"route("/helloWorld") .defhelloWorld(): print('请求方式----》', request.method) args=request.args.get("name") or"args没有"print('', args) form=request.form.get('test') or'form没有'print('', form) returnjsonify(success=True, args=args, form=form) if__name__=='__main__': app.run(host='0.0.0.0', port=8987, debug=True)
创建uwsgi.ini
[uwsgi] http=:8987 socket=127.0.0.1:8001 wsgi-file=/usr/local/project/helloword.py callable=app processes=4 threads=2 stats=127.0.0.1:9191 vacuum=true
安装 nginx
yum install nginx
修改nginx.conf文件 代理uwsgi请求
upstream flask{ server 127.0.0.1:8001; } server { listen 80; server_name localhost; #charset koi8-r;#access_log logs/host.access.log main;location / { include uwsgi_params; uwsgi_pass flask; } }
nginx启动
/usr/local/nginx/sbin/nginx
uwsgi启动
uwsgi uwsgi.ini / 后台启动 uwsgi -d --ini uwsgi.ini
退出虚拟环境
deactivate
请求
http://localhost
http://localhost/helloWorld?name=haha
后记
pip3 安装组件 最好换源 -i https://pypi.tuna.tsinghua.edu.cn/simple 国内源快很多,还有尽量都使用同一个源安装,以防出现不兼容问题。
新补充
使用 supervisor 管理uwsgi服务,以及后续遇到的问题解决方案:
https://blog.csdn.net/qq_35304570/article/details/80304482
此链接中 supervisor 部分
安装supervisor,新增配置文件到etc下
pip3 install supervisor echo_supervisord_conf > supervisor.conf # 生成 supervisor 默认配置文件vim supervisor.conf # 修改 supervisor 配置文件,添加 uwsgi 进程管理mv supervisor.conf /etc/
修改配置文件增加uwsgi子进程配置
vim /etc/supervisor.conf
[program:uwsgi] redirect_stderr=true command=/usr/local/project/venv/bin/uwsgi /usr/local/project/uwsgi.ini ;supervisor启动命令 directory=/usr/local/project ; 项目的文件夹路径 startsecs=0 ; 启动时间 stopwaitsecs=0 ; 终止等待时间 autostart=true ; 是否自动启动 autorestart=true ; 是否自动重启 stdout_logfile=/usr/local/project/uwsgi/logs/uwsgi.log ; log 日志 stderr_logfile=/usr/local/project/uwsgi/logs/uwsgi.err ; 错误日志
打开这几项注释
[inet_http_server] ; inet (TCP) server disabled by default port=*:9001 ; ip_address:port specifier, *:port for all iface username=user ; default is no username (open server) password=123 ; default is no password (open server)
新建uwsgi日志文件
mkdir /usr/local/project/uwsgi/logs touch /usr/local/project/uwsgi/logs/uwsgi.log touch /usr/local/project/uwsgi/logs/uwsgi.err
启动supervisor 和 uwsgi子服务
supervisord -c /etc/supervisor.conf supervisorctl -c /etc/supervisor.conf status supervisorctl -c /etc/supervisor.conf start uwsgi
启动过程中可能遇到的错误:
unix:///var/run/supervisor.sock no such file
https://blog.csdn.net/weixin_41762173/article/details/88901970
解决办法:
修改/etc/supervisor.conf中路径
[unix_http_server] file=/var/run/supervisor.sock ; the path to the socket file ... [supervisorctl] serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
touch /var/run/supervisor.sock chmod777 /var/run/supervisor.sock supervisorctl reload
运行supervisorct 报如下错误:
supervisorctl unix:///var/run/supervisor.sock refused connection
解决办法:
unlink /var/run/supervisor.sock unlink /tmp/supervisor.sock
如果报如下错:
Unlinking stale socket /var/run/supervisor.sock
解决办法:
unlink /var/run/supervisor.sock