【保姆级】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./stop.sh

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/3245417.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

CyberVadis认证是什么

CyberVadis认证是一项全球性的、权威的、基于云的网络安全性评估和认证项目。它是由Altimeter公司开发的&#xff0c;专门针对云计算服务提供商、数据中心、软件即服务(SaaS)供应商、安全咨询服务公司和内部IT部门而设计的。 CyberVadis认证旨在评估和验证组织在网络安全方面的…

数电基础 - 硬件描述语言

目录 一. 简介 二. Verilog简介和基本程序结构 三. 应用场景 四. Verilog的学习方法 五.调式方法 一. 简介 硬件描述语言&#xff08;Hardware Description Language&#xff0c;HDL&#xff09;是用于描述数字电路和系统的形式化语言。 常见的硬件描述语言包括 VHDL&…

邮箱表单系统源码

邮箱表单简介 我们的邮箱表单系统是一个简洁高效的工具&#xff0c;旨在为用户提供一种便捷的方式来提交他们的邮箱地址。该系统可以用于订阅新闻通讯、注册活动、获取用户反馈等多种场景。 功能特点&#xff1a; 用户友好的界面&#xff1a; 表单设计简洁直观&#xff0c;用…

成功适配!极验设备指纹HarmonyOS 鸿蒙版官方下载

近日&#xff0c;华为开发者大会&#xff08;HDC 2024&#xff09;在东莞召开。在大会开幕日的首场主题演讲中&#xff0c;华为宣布当前已有TOP5000应用成为鸿蒙原生应用&#xff0c;350&#xff0b;SDK已适配HarmonyOS NEXT版本。其中&#xff0c;极验作为其重要伙伴&#xff…

AI自动生成PPT哪个软件好?高效制作PPT优选这4个

7.15初伏的到来&#xff0c;也宣告三伏天的酷热正式拉开序幕~在这个传统的节气里&#xff0c;人们以各种方式避暑纳凉&#xff0c;享受夏日的悠闲时光。 而除了传统的避暑活动&#xff0c;我们还可以用一种新颖的方式记录和分享这份夏日的清凉——那就是通过PPT的方式将这一传…

02 Git环境搭建

第2章&#xff1a;Git环境搭建 一、Git下载和安装 ​ 官网&#xff1a;Git (git-scm.com) 一&#xff09;安装主程序 ​ 准备安装包&#xff0c;双击安装 ​ 开始安装 ​ 选择安装位置 ​ 选择需要安装的组件&#xff08;默认&#xff09; ​ 选择文件夹菜单 ​ 选择编辑器&…

自适应巡航控制中的Stop Go功能详解

自适应巡航控制中的跟车行驶功能详解 文章目录 1. 背景介绍2. 功能定义3. 功能原理4. 传感器架构5. 实际应用案例6. 总结与展望 1. 背景介绍 自适应巡航控制&#xff08;Adaptive Cruise Control, ACC&#xff09;系统中的Stop & Go功能是提升驾驶舒适性和安全性的重要子…

Visual Studio2022中使用.Net 8 在 Windows 下使用 Worker Service 创建守护进程

Visual Studio2022中使用.Net 8 在 Windows 下创建 Worker Service 1 什么是 .NET Core Worker Service1.1 确认Visual Studio中安装了 ASP.NET和Web开发2 创建 WorkerService项目2.1 新建一个WorkerService项目2.2 项目结构说明3 将应用转换成 Windows 服务3.1 安装Microsoft.…

Spring与设计模式实战之策略模式

Spring与设计模式实战之策略模式 引言 在现代软件开发中&#xff0c;设计模式是解决常见设计问题的有效工具。它们提供了经过验证的解决方案&#xff0c;帮助开发人员构建灵活、可扩展和可维护的系统。本文将探讨策略模式在Spring框架中的应用&#xff0c;并通过实际例子展示…

【HarmonyOS】关于鸿蒙消息推送的心得体会 (一)

【HarmonyOS】关于鸿蒙消息推送的心得体会&#xff08;一&#xff09; 前言 这几天调研了鸿蒙消息推送的实现方式&#xff0c;形成了开发设计方案&#xff0c;颇有体会&#xff0c;与各位分享。 虽然没做之前觉得很简单的小功能&#xff0c;貌似只需要和华为服务器通信&…

MyPostMan 迭代文档管理、自动化接口闭环测试工具(自动化测试篇)

MyPostMan 是一款类似 PostMan 的接口请求软件&#xff0c;按照 项目&#xff08;微服务&#xff09;、目录来管理我们的接口&#xff0c;基于迭代来管理我们的接口文档&#xff0c;文档可以导出和通过 url 实时分享&#xff0c;按照迭代编写自动化测试用例&#xff0c;在不同环…

TypeScript 函数类型 (二)

函数类型 函数有两种方式定义 function 关键字来定义函数 function a(){}表达式定义&#xff08;箭头函数的形式&#xff09; const a()>{}函数需要定义类型的有三个地方 入参 和 返回值 以及 函数本身 的类型, 函数本身的类型常用于表达式定义的函数 function sum(a:stri…

【低照度图像增强系列(8)】URetinex-Net算法详解与代码实现(2022|CVPR)

前言 ☀️ 在低照度场景下进行目标检测任务&#xff0c;常存在图像RGB特征信息少、提取特征困难、目标识别和定位精度低等问题&#xff0c;给检测带来一定的难度。 &#x1f33b;使用图像增强模块对原始图像进行画质提升&#xff0c;恢复各类图像信息&#xff0c;再使用目标检…

Word参考文献交叉引用

前言 Word自带交叉引用功能&#xff0c;可在正文位置引用文档内自动编号的段落&#xff0c;同时创建超链接&#xff0c;适用于参考文献的引用。使用此方法对参考文献进行引用后&#xff0c;当参考文献的编号发生变化时&#xff0c;只需要更新域即可与正文中的引用相对应。下文…

实习随笔【实现Json格式化与latex渲染】

【写在前面】在实习中&#xff0c;遇到了如下需求&#xff1a; 待格式化数据大概长这样&#xff0c;里面存在Json乱码以及由$$包裹的公式 目标格式&#xff1a; 一、Json格式化 我们这里的任务主要分为两部分&#xff1a; 解析一个可能包含嵌套的 JSON 字符串格式化 JSON 对象…

这个工具居然能让你的微信暴露得一览无余!!

今天在github看到一个不错的项目&#xff0c;叫做wx-dump-4j&#xff0c;不看不知道&#xff0c;一看吓一跳&#xff0c;这个工具完全的解析了我的微信&#xff01;这个工具准确显示好友数、群聊数和当日消息总量&#xff0c;并且&#xff01;&#xff01;这个工具提供过去15天…

冷却塔的选型计算介绍

1 冷却塔补水量计算 冷却塔补水蒸发量排污量飘散损失泄漏量。一般冷却塔内水份的蒸发量约为进水量的1&#xff5e;2.5%。 &#xff08;1&#xff09;蒸发量 总冷却循环水量的蒸发量E C ① 冷却蒸发量 E E&#xff1d;72Q(X1–X2)&#xff1d;L △t&#xff0f;600 E : 蒸发…

【C++题解】1154. 数组元素的查找

问题&#xff1a;1154. 数组元素的查找 类型&#xff1a;数组找数 题目描述&#xff1a; 给你 m 个整数&#xff0c;查找其中有无值为 n 的数&#xff0c;有则输出该数第一次出现的位置,没有则输出 −1 。 输入&#xff1a; 第一行一个整数 m 代表数的个数 ( 0≤m≤100 ) 。…

数据结构-C语言-排序(2)

代码位置&#xff1a;test-c-2024: 对C语言习题代码的练习 (gitee.com) 一、前言&#xff1a; 1.1-排序定义&#xff1a; 排序就是将一组杂乱无章的数据按照一定的规律&#xff08;升序或降序&#xff09;组织起来。(注&#xff1a;我们这里的排序采用的都为升序) 1.2-排序分…

Ubuntu 22.04.4 LTS (linux) 安装iftop 监控网卡流量 软件

1 安装iftop sudo apt update sudo apt-get install iftop 2 监控网卡 sudo iftop -i eth0 -n -p 界面最上面&#xff0c;显示的是类似刻度尺的刻度范围&#xff0c;显示流量图形的长条作标尺用的。 中间的< >这两个左右箭头&#xff0c;表示的是流量的进出方向.TX&…