如何使用开源SSL提供商Let's Encrypt来保护Apache
本文将学习如何在Ubuntu服务器上使用“Let's Encrypt”为Apache Web服务器设置TLS/SSL证书,还将介绍如何使用cron作业自动更新证书。
先决条件
要完成此演示,我们需要满足以下要求
- 具有sudo权限的用户在Ubuntu服务器上。
- 已安装Apache服务器,并配置了正确的域名。
- 在服务器上安装依赖项
我们需要使用以下命令更新服务器,并安装git以从存储库获取Let's Encrypt客户端。
$ sudo apt-get update
$ sudo apt-get install git
将Let's Encrupt客户端下载到服务器
这里我们将从存储库下载“Let's Encrypt”客户端。我们需要将文件保存在单独的位置。由于Let's Encrypt客户端仅处于测试阶段。不建议在生产环境中使用,我们可以将此SSL证书用于开发和测试目的,因此可能需要频繁更新以修复错误并实现新功能。
让我们将代码下载到/opt目录中,运行以下命令以从github存储库获取更新的代码
$ sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt Cloning into '/opt/letsencrypt'... remote: Counting objects: 37273, done. remote: Compressing objects: 100% (54/54), done. remote: Total 37273 (delta 29), reused 1 (delta 1), pack-reused 37217 Receiving objects: 100% (37273/37273), 10.16 MiB | 1.35 MiB/s, done. Resolving deltas: 100% (26562/26562), done. Checking connectivity... done.
这会将代码从Let's Encrypt存储库获取到本地文件夹中
设置SSL证书
我们需要使用Let's Encrypt客户端为Apache生成SSL证书,这非常简单。客户端将自动获取并安装新的SSL证书,该证书对我们在参数中提供的域名有效。
以下是从服务器生成和获取SSL证书的过程。
$ cd /opt/letsencrypt $ sudo ./letsencrypt-auto --apache -d domain.com Bootstrapping dependencies for Debian-based OSes... Hit:1 http://in.archive.ubuntu.com/ubuntu xenial InRelease Hit:2 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease Hit:3 http://security.ubuntu.com/ubuntu xenial-security InRelease Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease Reading package lists... Done Reading package lists... Done Building dependency tree Reading state information... Done ca-certificates is already the newest version (20160104ubuntu1). gcc is already the newest version (4:5.3.1-1ubuntu1). The following additional packages will be installed: libexpat1-dev libpython-dev libpython-stdlib libpython2.7-dev python-minimal python-pip-whl python-pkg-resources python2.7 python2.7-dev python2.7-minimal python3-virtualenv zlib1g-dev Suggested packages: augeas-doc augeas-tools python-doc python-tk python-setuptools python2.7-doc binfmt-support Recommended packages: libssl-doc The following NEW packages will be installed: augeas-lenses dialog libaugeas0 libexpat1-dev libffi-dev libpython-dev libpython-stdlib libpython2.7-dev libssl-dev python python-dev python-minimal python-pip-whl python-pkg-resources python-virtualenv python2.7 python2.7-dev python2.7-minimal python3-virtualenv virtualenv zlib1g-dev 0 upgraded, 21 newly installed, 0 to remove and 26 not upgraded. Need to get 33.5 MB of archives. . . . .
如果我们想要获取和安装单个有效域名或子域名的证书,可以使用这些选项。
如果我们需要为多个域名生成SSL证书,可以使用以下命令为多个域名获取证书。
$sudo ./letsencrypt-auto --apache -d domain.com -d domain1.com
这将安装依赖项,然后将显示一个基于GUI的分步指南,以自定义证书选项。

如果我们丢失了密钥,我们可以检索它,系统会要求您提供邮件地址,以便接收任何通知

您可以选择启用http和https访问,也可以将其配置为将所有http请求重定向到https。

安装完成后,我们可以在/etc/letsencrypt/live中找到证书文件。

我们可以使用以下链接验证SSL证书
https://www.ssllabs.com/ssltest/analyze.html?d=example.com&latest
配置自动更新
通常,Let's Encrypt证书仅有效90天,因此建议每60天更新一次证书,这样可以在没有错误的情况下进行更新。我们可以使用renew命令来实现。以下是获取证书更新的命令:
$ ./letsencrypt-auto renew
它将检查依赖项并检查更新并获取证书。
如果我们在机器上为多个域名配置了证书,它将自动更新机器中的所有域名。
我们可以配置cron作业来执行此操作,它将定期自动更新证书,因为服务器在证书过期前30天内不允许更新。
以下是一个cron作业示例,它将在每天凌晨5:00检查更新
00 5 * * 1 /opt/letsencrypt/letsencrypt-auto renew >> /var/log/le-renew.log
在本文中,我们学习了如何配置和安装来自“Let's Encrypt”的免费SSL证书,以保护使用Apache Web服务器托管的网站,由于“Let's Encrypt”处于测试阶段。我建议您查看Let's Encrypt博客以获取重要更新。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP