首页 > 解决方案 > 如何在 aws ec2 中安装带有 nss 后端的 libcurl?(Python 3.6 64 位亚马逊 Linux)

问题描述

我在 AWS 中有一个运行 Python3.6 (Amazon Linux/2.8.3) 的 ec2 实例,我需要在其中安装 pycurl 和 NSS ssl backend

首先我通过添加pycurl==7.43.0 --global-option="--with-nss"到我的requirement.txt文件来尝试它,但我收到错误安装错误。所以我最终通过添加一个.config文件.ebextensions(在部署期间运行)来做到这一点:

container_commands:
  09_pycurl_reinstall:
    # the upgrade option is because it will run after PIP installs the requirements.txt file.
    # and it needs to be done with the virtual-env activated
    command: 'source /opt/python/run/venv/bin/activate && PYCURL_SSL_LIBRARY=nss pip3 install pycurl --global-option="--with-nss" --upgrade --no-cache-dir --compile --ignore-installed'


Pycurl 似乎安装正确,但 celery worker 没有运行。芹菜工人日志显示:

__main__.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory

如果我 ssh 连接到实例并运行,python 3.6 -c 'import pycurl'我会收到更详细的错误:

ImportError: pycurl: libcurl link-time ssl backend (openssl) is different from compile-time ssl backend (nss)

所以我想我的问题是我之前用 openSSL 而不是 NSS 安装了 libcurl,因此 libcurl 和 pycurl 之间不匹配。


根据另一个stackoverflow问题,对于要与NSS后端一起安装的libcurl,我应该安装它:

sudo apt libcurl4-nss-dev

但由于服务器运行的是 Amazon Linux,我无法使用该apt命令。所以我改为:

yum install libcurl-devel

我想这就是问题所在:当我需要 NSS 支持时,它会安装带有 OpenSSL 支持的 libcurl。

如何在 Amazon Linux 中使用 NSS 安装 libcurl?(我需要 NSS,因为我正在使用 SQS 作为代理运行带有 celery 的 Django 应用程序,而 SQS 需要 NSS)。

非常感谢!

标签: pythonamazon-web-servicesamazon-ec2libcurlpycurl

解决方案


我刚刚遇到了同样的问题并设法解决了它:)

  1. 检索 Amazon Linux 配置选项libcurl
curl-config --configure

可以忽略所有引用路径的选项,但如果您想要与 system 相同的功能,则必须保留其他选项libcurl。当然--with-ssl会换成--without-ssl --with-nss.

1.1 安装先决条件:

sudo yum install libssh2-devel nss-devel

(当然,您应该将 then 添加到您的 ebextensions 的packages>yum部分)

  1. 从源代码编译libcurl(我选择了 7.61.1 以匹配 Amazon Linux 2018.03 使用的那个):
wget https://curl.haxx.se/download/curl-7.61.1.tar.gz
tar xf curl-7.61.1.tar.gz
cd curl-7.61.1
./configure '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--target=x86_64-amazon-linux-gnu' '--program-prefix=' '--cache-file=../config.cache' '--disable-static' '--enable-symbol-hiding' '--enable-ipv6' '--enable-threaded-resolver' '--with-gssapi' '--with-nghttp2' '--without-ssl' '--with-nss' '--with-ca-bundle=/etc/pki/tls/certs/ca-bundle.crt' '--enable-ldap' '--enable-ldaps' '--enable-manual' '--with-libidn2' '--with-libpsl' '--with-libssh2' 'build_alias=x86_64-redhat-linux-gnu' 'host_alias=x86_64-redhat-linux-gnu' 'target_alias=x86_64-amazon-linux-gnu' 'CFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
make
sudo make install

(当然,这应该通过files您的 ebextensions 部分正确部署为 Shell 脚本)。

现在您应该看到libcurl.so.4created in /usr/local/lib

  1. 定义以下 envvarspycurl以使用您的自定义进行编译libcurl
LD_LIBRARY_PATH=/usr/local/lib
PYCURL_CURL_CONFIG=/usr/local/bin/curl-config
PYCURL_SSL_LIBRARY=nss
  1. 运行你的pip install

您可以检查pycurl链接到右侧libcurl

ldd /opt/python/run/venv/local/lib64/python3.6/site-packages/pycurl.cpython-36m-x86_64-linux-gnu.so

应该告诉你libcurl.so.4 => /usr/local/lib64/libcurl.so.4

当然python 3.6 -c 'import pycurl'应该工作。

而已!您应该能够使用 SQS 运行 Celery。


推荐阅读