An Openssl Error When Upgrade Scrapy

今天,在更新 Scrapy 时,出现了一个错误:

1
2
3
4
5
6
7
building '_openssl' extension
gcc -pthread -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -fPIC -I/usr/local/include/python2.7 -c build/temp.linux-x86_64-2.7/_openssl.c -o build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7/_openssl.o
gcc -pthread -shared -fPIC build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7/_openssl.o -lssl -lcrypto -o build/lib.linux-x86_64-2.7/cryptography/hazmat/bindings/_openssl.so
/usr/bin/ld: /usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../lib64/libssl.a(s2_meth.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../lib64/libssl.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1

很显然,是 openssl 出了问题。于是

1
2
3
$ sudo aptitude show openssl
$ openssl version
OpenSSL 1.0.2d 9 Jul 2015

查看openssl包状态,没有问题啊。再用命令 which openssl 一看,

1
2
$ which openssl 
/usr/local/bin/openssl

原来这个版本的 openssl 根本不是系统自带的版本,而系统自带的 openssl 根本没有问题。之前在 nginx 上折腾 HTTP/2 时,自己编译安装了一个新版本的 openssl 在 /usr/local/ 目录下,而 /usr/bin/ld 加载动态链接库时,根据 /etc/ld.so.conf 里面的配置, 先在 /usr/local/lib 目录查找。根据上面的错提示,此版本的 openssl 需要用 -fPIC 参数重新编译才行。

  • -fPIC 参数重新编译 openssl;
  • 编辑 /etc/ld.so.conf,将 /usr/local/lib/usr/local/lib64 两行换到该文件末尾。完了再换回来,否则影响其他自编译安装的动态库的加载顺序。

当然,一劳永逸的方法,就是重新编译 openssl 了。