如何在Ubuntu服务器上部署Let’s Encrypt SSL证书

Apache是Apache基金会在1995年开发的web服务器,Apache是应用最广泛、最稳定可靠的web服务器之一。Apache web服务器需要有效的SSL证书,以便通过加密通信来启用服务器和客户端之间的web安全性。

Let’s Encrypt是一个提供免费SSL证书的证书颁发机构,它在所有主要web浏览器中都是现成的。Certbot是由Electronic Frontier Foundation(EFF)开发的加密客户端。让我们加密SSL证书将为Apache提供更好的安全性,并且使用Certbot工具很容易集成。

本篇主要讲述如何在Ubuntu服务器上部署Let’s Encrypt SSL证书(本例环境Ubuntu 18.04),以下是详细教程:

一、安装所需的软件包

Apache Web服务器和Certbot的安装非常简单。默认的Ubuntu存储库包含Apache和Certbot的所有包。高级打包工具(APT)是Ubuntu的默认包管理器,它可以非常有效地安装这些包。

1、首先删除并刷新旧的apt存储库缓存。

sudo apt clean all

2、然后更新存储库以加载最新的包信息。

sudo apt update

3、您需要执行到的完全升级以更新已安装的软件包。

sudo apt dist-upgrade
4、一旦实现了更新,就可以使用默认的apt包安装程序为Apache安装Apache web服务器和Certbot(让我们加密客户端)。

sudo apt install apache2 python-certbot-apache

成功安装所需软件包后。让我们配置下一步中介绍的防火墙。

二、配置UFW防火墙

UFW(统一防火墙)是Ubuntu使用的默认防火墙。它是一个基于端口的防火墙,易于配置和管理。Apache web服务器需要HTTP和HTTPS服务来允许常规和加密的通信。

1、为了允许这些服务,您需要打开HTTP(80)和HTTPS(443)端口,这是Apache web服务器所必需的。因此,您可以通过在防火墙中实现基于端口的规则来允许这些端口。

sudo ufw allow http
sudo ufw allow https

2、SSH(22)端口是登录到服务器的一个重要端口,但从安全性方面来说,最好在有限的权限下保留该端口。这将使基于端口的安全性能够保护此端口免受暴力攻击。

sudo ufw limit ssh

3、将所有规则添加到防火墙后,您需要启动防火墙并在启动时启用它,以便在启动期间保留防火墙服务。

sudo ufw enable

4、所有操作完成后,就要检查是否添加了端口。

sudo ufw status

5、如果所有配置都很完美,那么它看起来就像给定的示例输出。

Status: active

To Action From
— —— —-
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
22/tcp LIMIT Anywhere
80/tcp (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
22/tcp (v6) LIMIT Anywhere (v6)

完成这一步后,现在让我们进入下一步。

三、配置Apache VirtualHost

Apache VirtualHost允许多个域名在单个Apache web服务器上提供内容。它允许安全机制为多个域名保留不同的目录。这是一种成本效益高的解决方案,可以在低流量下开箱即用。这个过程将把多个网站像共享环境一样保存在一台机器上。

1、首先要添加域名的虚拟主机。给定的示例Apache VirtualHost配置文件将帮助您免费获取加密SSL证书。在Ubuntu中,Apache VirtualHost位于Apache配置目录中的sites available和sites enabled目录下。

sudo nano /etc/apache2/sites-available/example.com.conf

2、将给定的行添加到配置文件中,并确保您的域名必须添加到服务器名和服务器别名中,以便获取加密SSL证书。

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/html/
</VirtualHost>

<Directory /var/www/html>
Require all granted
AllowOverride None
</Directory>

完成后,按Ctrl+O保存,按Ctrl+X退出nano编辑器。

3、在添加Apache VirtualHost配置文件之后,您必须在可用站点和启用站点的目录之间创建符号链接。这允许您在任何时候更改站点可用目录中的VirtualHost配置文件,它将通过使用这些软链接自动复制到站点启用目录。

sudo ln -s /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-enabled/example.com.conf

4、检查配置文件是否正确,此命令还将检查Apache配置文件的语法。

sudo apachectl -t

5、如果一切正常,那么它将语法OK显示为输出。如果出现任何问题,请重新验证Apache VirtualHost文件。全部完成后,重新启动服务以更改效果。

sudo systemctl restart apache2

在添加Apache VirtualHost文件之后,是时候通过生成免费的Let’s Encrypt加密证书SSL来保护Apache web服务器了。

四、生成Let’s Encrypt SSL证书

1、Certbot是Let’s Encrypt客户端,用于自动生成Let’s Encrypt SSL并与Apache web服务器集成。这一步将指导您如何使用Certbot,使用子标志apache实现与apache的自动SSL集成,并为所有需要SSL证书的域名使用flag-d。

sudo certbot –apache -d example.com -d www.example.com

2、在交互式设置Let’s Encrypt脚本期间,需要提供一些信息。您将提供电子邮件地址以获取电子邮件通知,其中包含与Let’s Encrypt SSL证书相关的各种信息。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter ‘c’ to
cancel): admin@example.com

3、接受服务条款并选择您的电子邮件地址是否与Electronic Frontier Foundation共享。

——————————————————————————-
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
——————————————————————————-
(A)gree/(C)ancel: A

——————————————————————————-
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let’s Encrypt project and the non-profit
organization that develops Certbot? We’d like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
——————————————————————————-
(Y)es/(N)o: N

4、全部完成后,Let’s Encrypt将执行两项免费获取的尝试:Let’s Encrypt SSL证书。如果所有配置都正确,那么在完成所有步骤后,您将获得Let’s Encrypt SSL证书。

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
http-01 challenge for www.example.com
Enabled Apache rewrite module
Waiting for verification…
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/000-default-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/000-default-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/000-default-le-ssl.conf

5、如果Certbot找不到您的服务器virtualhost,它将询问您要添加哪个配置文件。

We were unable to find a vhost with a ServerName or Address of www.example.com.
Which virtual host would you like to choose?
(note: conf files with multiple vhosts are not yet supported)
——————————————————————————-
1: 000-default.conf | | | Enabled
2: 000-default-le-ssl.conf | example.com | HTTPS | Enabled
——————————————————————————-
Select the appropriate number [1-2] then [enter] (press ‘c’ to cancel): 2
Deploying Certificate to VirtualHost /etc/apache2/sites-available/000-default-le-ssl.conf

6、此步骤适用于从不安全的HTTP流量重定向到安全的HTTPS流量。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
——————————————————————————-
1: No redirect – Make no further changes to the webserver configuration.
2: Redirect – Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you’re confident your site works on HTTPS. You can undo this
change by editing your web server’s configuration.
——————————————————————————-
Select the appropriate number [1-2] then [enter] (press ‘c’ to cancel): 2
Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/000-default.conf to ssl vhost in /etc/apache2/sites-available/000-de

7、当所有步骤都成功实现并最终生成加密SSL证书时,它将显示给定的信息。

——————————————————————————-
Congratulations! You have successfully enabled https://example.com and
https://www.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=example.com
https://www.ssllabs.com/ssltest/analyze.html?d=www.example.com
——————————————————————————-

IMPORTANT NOTES:
– Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2019-08-02. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the “certonly” option. To non-interactively renew *all* of
your certificates, run “certbot 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

8、成功生成Let’s Encrypt证书后,会自动添加该证书以保护Apache。在下一步中,您将设置crontab以自动更新Let’s Encrypt SSL证书。

五、自动更新Let’s Encrypt证书

Let’s Encrypt SSL证书的有效期为自颁发之日起90天。因此,在证书到期前更新证书是至关重要的。crontab允许在计划时间自动计划续订任务。
要将条目添加到crontab,您需要计划时间,并且certbot续订将在配置的计划时间内定期运行。

sudo crontab -e

在第一次运行期间,它会询问默认编辑器您必须选择nano作为文本编辑器。

Select an editor. To change later, run ‘select-editor’.
1. /bin/nano <—- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
4. /bin/ed

Choose 1-4 [1]: 1

将给定的行添加到crontab中,以便在05:00 AM启用自动Let’s Encrypt 续订。这将在到期日前自动续订SSL证书。

0 5 * * * /usr/bin/certbot renew –post-hook “systemctl restart apache2”

完成后,按Ctrl+O保存,按Ctrl+X退出nano编辑器。
检查这些条目是否添加到crontab。

sudo crontab -l

此命令显示添加的cronjobs的输出。这意味着您已经成功地添加了crontab条目,用于自动续订Let’s Encrypt SSL证书。

最后,我们使用Let’s Encrypt SSL证书来保护Apache。它还将启用使用crontab自动更新Let’s Encrypt SSL证书。这将通过实现Let’s Encrypt SSL证书来保护您的web属性。此时,您可以为您的网站使用安全协议HTTPS。

 

发表评论

您的电子邮箱地址不会被公开。