CentOS 7上安装Python 3.11.5,支持Django

CentOS 7上安装Python 3.11.5,支持Django

今天安装django,报了“Django - deterministic=True requires SQLite 3.8.3 or higher upon running python manage.py runserver”。查了一番资料,记录下来。

参考链接:

参考链接: Django的web项目部署至Centos7服务器并配置域名访问.
参考链接: Centos7安装Python3和升级SQLite高版本.
参考链接: configure指定编译头文件和库文件路径.

安装要求

  1. CentOS 7.9
  2. Python 3.11.5
  3. Django 4.2.5

安装步骤

下载文件

wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz 
wget https://www.sqlite.org/2023/sqlite-autoconf-3430000.tar.gz --no-check-certificate
wget https://www.python.org/ftp/python/3.11.5/Python-3.11.5.tgz

准备环境

Centos 7可使用yum直接升级openssl-devel至1.0.2,正常可以直接装python3.11,如果无法导入ssl可采用下列编译安装openssl。

安装openssl
  1. 编译安装openssl
tar -zxvf openssl-1.1.1d.tar.gz
cd openssl-1.1.1d
#./config --prefix=/usr/local/openssl no-zlib      #不需要zlib
./config --prefix=/usr/local/openssl

显示结果

Operating system: x86_64-whatever-linux2
Configuring OpenSSL version 1.1.1d (0x1010104fL) for linux-x86_64
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                      ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                 ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL file first)           ***
***                                                                ***
**********************************************************************
make && make install && make clean
  1. 配置openssl
mv /usr/bin/openssl /usr/bin/openssl.bak
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
# 直接添加link,否则装包的时候经常找不到库,比如装uwsgi
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/local/lib64/libcrypto.so
#  pip安装uwsgi时的报错信息。
    /usr/bin/ld: 找不到 -lssl
    /usr/bin/ld: 找不到 -lcrypto
    collect2: 错误:ld 返回 1
    *** error linking uWSGI ***
  1. 修改系统配置
#写入openssl库文件的搜索路径
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf.d/openssl-x86_64.conf 
#使修改后的/etc/ld.so.conf生效
ldconfig -v
#查看所需的动态库
ldd /usr/local/openssl/bin/openssl
  1. 验证版本
openssl version -a
# 显示结果
OpenSSL 1.1.1d  10 Sep 2019
  1. 卸载系统安装openssl-devel以免版本冲突(如果系统已安装了)
yum remove openssl-devel
安装SQLite3
  1. 编译安装sqlite3
tar -zxvf sqlite-autoconf-3430000.tar.gz
cd sqlite-autoconf-3430000/
mkdir /usr/local/sqlite3.43
./configure --prefix=/usr/local/sqlite3.43/
make && make install
make clean
  1. 配置sqlite3
mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
ln -s /usr/local/sqlite3.43/bin/sqlite3 /usr/bin/sqlite3
  1. 修改系统配置
#写入sqlite3库文件的搜索路径
echo "/usr/local/sqlite3.43/lib" > /etc/ld.so.conf.d/sqlite3.conf 
#使修改后的/etc/ld.so.conf生效
ldconfig -v
#查看所需的动态库
ldd /usr/local/sqlite3.43/bin/sqlite3 
  1. 验证版本
sqlite3 --version 
# 显示结果
3.43.0 2023-08-24 12:36:59 0f80b798b3f4b81a7bb4233c58294edd0f1156f36b6ecf5ab8e83631d468778c (64-bit)

安装依赖包

yum -y install gcc gcc-c++ zlib-devel bzip2-devel  ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel  expat-devel gdbm-devel  make

安装Python

  1. 准备文件
tar -zxvf Python-3.11.5.tgz
cd Python-3.11.5
  1. 编译安装
# 需要配置LD_RUN_PATH指定依赖的搜索目录,LDFLAGS指定库文件路径,CPPFLAGS指定头文件路径
# 如果是编译安装了openssl,需要指定新版路径
./configure LD_RUN_PATH=usr/local/sqlite3.43/lib LDFLAGS="-L/usr/local/sqlite3.43/lib" CPPFLAGS="-I/usr/local/sqlite3.43/include" --prefix=/usr/local/python3.11 --with-openssl=/usr/local/openssl 

# 编译并安装 altinstall 不自动创建链接,需要手动创建,建议使用,保障多个版本共存。
make && make  altinstall 
make clean
make distclean
  1. 建立链接
ln -s /usr/local/python3.11/bin/python3.11 /usr/local/bin/
ln -s /usr/local/python3.11/bin/pip3.11 /usr/local/bin/
  1. 验证结果
python3.11

显示结果

Python 3.11.5 (main, Sep  4 2023, 19:12:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> 

5、补充说明
之前在Centos6.8上安装Python3.8遇到过死活不能import ssl
参考了https://blog.csdn.net/weixin_30951743/article/details/99891139 .最后成功安装,留做备用信息。

# cd openssl-1.1.1d
# ./config --prefix=$HOME/openssl shared zlib   # 这里不同,装了shared zlib 和 $HOME路径
# make && make install

# echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/openssl/lib" >> $HOME/.bash_profile
# source $HOME/.bash_profile
# cat $HOME/.bash_profile          

# cd Python-3.8.3
# ./configure --prefix=/usr/local/python3.8 --with-openssl=$HOME/openssl
# make && make  altinstall 
# make clean
# make distclean

升级PIP

python3.11 -m pip install --upgrade pip

修改PIP源

指定公司内部源(生产环境不能访问外网)
pip3.11 config set global.trusted-host 10.100.224.65 
pip3.11 config set global.index-url http://10.100.224.65/root/pypi/+simple/
pip3.11 config set search.index http://10.100.224.65/root/pypi/
pip3.11 config set global.trusted-host http://pypi.douban.com/simple/

或者直接修改文件也可以.
参考: https://www.cnblogs.com/cou1d/p/12403097.html.

mkdir ~/.pip
vim ~/.pip/pip.conf

增加以下内容(以阿里源为例)

[global]
timeout = 6000  
index-url = http://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
临时指定源安装(以豆瓣源为例)
pip3.11 install pygame -i https://pypi.doubanio.com/simple

安装Django

  1. 创建沙箱
python3.11 -m venv /opt/dev/venv/django_test
  1. 安装Django
pip3.11 install django
pip3.11 list

显示结果

Package            Version
------------------ ---------
asgiref            3.7.2
certifi            2023.7.22
charset-normalizer 3.2.0
Django             4.2.5
idna               3.4
pip                23.2.1
PyMySQL            1.1.0
requests           2.31.0
setuptools         65.5.0
sqlparse           0.4.4
urllib3            2.0.4
  1. 创建项目
cd /opt/dev
/opt/dev/venv/django_test/bin/django-admin startproject django_test

显示结果

(django_test) [root@centos7-18 django_test]# pwd
/opt/dev/django_test
(django_test) [root@centos7-18 django_test]# ls
db.sqlite3  django_test  manage.py
  1. 启动项目
python3.11 manage.py runserver

显示结果

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 04, 2023 - 12:11:22
Django version 4.2.5, using settings 'django_test.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值