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

news2024/9/21 4:26:13

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 local
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz

 解压 > 编译 > 安装

tar -xvf Python-3.9.5.tgz
cd Python-3.9.5.tgz
./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 WebInfoFactory
  File "/home/python_item/tender_project/./common.py", line 2, in <module>
    import requests
  File "/home/python_item/envs/tender/lib/python3.9/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/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 urllib3

pip3.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 grep

sleep 0.5

echo -e "======================going to close ======================"

ps -ef | grep tender_test.ini | grep -v grep | awk '{print $2}' | xargs kill -9

sleep 0.5

echo -e "======================check if the kill action is correct ======================"

/home/python_item/envs/tender/bin/uwsgi --ini tender_test.ini & >/dev/null

echo -e "======================started... ======================"

sleep 1

ps -ef | grep tender_test.ini |grep -v grep

 stop.sh

echo -e "======================wsgi process======================"

ps -ef|grep tender_test.ini |grep -v grep

sleep 0.5

echo -e "======================going to close ======================"

ps -ef  |grep tender_test.ini | grep -v grep | awk '{print $2}' | xargs kill -9

sleep 0.5

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

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

相关文章

ELK日志管理

目录 一.ELK简介 1.Elasticsearch 2.Logstash 4.ELK 组件解决的主要问题 5.ELK应用的好处 6.ELK的工作原理 7.ELK的主要应用场景 二.安装部署ELK 1.前期准备 2.安装部署ElasticseaErch 软件 3. 在apache节点上 部署 Logstash 4.安装 kibana 三.实现nginx日志管理 …

modbus slave 设备通过 网关thingsboard-gateway 将数据上传到thingsboard云平台

搭建thingsboard物联网云平台花了大量时间&#xff0c;从小白到最后搭建成功&#xff0c;折磨了好几天&#xff0c;也感谢网友的帮助&#xff0c;提供了思路最终成功搞定&#xff0c;特此记录。 一、thingsboard环境搭建&#xff08;Ubuntu20.04LTS&#xff09; 参考官方文档&a…

景区导航导览系统:基于AR技术+VR技术的功能效益全面解析

在数字化时代背景下&#xff0c;游客对旅游体验的期望不断提升。游客们更倾向于使用手机作为旅行的贴身助手&#xff0c;不仅因为它能提供实时、精准的导航服务&#xff0c;更在于其融合AR&#xff08;增强现实&#xff09;、VR&#xff08;虚拟现实&#xff09;等前沿技术&…

Template execution failed: ReferenceError: name is not defined

问题 我们使用了html-webpack-plugin&#xff08;webpack&#xff09;进行编译html&#xff0c;导致的错误。 排查结果 连接地址 html-webpack-plugin版本低(2.30.1)&#xff0c;html模板里面不能有符号&#xff0c;注释都不行 // var reg new RegExp((^|&)${name}([^&…

【ROS2】高级:使用 Fast DDS 发现服务器作为发现协议 [社区贡献]

目标&#xff1a;本教程将展示如何使用 Fast DDS Discovery Server 发现协议启动 ROS 2 节点。 教程级别&#xff1a;高级 时间&#xff1a;20 分钟 目录 背景 快速 DDS 发现服务器 v2 先决条件 运行此教程 设置发现服务器 启动监听节点 启动对话节点展示发现服务器执行 可视化…

【15】Android基础知识之Window(一)

概述 这篇文章纠结了很久&#xff0c;在想需要怎么写&#xff1f;因为window有关的篇幅&#xff0c;如果需要讲起来那可太多了。从层级&#xff0c;或是从关联&#xff0c;总之不是很好开口。这次也下定决心&#xff0c;决定从浅入深的讲讲window这个东西。 Window Window是…

django报错(三):No crontab program或got an unexpected keyword argument ‘user’

Crontab是linux系统上的定时管理模块&#xff0c;简单配置&#xff0c;灵活使用。但是要在windows使用必须借助Cygwin等虚拟工具&#xff0c;否则会报错“No crontab program”。如下图&#xff1a; python-crontab是其提供了python模块对crontab的访问&#xff0c;即可以通过p…

【简历】惠州某二本学院:前端简历指导,秋招面试通过率为0

注&#xff1a;为保证用户信息安全&#xff0c;姓名和学校等信息已经进行同层次变更&#xff0c;内容部分细节也进行了部分隐藏 简历说明 这是一份25届二本同学&#xff0c;投递前端职位的简历&#xff0c;那么在校招环节二本同学主要针对的还是小公司&#xff0c;这个学校因为…

怎么关闭Windows安全中心?

Windows安全中心是Windows操作系统中的一项重要功能&#xff0c;系统提供这个功能的目的是保护电脑免受各种安全威胁。尽管如此&#xff0c;有时候我们可能出于某些原因需要关闭它。本文将详细介绍如何关闭Windows安全中心&#xff0c;以及需要注意的事项。 重要提醒&#xff1…

minIO集成springboot

问题 minIO与spring集成。 步骤 创建桶 创建key 找到创建账号页面&#xff0c;如下图&#xff1a; 点击创建&#xff0c;如下图&#xff1a; 设置如下权限&#xff1a; {"Version": "2012-10-17","Statement": [{"Effect": &q…

生成式AI、3D模型交易、模型轻量化、模型格式转换、3D可视化、数字孪生引擎等

老子云3D可视化快速开发平台&#xff0c;集云压缩、云烘焙、云存储云展示于一体&#xff0c;使3D模型资源自动输出至移动端PC端、Web端&#xff0c;能在多设备、全平台进行展示和交互&#xff0c;是全球领先、自主可控的自动化3D云引擎。 平台架构 平台特性 1、基于 HTML5 和 …

django报错(一):python manage.py makemigrations,显示“No changes detected”

执行python manage.py makemigrations命令无任何文件生成&#xff0c;结果显示“No changes detected”。 解决方案一&#xff1a; 1、执行命令&#xff1a;python manage.py makemigrations –empty appname 2、删除其中的0001_initial.py文件&#xff08;因为这个文件内容是…

《昇思25天学习打卡营第25天|第10天》

今天是打卡的第十天&#xff0c;今天开始学应用实践中的LLM原理和实践&#xff0c;今天学的是基于MindSpore实现BERT对话情绪识别。最先了解的是BERT模型的简介&#xff08;来自变换器的双向编码器表征量&#xff08;Bidirectional Encoder Representations from Transformers&…

【Java】:浅克隆和深克隆

克隆 克隆和赋值 克隆的结果是有多个相同的实体&#xff0c;各个对象指向不同的实体而多个不同对象指向一个相同的实体不是克隆&#xff0c;而是赋值 克隆的过程 首先实例化一个 student1 对象 在堆里开辟了一块内存用来存储 age 10 这个数据 调用 clone 方法 在堆中又开辟了一…

Python数据结构:实现自定义栈与队列

更多Python学习内容&#xff1a;ipengtao.com 在计算机科学中&#xff0c;栈&#xff08;Stack&#xff09;和队列&#xff08;Queue&#xff09;是两种常见的数据结构。它们在算法和数据处理方面有着广泛的应用。本文将详细介绍如何在Python中实现自定义的栈与队列&#xff0c…

stm32精密控制步进电机(升级篇)

这一篇文章里会深入的对步进电机控制方法进行论述 如何避免步进电机丢转的问题 1.机械结构&#xff1a;排查一下传动的问题&#xff0c;举个例子&#xff0c;我的毕设里大臂机械臂的步进电机有时会有丢转问题&#xff0c;造成无法运动到指定位置&#xff0c;后面发现是因为皮带…

【单元测试】SpringBoot

【单元测试】SpringBoot 1. 为什么单元测试很重要&#xff1f;‼️ 从前&#xff0c;有一个名叫小明的程序员&#xff0c;他非常聪明&#xff0c;但有一个致命的缺点&#xff1a;懒惰。小明的代码写得又快又好&#xff0c;但他总觉得单元测试是一件麻烦事&#xff0c;觉得代码…

2.RabbitMQ相关概念

介绍 RabbitMQ是一个消息中间件&#xff0c;接受并转发消息。它接收、存储和转发消息数据。 四大核心概念&#xff1a; 1.生产者 产生数据发送消息的程序是生产者。 2.消费者 3.队列 每一个队列对应一个消费者。 如果两个消费者对应同一个队列&#xff0c;那么队列中的…

云监控(华为) | 实训学习day1(10)

云监控&#xff1a;确保服务器高效运行 在当今的数字化时代&#xff0c;服务器的稳定运行对于任何企业都至关重要。为了确保服务器的 CPU、内存和硬盘等资源的合理运行&#xff0c;云监控成为了一项不可或缺的技术。本文将详细介绍云监控的基本概念、所需软件、配置方法以及如何…

分布式 I/O 系统 BL200 Modbus TCP 耦合器

BL200 耦合器是一个数据采集和控制系统&#xff0c;基于强大的 32 位微处理器设计&#xff0c;采用 Linux 操作系统&#xff0c;支持 Modbus 协议&#xff0c;可以快速接入现场 PLC、SCADA 以及 ERP 系统&#xff0c; 内置逻辑控制、边缘计算应用&#xff0c;适用于 IIoT 和工业…