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

news/2024/8/26 16:26:39 标签: python, 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 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.niftyadmin.cn/n/5558313.html

相关文章

YOLO--置信度(超详细解读)

YOLO&#xff08;You Only Look Once&#xff09;算法中的置信度&#xff08;Confidence&#xff09;是一个关键概念&#xff0c;用于评估模型对预测框内存在目标对象的信心程度以及预测框对目标对象位置的准确性。 一、置信度的定义 数值范围&#xff1a;置信度是一个介于0和…

数学基础【俗说矩阵】:齐次线性方程和非齐次线性方程求解-学习笔记

一、矩阵基础知识 二元一次方程的传统解法 不论是代入消元法还是加减消元法都统称 【高斯消元法】。 齐次方程组和非齐次方程组 线性方程组的解 线性方程的向量展示 向量规则 矩阵的高斯消元和初等行变行及其规则 高斯消元规则 初等行变换 矩阵经初等行变换成阶梯矩阵&…

MySQL 数据库 day 7.16

ok了家人们今天继续记录一下数据库,看看今天学了什么。 一.事物概述 1.1 环境准备 -- 账户表 create table account( id int primary key auto_increment, name varchar(20), money double );insert into account values (null,张三,1000); insert into account values (n…

Windows命令行(CMD)中,tasklist | findstr(搜索并显示包含特定字符串的进程信息)

文章目录 示例注意事项示例&#xff1a;使用 /FI 选项过滤进程 在Windows命令行&#xff08;CMD&#xff09;中&#xff0c; tasklist 命令用于显示当前运行的进程列表&#xff0c;而 findstr 命令则用于搜索字符串。当你将 tasklist 命令的输出通过管道&#xff08; |&…

Postman、Apifox、Apipost用哪个?

Postman、Apifox、Apipost都是流行的API接口管理工具&#xff0c;它们各自具有不同的特点和优势&#xff0c;因此哪个更好用取决于具体的使用场景和需求。以下是对这三个工具的比较分析&#xff1a; 一、Postman 特点与优势&#xff1a; 支持多种请求方式&#xff1a;包括GE…

uni-app开发时自定义导航栏

1. 修改pages.json文件 增加navigationStyle {"path": "pages/index/index","style": {"navigationStyle": "custom","navigationBarTitleText": "首页"}},2. 下载uni-ui扩展组件库 npm install dcl…

Mongodb数组字段索引之多键索引

学习mongodb&#xff0c;体会mongodb的每一个使用细节&#xff0c;欢迎阅读威赞的文章。这是威赞发布的第92篇mongodb技术文章&#xff0c;欢迎浏览本专栏威赞发布的其他文章。如果您认为我的文章对您有帮助或者解决您的问题&#xff0c;欢迎在文章下面点个赞&#xff0c;或者关…

R-CNN、Fast R-CNN和Faster R-CNN:目标检测的进化之路

在计算机视觉的世界里,目标检测是一个重要的任务,它的目标是找到图像中的特定物体,并标注出它们的位置。这项技术广泛应用于自动驾驶、安防监控等领域。为了让计算机能够准确高效地完成这一任务,科学家们提出了许多优秀的算法,其中最具代表性的就是R-CNN、Fast R-CNN和Fas…