给网站加上免费的https证书

2017-09-04 17:23:52

    目前,网站https应该说是大势所趋。微信小程序,以及支付宝小程序都已硬性要求https必须。所以我们还是得花些功夫搞一个https证书安装到网站上为好。

    但是市场上的https证书真的是太贵了,我等小站一年花个千把块搞个证书,确实不怎么划算。当然你要是土豪那就另说了。最终我们还是决定使用 Let's Encrypt 提供的免费证书,小站嘛。成本当然是放在第一位的啦。

     Let's Encrypt 开源地址为 Let's Encrypt ,项目初衷是为了推进https的发展。真是一个有情怀的组织啊,我要不是没钱真的打算去捐赠了...

    闲话不多说了,开搞。

    首先当然是把项目下载下来了

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

下载完以后,发现certbot文件夹中有一个certbot-auto可执行文件,尝试执行一下,报错

Creating virtual environment...
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 2363, in <module>
    main()
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 719, in main
    symlink=options.symlink)
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 988, in create_environment
    download=download,
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 918, in install_wheel
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 812, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /root/.local/share/letsencrypt/bin/python2.7 - setuptools pkg_resources pip wheel failed with error code 2

    应该是virtualenv环境出现了问题,尝试重新安装以后解决

pip install virtualenv
sudo git clone https://github.com/certbot/certbot.git

    再尝试运行certbot-auto

Installing Python packages...
Installation succeeded.
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Failed to find executable apache2ctl in PATH: /usr/java/jdk/bin:/usr/java/jdk/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Certbot doesn't know how to automatically configure the web server on this system. However, it can still get a certificate for you. Please run "certbot-auto certonly" to do so. You'll need to manually configure your web server to use the resulting certificate.

    发现正常运行了,出现报错,是因为我们还没有指定参数啦。

./certbot-auto certonly --standalone --email admin@example.com -d example.com -d www.example.com -d other.example.net

    执行下面命令即可生成相应的证书,注意生成证书的路径。我们需要记住它,并且在nginx在指定证书,这样我们的网站就可以愉快的使用https证书啦。

    因为证书的有效期仅仅只有三个月的时间,所以我们需在做一个脚本自动更新一下我们的证书。相应更新命令为:

./certbot-auto renew --force-renew

    接下来说一下nginx中的配置

server {
        listen 443 ssl;
        listen [::]:443 ssl ipv6only=on;

        ssl_certificate /etc/letsencrypt/live/your.domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/your.domain.com/privkey.pem;
        ssl_trusted_certificate /etc/letsencrypt/live/your.domain.com/chain.pem;        
        // ... other settings ...}

    注意:1.生成证书的时候,要保证web服务处于关闭状态

            2.最好不要动证书的默认生成路径,这样的话下次更新的时候会方便许多。

            也许是可以指定生成位置的,就不研究它了。