the5fire

关注Python、Django、Vim、Linux、Web开发、团队管理和互联网--Life is short, we need Python.


编译Python3.7并配置ssl库为LibreSSL

作者:the5fire | 标签:         | 发布:2018-07-24 9:48 p.m. | 阅读量: 13357, 11075

背景

重新安装Python 3.7.0的版本。

按照之前的套路,安装系统依赖:

  yum install bzip2-devel python-devel libffi-dev sqlite-devel libuuid-devel readline-devel mysql-devel -y

下载源码,手动编译:./configure --prefix=/opt/python/,安装: make && make install

一切顺利。

问题

在本地执行fab redeploy:python3.7(编写好了fabric脚本,改了创建虚拟环境的命令为python3.7 -m venv),结果:

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")'

解决

做为Python老鸟,这样的错误显然遇到过不少次,方案如下:

1. 手动编译安装openssl包,放到/opt/openssl下。

2. 配置编译Python时的环境变量:

export LDFLAGS="-L/opt/openssl/lib"
export CPPFLAGS="-I/opt/openssl/include"
export PKG_CONFIG_PATH="/opt/openssl/lib/pkgconfig"

3. 再次make

这里我用的是openssl的1.0.2o的版本,奇怪的是make的时候会有warning:

Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381

不太懂为什么不匹配。

不过既然warning中提到了LibreSSL了,索性去看下这个东西。

LibreSSL是OpenSSL加密软件库的一个分支,是一个安全套接层(SSL)和传输层安全(TLS)协议的开源实现。在OpenSSL爆出心脏出血安全漏洞之后,一些OpenBSD开发者于2014年4月创立了LibreSSL,目标是重构OpenSSL的代码,以提供一个更安全的替代品。LibreSSL复刻自OpenSSL库的1.0.1g分支,它将遵循OpenBSD基金会在其他项目所使用的安全指导原则。

--- 维基百科

它的Logo很有寓意,滴血的心脏(Heartbleed bug)。

于是下载了并安装了LibreSSL,重复上面的逻辑,make时没warning。安装成功。

不过安装完后运行python3.7:import ssl还是会报错:

    import _ssl             # if we can't import it, let the error propagate
ImportError: libssl.so.45: cannot open shared object file: No such file or directory

最终还需要配置:export LD_LIBRARY_PATH=/libressl/lib(你自己的安装路径)。把这个命令放到/etc/bashrc中。

参考

补充

可能还需要修改源码包中的Module/Setup文件,取消对应的注释:

SSL=/usr/local/ssl
_ssl _ssl.c \
        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
        -L$(SSL)/lib -lssl -lcrypto
- from the5fire.com
----EOF-----

微信公众号:Python程序员杂谈


其他分类: