You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu16升级Python后SSL模块缺失、Nginx故障求助

解决Ubuntu 16服务器Python升级与Nginx、SSL问题的分步方案

一、先修复Nginx启动问题

Nginx报错找不到/etc/letsencrypt/options-ssl-nginx.conf,是卸载OpenSSL时误删或破坏了Let's Encrypt配置文件,按以下步骤修复:

  • 检查Certbot状态:dpkg -l | grep certbot
    • 若未安装,重新安装适配Ubuntu16的Certbot:apt-get update && apt-get install certbot python-certbot-nginx
    • 若已安装,重新生成缺失配置:certbot renew --dry-run,该命令会自动修复Let's Encrypt相关Nginx配置
  • 验证Nginx配置:nginx -t,无报错则重启服务:systemctl restart nginx

二、解决Python的SSL模块与版本兼容问题

Ubuntu16默认OpenSSL 1.0.2g不满足Python3.11要求,源码装OpenSSL3.0.x出现兼容问题,优先用OpenSSL1.1.1系列(LTS版本)适配Python3.11:

步骤1:安装OpenSSL 1.1.1(自定义路径,不覆盖系统默认)

# 下载最新1.1.1分支源码
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar -zxf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
# 编译安装到自定义目录
./config --prefix=/usr/local/openssl-1.1.1w --openssldir=/usr/local/openssl-1.1.1w shared zlib
make -j$(nproc)
make install
# 创建软链接让系统识别新库
ln -s /usr/local/openssl-1.1.1w/lib/libssl.so.1.1 /usr/lib/x86_64-linux-gnu/libssl.so.1.1
ln -s /usr/local/openssl-1.1.1w/lib/libcrypto.so.1.1 /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
# 更新系统库缓存
ldconfig

步骤2:重新编译Python3.11并指定OpenSSL路径

# 卸载之前安装的Python3.11(源码安装的进入源码目录执行make uninstall)
apt-get remove --purge python3.11
# 安装Python编译依赖
apt-get install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
# 下载Python3.11源码
wget https://www.python.org/ftp/python/3.11.4/Python-3.11.4.tgz
tar -zxf Python-3.11.4.tgz
cd Python-3.11.4
# 指定OpenSSL路径编译
./configure --prefix=/usr/local/python3.11 --with-openssl=/usr/local/openssl-1.1.1w --with-openssl-rpath=auto
make -j$(nproc)
make install
# 创建软链接切换默认Python3版本
ln -sf /usr/local/python3.11/bin/python3.11 /usr/bin/python3
# 验证SSL模块
python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"

执行验证命令后,应显示OpenSSL 1.1.1w,表示SSL模块正常。

步骤3:修复Python3.5的pip SSL问题

若需保留系统默认Python3.5:

# 恢复系统默认OpenSSL软链接
rm /usr/lib/x86_64-linux-gnu/libssl.so.1.1
rm /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
ln -s /lib/x86_64-linux-gnu/libssl.so.1.0.0 /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0
ln -s /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0
# 重新安装Python3.5的pip
apt-get install --reinstall python3-pip
# 验证
python3.5 -m pip --version

三、后续避坑建议

  • 不要卸载系统默认OpenSSL,Ubuntu16大量组件依赖1.0.2g,用自定义路径安装高版本OpenSSL,编译Python时指定路径即可
  • 用update-alternatives管理多Python版本,避免直接覆盖系统默认链接:
    update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
    update-alternatives --install /usr/bin/python3 python3 /usr/local/python3.11/bin/python3.11 2
    
    后续可通过update-alternatives --config python3切换版本

内容的提问来源于stack exchange,提问作者Brian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 02:05:24