1、wget https://www.python.org/ftp/python/3.11.2/Python-3.11.2.tgz
2、安装python依赖环境
切换到root用户,然后执行下面命令:
3、安装gcc,用于后续安装Python时编译源码:
yum install gcc -y
4、安装Python3相关依赖:
yum install zlib zlib-devel -y
yum install bzip2 bzip2-devel -y
yum install ncurses ncurses=devel -y
yum install readline readline-devel -y
yum install openssl openssl-devel -y
yum install xz lzma xz-devel -y
yum install sqlite sqlite-devel -y
yum install gdbm gdbm-devel -y
yum install tk tk-devel -y
yum install mysql-devel -y
yum install python-devel -y
yum install libffi-devel -y
5、编译、安装python
编译环境准备好后,cd到Python源码安装目录:
[root@localhost sam]# cd Python-3.12.0
[root@localhost Python-3.12.0]# pwd
/home/sam/Python-3.12.0
然后执行configure文件:
./configure
# 或者
./configure --prefix=安装路径
# 其中,安装路径是自己想要安装到的路径,可以自己设置。
# 或者 建议用下面的可以省略prefix,一定要使用with-openssl
./configure --prefix=安装路径 --with-openssl=/usr/local/openssl
# 配置安装目录,指定openssl环境的安装目录
编译
make && make install:
6、检查python环境是否安装成功
whereis python
7、建立软连接
[root@localhost bin]# which python3
/usr/local/bin/python3
[root@localhost bin]# ln -s /usr/local/bin/python3 /usr/bin/python3
which python3 # 查看安装路径
ln -s /usr/local/bin/python3 /usr/bin/python3 # 创建软链接
/usr/local/bin/python3:是安装路径
/usr/bin/python3:是软链接路径
rm /usr/bin/python:删除软链接
8、查看、验证
python3 --version
[root@localhost bin]# python3
Python 3.12.0 (main, Aug 18 2024, 22:24:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
9、卸载:
[root@localhost ~]# whereis python3
python3: /usr/local/bin/python3.12 /usr/local/bin/python3.12-config /usr/local/bin/python3 /usr/local/lib/python3.12
[root@localhost ~]# rm -rf /usr/local/bin/python3.12
[root@localhost ~]# rm -rf /usr/local/bin/python3.12-config
[root@localhost ~]# rm -rf /usr/local/bin/python3
[root@localhost ~]# rm -rf /usr/local/lib/python3.12
[root@localhost ~]# whereis python3
python3:[root@localhost ~]# python3 --version
-bash: /usr/local/bin/python3: 没有那个文件或目录
10、虚拟环境
为了环境不被污染和后期版本更新迭代管理,建议安装虚拟环境
pip install virtualenv(注意需要配置环境变量:vi /etc/profile,添加 PATH=$PATH:/usr/local/bin)
下载太慢就添加源-i https://pypi.tuna.tsinghua.edu.cn/simple
创建虚拟环境目录并创建虚拟环境
mkdir /home/sam/envs
which python3
#/usr/local/bin/python3,将查找到的放到 --pyhton=后
virtualenv /home/sam/envs/nb --python=/usr/local/bin/python3
激活虚拟环境
source /home/sam/envs/nb/bin/activate
启动完虚拟环境后,就可以愉快的进行pip安装相关的库了
#虚拟环境里用python3安django和uwsgi
pip3 install django==2.1.7 #指定版本,和项目一致,不然可能会出错
pip3 install uwsgi
11、uwsgi
安装:
source /home/sam/envs/nb/bin/activate
pip install uwsgi
基于uwsgi运行项目:
cd 项目目录
命令的方式:
uwsgi --http :8080 --wsgi-file app.py --callable app
配置文件(推荐):
nb_uwsgi.ini
[uwsgi]
socket = 127.0.0.1:8001
chdir = /data/www/day28/
wsgi-file = app.py
callable = app
processes = 1
virtualenv = /envs/nb/
source /envs/nb/bin/activate
uwsgi --ini nb_uwsgi.ini
停止:
ps -ef|grep nb_uwsgi
kill -9 12648