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 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
#! /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
在项目目录下执行重启或停止,脚本放在下面,根据自己的实际情况进行修改即可
更新权限
chmod 775 reboot.sh
chmod 775 stop.sh
启动!
./reboot.sh
./stop.sh