Centos編譯Python3.10解決openssl異常

艳沐石發表於2024-11-20

問題描述

在Linux中進行Python應用部署時,安裝Python3.10後,在pip安裝依賴出現SSLError異常。

(venv) [root@server100 flask-app]# pip install flask
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
WARNING: 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.")': /pypi/simple/flask/
WARNING: Retrying (Retry(total=3, 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.")': /pypi/simple/flask/
WARNING: Retrying (Retry(total=2, 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.")': /pypi/simple/flask/
WARNING: Retrying (Retry(total=1, 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.")': /pypi/simple/flask/
WARNING: Retrying (Retry(total=0, 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.")': /pypi/simple/flask/
Could not fetch URL https://mirrors.aliyun.com/pypi/simple/flask/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='mirrors.aliyun.com', port=443): Max retries exceeded with url: /pypi/simple/flask/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement flask (from versions: none)
ERROR: No matching distribution found for flask
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://mirrors.aliyun.com/pypi/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='mirrors.aliyun.com', port=443): Max retries exceeded with url: /pypi/simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

問題原因

Python3.10引用的openssl是1.1+版本,而當前yum安裝的openssl為1.0.2k版本。

解決方法

兩種解決方法:

  • 修改configura檔案中openssl11的引用
  • 手動編譯openssl

前置準備


# 安裝python需要的依賴
sudo yum groupinstall "Development Tools" -y
sudo yum install -y openssl-devel bzip2-devel libffi-devel

# uWSGI 需要 EPEL(Extra Packages for Enterprise Linux)倉庫。你可以透過以下命令安裝 EPEL:
# 編譯openssl時也需要
sudo yum install epel-release -y

方法一:yum安裝openssl11(成功)

# 配置ssl
yum install -y epel-release.noarch
yum install -y openssl11 openssl11-devel

# 替換openssl為openssl11
sed -i 's/PKG_CONFIG openssl /PKG_CONFIG openssl11 /g' configure

# 重新編譯,安裝
sudo ./configure --enable-optimizations

sudo make altinstall

方法二:手動編譯openssl(未成功)

安裝版本:3.0.14

# 安裝perl
yum install -y gcc gcc-c++ zlib-devel libtool autoconf automake perl perl-IPC-Cmd perl-Data-Dumper perl-CPAN

./config --prefix=/usr/local/openssl shared zlib 
make && make install

# 配置LD_LIBRARY_PATH
echo "export LD_LIBRARY_PATH=/usr/local/openssl/lib64:$LD_LIBRARY_PATH" >> ~/.bashrc
echo "export PKG_CONFIG_PATH=/usr/local/openssl/lib64/pkgconfig" >> ~/.bashrc
source ~/.bashrc

# 配置ldconfig
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
echo "/usr/local/openssl/lib64" >> /etc/ld.so.conf
ldconfig -v

./configure --enable-optimizations --with-openssl=/usr/local/openssl
sudo make altinstall

相關文章