• 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
    
    • 1
    • 2
    • 3

    准备环境

    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
    
    • 1
    • 2
    • 3
    • 4

    显示结果

    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)           ***
    ***                                                                ***
    **********************************************************************
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    make && make install && make clean
    
    • 1
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    #  pip安装uwsgi时的报错信息。
        /usr/bin/ld: 找不到 -lssl
        /usr/bin/ld: 找不到 -lcrypto
        collect2: 错误:ld 返回 1
        *** error linking uWSGI ***
    
    • 1
    • 2
    • 3
    • 4
    • 5
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 验证版本
    openssl version -a
    
    • 1
    # 显示结果
    OpenSSL 1.1.1d  10 Sep 2019
    
    • 1
    • 2
    1. 卸载系统安装openssl-devel以免版本冲突(如果系统已安装了)
    yum remove openssl-devel
    
    • 1
    安装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
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 配置sqlite3
    mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
    ln -s /usr/local/sqlite3.43/bin/sqlite3 /usr/bin/sqlite3
    
    • 1
    • 2
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 验证版本
    sqlite3 --version 
    
    • 1
    # 显示结果
    3.43.0 2023-08-24 12:36:59 0f80b798b3f4b81a7bb4233c58294edd0f1156f36b6ecf5ab8e83631d468778c (64-bit)
    
    • 1
    • 2

    安装依赖包

    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
    
    • 1

    安装Python

    1. 准备文件
    tar -zxvf Python-3.11.5.tgz
    cd Python-3.11.5
    
    • 1
    • 2
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    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
    • 2
    1. 验证结果
    python3.11
    
    • 1

    显示结果

    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
    >>> 
    
    • 1
    • 2
    • 3
    • 4

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    修改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/
    
    • 1
    • 2
    • 3

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

    mkdir ~/.pip
    vim ~/.pip/pip.conf
    
    • 1
    • 2

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

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

    安装Django

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

    显示结果

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 创建项目
    cd /opt/dev
    /opt/dev/venv/django_test/bin/django-admin startproject django_test
    
    • 1
    • 2

    显示结果

    (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
    • 2
    • 3
    • 4
    1. 启动项目
    python3.11 manage.py runserver
    
    • 1

    显示结果

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    ROS1云课→14可视化交互
    一文读懂 spring MVC 请求处理流程
    三次握手时,客户端发送的 SYN 报文为什么会被丢弃?
    基于opencv+tensorflow+神经网络的智能银行卡卡号识别系统——深度学习算法应用(含python、模型源码)+数据集(三)
    js常用方法之 slice
    基于光流的视频插帧算法 TOFlow 解读教程
    cubemx stm32 lu90614模块 人体温度检测模块 可以替代温枪 驱动代码
    【Android进阶】4、UI状态的保存和恢复
    vue3父子通信+ref,toRef,toRefs使用实例
    vue小技能:组件间的数据传递
  • 原文地址:https://blog.csdn.net/shijin741231/article/details/132676104