为Ubuntu 16.04 添加永久免费https SSL证书(解决python2.7 - pip wheel failed with error code 2 问题)

本文介绍如何使用Let'sencrypt获取免费SSL证书,并提供了一键生成及自动续期的脚本示例。此外还讨论了Python2与Python3环境间的转换问题。
部署运行你感兴趣的模型镜像

SSL证书这么贵,自签名证书这么不受浏览器待见,为什么不用Let’s encrypt免费证书呢?而且这个证书基本上一键生成,下面是方法。

下载let’s encrypt客户端

git clone https://github.com/certbot/certbot

进入下载的目录,执行自动脚本:

./certbot-auto --apache -d abc.com -d www.abc.com

2021 更新:

certbot-auto 命令已经废除,使用新命令:

certbot --nginx -d abc.com -d www.abc.com

输入email之类的信息,就可以完成了!!!
检测一下看看:https://www.ssllabs.com/ssltest/analyze.html?d=abc.com&latest
是最高等级的评级!

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/abc.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/abc.com/privkey.pem
   Your cert will expire on 2018-02-02. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again with the "certonly" option. To non-interactively renew *all*
   of your certificates, run "certbot-auto renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

证书更新

先切换到Python2,切换python2/3,点这里

./certbot-auto certonly --apache --renew-by-default -d abc.com -d www.abc.com

转发一个自动更新的脚本:

#!/bin/bash
#================================================================
# Let's Encrypt renewal script for Apache on Ubuntu/Debian
# @author Erika Heidi<erika@do.co>
# Usage: ./le-renew.sh [base-domain-name]
#================================================================
domain=$1
le_path='/opt/letsencrypt'
le_conf='/etc/letsencrypt'
exp_limit=30;

get_domain_list(){
        certdomain=$1
        config_file="$le_conf/renewal/$certdomain.conf"
        
        if [ ! -f $config_file ] ; then
                echo "[ERROR] The config file for the certificate $certdomain was not found."
                exit 1;
        fi

        domains=$(grep --only-matching --perl-regex "(?<=domains \= ).*" "${config_file}")
        last_char=$(echo "${domains}" | awk '{print substr($0,length,1)}')

        if [ "${last_char}" = "," ]; then
                domains=$(echo "${domains}" |awk '{print substr($0, 1, length-1)}')
        fi

        echo $domains;
}

if [ -z "$domain" ] ; then
        echo "[ERROR] you must provide the domain name for the certificate renewal."
        exit 1;
fi

cert_file="/etc/letsencrypt/live/$domain/fullchain.pem"

if [ ! -f $cert_file ]; then
        echo "[ERROR] certificate file not found for domain $domain."
        exit 1;
fi

exp=$(date -d "`openssl x509 -in $cert_file -text -noout|grep "Not After"|cut -c 25-`" +%s)
datenow=$(date -d "now" +%s)
days_exp=$(echo \( $exp - $datenow \) / 86400 |bc)

echo "Checking expiration date for $domain..."

if [ "$days_exp" -gt "$exp_limit" ] ; then
        echo "The certificate is up to date, no need for renewal ($days_exp days left)."
        exit 0;
else
        echo "The certificate for $domain is about to expire soon. Starting renewal request..."
        domain_list=$( get_domain_list $domain )
        "$le_path"/letsencrypt-auto certonly --apache --renew-by-default --domains "${domain_list}"
        echo "Restarting Apache..."
        /usr/sbin/service apache2 reload
        echo "Renewal process finished for domain $domain"
        exit 0;
fi

两个问题:

  • 一键生成SSL证书的脚本是用python 2写的,然后oj需要python3的支持。如何在SSL证书到期自动生成的脚本中加入python2 、3之间的自动转换(即生成证书前把python3转到python2,生成自动转3)
  • http强制跳转https有何潜在问题
第一个问题:

certbot脚本基于python2,当系统里有python2 和python3时,会报错:

OSError: Command /root/.local/share/letsencrypt/bin/python2.7 - setuptools pkg_resources pip wheel failed with error code 2
Let's Encrypt returned an error status. Aborting.

解决方法1是升级pip,参考前3步:
https://github.com/interbrite/letsencrypt-vesta/issues/46#issuecomment-273014451

解决方法2,3是更新系统语言或者apt按照letsencrypt,更新语言尝试了但是没用。apt安装?~看着太混乱,没试:
https://github.com/certbot/certbot/issues/4062#issuecomment-273236106

解决办法4,重新安装virtualenv环境(有效):
先卸载:

apt-get purge python-virtualenv python3-virtualenv virtualenv

再安装:

pip install virtualenv

注意,安装在python2环境下,运行certbot命令后又会安装virtualenv环境

切换python2/3,点这里

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

<think>我们正在使用Ubuntu 16.04系统,并且想要通过apt安装python2.7-dev。 根据用户的问题,他明确要求使用apt安装,并且指定了版本为2.7。 在Ubuntu中,python2.7-dev是用于Python 2.7开发的包,它包含了开发Python扩展模块所需的头文件等。 步骤: 1. 更新软件包列表(以确保获取最新的包信息) 2. 安装python2.7-dev 注意:在Ubuntu 16.04中,默认的Python 2版本就是2.7,因此我们可以直接安装。 命令如下: ``` sudo apt update sudo apt install python2.7-dev ``` 另外,根据引用[1]和引用[2]中提供的信息,虽然它们不是直接关于python2.7-dev的,但我们可以看到在安装其他软件时也使用了类似的apt命令。因此,我们可以确认使用apt安装的方法是可行的。 但是,用户可能还需要注意,如果系统中有多个Python版本,安装后需要确认是否安装成功。 验证安装: 安装完成后,可以检查/usr/include/python2.7目录是否存在,或者尝试导入distutils模块(因为python-dev包中包含了distutils模块所需的内容)。 例如,可以运行: ``` python2.7 -c "import distutils; print(distutils.__file__)" ``` 如果输出了distutils模块的路径,则说明安装成功。 注意:在Ubuntu 16.04中,默认可能已经安装了Python 2.7,但python2.7-dev包是单独的需要安装的。 因此,回答如下: </think> 在Ubuntu 16.04系统中,使用`apt`安装`python2.7-dev`的步骤如下: ### 安装步骤 1. **更新软件包索引**(确保获取最新版本信息): ```bash sudo apt update ``` 2. **安装python2.7-dev**: ```bash sudo apt install python2.7-dev ``` 3. **验证安装**: ```bash dpkg -l | grep python2.7-dev ``` 若输出类似: ``` ii python2.7-dev 2.7.12-1~16.04 amd64 Header files for Python 2.7 ``` 表示安装成功。也可检查头文件目录: ```bash ls /usr/include/python2.7/Python.h ``` ### 注意事项 - Ubuntu 16.04默认预装Python 2.7,但`python2.7-dev`需单独安装。 - 若遇到依赖问题,可先修复依赖关系: ```bash sudo apt --fix-broken install ``` - 安装后如需使用`pip`管理Python 2.7的包,需额外安装: ```bash sudo apt install python-pip ``` ### 补充说明 `python2.7-dev`包含Python 2.7的头文件和静态库,用于编译C/C++扩展模块(如通过`pip`安装需要编译的Python包)[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值