Redhat/SUSE旧系统安装支持SSL的Python 3.6+

776 阅读1分钟

概述

随着2020年1月Python 2完成历史使命,正式停止官方支持,即使是之前一直对Python 3无友好原生支持的Redhat系Linux系统也在新版本中对Python 3.6+的版本有了较好的支持, 例如CentOS 8RHEL 8

如果由于legacy的原因,你还在使用RHEL 7.6或者SLES 12这样的老系统,并且一时半会儿无法升级到新系统,那么可以尝试以下方法安装较高版本的Python 3,并且支持SSL.

SLES 12安装方法

SLES 12系统默认的Python 33.4,按照其Release Schedule, 已终止维护, 可以通过源代码来安装更高版本的Python 3

# 安装Build Python所需依赖, 包括openssl-devel
$ sudo zypper refresh
$ sudo zypper install -y gcc gcc-c++ make ncurses patch wget tar zlib-devel zlib openssl-devel
# 下载Python 3.6.9源码
$ PYTHON_VERSION="3.6.9"
$ PYTHON_SRC_DIR=$(mktemp -d)
$ wget -qO- https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz | tar -xz -C "$PYTHON_SRC_DIR"
# Build Python
$ cd $PYTHON_SRC_DIR/Python-$PYTHON_VERSION
# --prefix=/usr会覆盖系统的/usr/bin/python3, 如果不希望如此,可以使用默认的/usr/local或者其他目录
$ ./configure --with-ssl --prefix=/usr
$ make
$ sudo make install

此时可以通过键入python3启动console,并import ssl来验证是否已支持SSL

RHEL 7.6安装方法

RHEL 7.6上我们尝试让Python 3.6SSL 1.1.1进行支持,也使用从源代码安装的方式,配置会略复杂一些。
先build openssl:

# 安装Build Python和openssl所需依赖
$ sudo yum install gcc gcc-c++ make ncurses patch wget tar zlib zlib-devel -y
$ cd ~
# 先build openssl 1.1.1
$ wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz
$ tar -xzf openssl-1.1.1d.tar.gz
$ cd openssl-1.1.1d
$ ./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl
$ make
$ sudo make install
# 配置shared object, 否者python build时会找不到libssl.so.1.1
# 也有教程通过配置环境变量:export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/ssl/lib来解决,但是我试了不起作用
$ cd /etc/ld.so.conf.d/
$ sudo vi openssl-1.1.1d.conf
# 加入以下内容
/usr/local/ssl/lib
# 重载配置
$ sudo ldconfig -v

再build python:

$ PYTHON_VERSION="3.6.9"
$ PYTHON_SRC_DIR=$(mktemp -d)
$ wget -qO- https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz | tar -xz -C "$PYTHON_SRC_DIR"

需要修改setup配置

$ cd $PYTHON_SRC_DIR/Python-$PYTHON_VERSION
$ vi Modules/Setup.dist

搜索ssl并去掉以下内容的注释

_socket socketmodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I(SSL)/include/opensslL(SSL)/include/openssl \\** **-L(SSL)/lib -lssl -lcrypto

最后configure and make

$ ./configure --prefix=/usr --with-openssl=/usr/local/ssl
$ make
$ sudo make install

总结

在Linux旧系统上如果要使用较高版本的Python 3,一般可以通过从源代码构建安装的方式,默认情况下不带有对SSL的支持,需要在configure以及Setup中配置。