当前位置: 首页 > news >正文

【保姆级】Python项目部署到Linux生产环境(uwsgi+python+flask+nginx服务器)

1.安装python

我这里是3.9.5版本

安装依赖:

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y

根据自己的需要下载对应的python版本:

cd /usr/local
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz

 解压 > 编译 > 安装

tar -xvf Python-3.9.5.tgz
cd /usr/local/Python-3.9.5
./configure
make all
make install

Python解释器配置清华源:

pip3.9 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/

2.虚拟环境配置

1)安装 virtualenv

pip3.9 install virtualenv

2)创建虚拟环境(一般是一个项目一个虚拟环境)

mkdir home/python_item/envs
cd /home/python_item/envs/
virtualenv /home/python_item/envs/tender --python=python3.9

3)激活虚拟环境

source  /home/python_item/envs/tender/bin/activate

4)安装项目依赖

cd /home/python_item/tender_project #进入项目目录pip3.9 install flask
pip3.9 install lxml
pip3.9 install pymysql
pip3.9 install requests

3、uwsgi安装

1)安装uwsgi

激活虚拟环境,在虚拟环境中安装
source  /home/python_item/envs/tender/bin/activate
pip install uwsgi

2)基于uwsgi配置文件的方式运行flask项目

[uwsgi]socket = 127.0.0.1:8001  chdir = /home/python_item/tender_project wsgi-file = main.py callable = app processes = 1 virtualenv = /home/python_item/envs/tender/ 

3)启动项目

uwsgi --ini tender_test.ini

报错:

(tender) [root@node1 tender_project]# uwsgi --ini tender_test.ini
[uWSGI] getting INI configuration from tender_test.ini
*** Starting uWSGI 2.0.26 (64bit) on [Tue Jul 16 15:30:21 2024] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-44) on 16 July 2024 07:24:56
os: Linux-3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022
nodename: node1
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /home/python_item/tender_project
detected binary path: /home/python_item/envs/tender/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 /home/python_item/tender_project
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7787
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 127.0.0.1:8001 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.9.5 (default, Jul 16 2024, 15:00:56)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
PEP 405 virtualenv detected: /home/python_item/envs/tender/
Set PythonHome to /home/python_item/envs/tender/
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x27abcc0
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 ***
Traceback (most recent call last):File "main.py", line 7, in <module>from common import WebInfoFactoryFile "/home/python_item/tender_project/./common.py", line 2, in <module>import requestsFile "/home/python_item/envs/tender/lib/python3.9/site-packages/requests/__init__.py", line 43, in <module>import urllib3File "/home/python_item/envs/tender/lib/python3.9/site-packages/urllib3/__init__.py", line 42, in <module>raise ImportError(
ImportError: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fips  26 Jan 2017'. See: https://github.com/urllib3/urllib3/issues/2168
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: 1600, cores: 1)

解决:

urllib3版本不兼容,卸载重装

pip3.9 uninstall urllib3pip3.9 install urllib3==1.22

重启即可

uwsgi --ini tender_test.ini

nginx

安装:Linux安装Nginx

配置:

    upstream flask {server 127.0.0.1:8001 weight=1;
}server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}location /tenderTest {    uwsgi_pass   flask;include  /usr/local/nginx/conf/uwsgi_params;
#            index  index.html index.htm;}location /test/ {uwsgi_pass   127.0.0.1:8002;include  /usr/local/nginx/conf/uwsgi_params;
#            index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}

4.访问成功 

5.编写脚本快速重启/停止

reboot.sh

#! /usr/bin/env bash
echo -e "======================wsgi process======================"ps -ef|grep tender_test.ini |grep -v grepsleep 0.5echo -e "======================going to close ======================"ps -ef | grep tender_test.ini | grep -v grep | awk '{print $2}' | xargs kill -9sleep 0.5echo -e "======================check if the kill action is correct ======================"/home/python_item/envs/tender/bin/uwsgi --ini tender_test.ini & >/dev/nullecho -e "======================started... ======================"sleep 1ps -ef | grep tender_test.ini |grep -v grep

 stop.sh

echo -e "======================wsgi process======================"ps -ef|grep tender_test.ini |grep -v grepsleep 0.5echo -e "======================going to close ======================"ps -ef  |grep tender_test.ini | grep -v grep | awk '{print $2}' | xargs kill -9sleep 0.5

在项目目录下执行重启或停止,脚本放在下面,根据自己的实际情况进行修改即可

更新权限

chmod 775 reboot.shchmod 775 stop.sh 

启动!

./reboot.sh./stop.sh

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • python编程技巧——list计算
  • 继承与多态 Java
  • macOS 环境Qt Creator 快捷键
  • SimMIM:一个类BERT的计算机视觉的预训练框架
  • vue学习笔记(十)——Vuex(状态管理,组件间共享数据)
  • 极狐GitLab 如何管理 PostgreSQL 扩展?
  • 系统架构设计师教程(清华第二版) 第3章 信息系统基础知识-3.2 业务处理系统-解读
  • Android构建任务assemble、bundle、compile、package、install
  • python如何创建SQLite 数据库连接,如何将数据库存储在内存中?
  • git clone加速
  • huawei USG6001v1学习---防火墙相关知识(2)
  • Java基础编程500题——String
  • Kubernetes Service 之 LoadBalancer
  • Proxyman for Mac v5.6.1 抓包调试工具
  • 【Qt+opencv】计时函数与图像变换
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • eclipse(luna)创建web工程
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • learning koa2.x
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • Linux编程学习笔记 | Linux多线程学习[2] - 线程的同步
  • Odoo domain写法及运用
  • ReactNativeweexDeviceOne对比
  • Twitter赢在开放,三年创造奇迹
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 记录一下第一次使用npm
  • 看域名解析域名安全对SEO的影响
  • 学习使用ExpressJS 4.0中的新Router
  • Prometheus VS InfluxDB
  • ​学习笔记——动态路由——IS-IS中间系统到中间系统(报文/TLV)​
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #QT(串口助手-界面)
  • #我与Java虚拟机的故事#连载02:“小蓝”陪伴的日日夜夜
  • (12)目标检测_SSD基于pytorch搭建代码
  • (145)光线追踪距离场柔和阴影
  • (2024.6.23)最新版MAVEN的安装和配置教程(超详细)
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (差分)胡桃爱原石
  • (第30天)二叉树阶段总结
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (三维重建学习)已有位姿放入colmap和3D Gaussian Splatting训练
  • (十三)Flink SQL
  • (学习日记)2024.01.19
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .net core 使用js,.net core 使用javascript,在.net core项目中怎么使用javascript
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET 通过系统影子账户实现权限维持
  • .NET/C# 避免调试器不小心提前计算本应延迟计算的值
  • .NET/C# 将一个命令行参数字符串转换为命令行参数数组 args
  • .NET的数据绑定