Nginx和Apache使用Let’s Encrypt通配符SSL证书

时间:2020-02-23 14:40:40  来源:igfitidea点击:

什么是SSL证书?
SSL代表安全套接字层。
它是一个标准的全局技术,可确保Web服务器和Web客户端之间的数据加密,最大限度地减少被黑客攻击的和Web应用程序的风险。
安装在Web服务器上的SSL证书可确保此安全连接。
SSL证书包含公钥,标识和任何其他相关信息,并在原始服务器中托管。
尝试与原始服务器通信的任何客户都需要引用该文件以获取公钥和身份。

Let’s Encrypt 是一个证书颁发机构,提供了一种自动获取和安装免费SSL/TLS证书的方法,在Web服务器上启用加密的HTTP流量。
它提供了一个名为CERTBOT的软件客户端,通过具有自动安装的大多数步骤,简单地使SSL安装简单。
对于Apache和Nginx Web服务器,SSL安装完全自动化。
在本教程中,我们将研究如何使用Let's Encrypt Puncard SSL证书与Ubuntu/CentOS上的Nginx和Apache。

在Ubuntu | Centos上安装CERTBOT.

要在Ubuntu和Centos上安装CertBot,我们将根据我们正在使用的Web服务器运行命令,如下所示。

对于nginx web服务器

要为nginx安装cerbot,请使用以下命令:

--- Ubuntu  --
sudo apt install certbot python3-certbot-nginx
--- CentOS 8 --
sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf config-manager --set-enabled PowerTools
sudo yum -y install certbot python3-certbot-nginx nginx
--- CentOS 7 --
sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum -y install certbot python2-certbot-nginx nginx

对于Apache Web服务器

对于Apache Web服务器,请运行以下命令以安装CERTBOT。

--- Ubuntu  --
sudo apt install certbot python3-certbot-apache2
--- CentOS 8 --
sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf config-manager --set-enabled PowerTools
sudo yum -y install certbot python3-certbot-apache httpd
--- CentOS 7 --
sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum -y install certbot python2-certbot-apache httpd

检查nginx和apache web服务器配置

我们需要确保我们在首选Web服务器后拥有Web服务器虚拟主机。
该文件应包含Web服务器名称和别名,如图所示。

对于Apache,请检查文件,如图所示:

--- Ubuntu --
sudo vim /etc/apache2/sites-available/example.com.conf
--- CentOS --
sudo vim /etc/httpd/conf.d/example.com.conf

我们应该具有服务器名称和别名名称,如图所示:

ServerName example.com
ServerAlias www.example.com

与nginx相同的情况,请检查配置,如图所示:

sudo vim /etc/nginx/conf.d/example.com.conf

我们也应该在这里拥有服务器名称和别名。

server_name  example.com  www.example.com;

如何通过CESTBOT获取允许我们加密通配符SSL

已确认WebServer虚拟主机,是时候对Let的加密通配符SSL进行了时间了。
通配符SSL是一种涵盖主域和其所有子域的SSL类型。
例如,for * .example.com的通配符SSL也应该保护某些东西.example.com,one.example.com等。

使用Let's Encrypt Wildcard SSL来保护Nginx/Apache

运行如下所示的命令,请求ssl for * .example.com。

sudo certbot certonly \
  --agree-tos \
  --email Hyman@theitroad \
  --manual \
  --preferred-challenges=dns \
  -d *.example.com \
  --server https://acme-v02.api.letsencrypt.org/directory

下面是上面命令中使用的各种参数的描述:--certonly:我们的命令中的Certonly选项将确保我们只想发出SSL证书。
如果从该命令中删除Certonly选项,CertBot将发出SSL证书,它还将更新虚拟主机文件以应用SSL Certificate.-Cateper-TOS:用于同意允许允许允许服务条款.-电子邮件:电子邮件:电子邮件:电子邮件:电子邮件提供用于存储SSL中的SSL加密帐户。
当SSL即将到期时,它将用于通知我们.-手册:这带来了发出SSL的交互式方式,其中提示了我们更多信息.-首选挑战:指定SSL验证的方法。
必须在发出SSL之前验证域名。
在这种情况下,我们是SLICTING DNS-D:用于指定要发出SSL证书 - 服务器的域:用于指定发出SSL证书的API端点。

执行命令后,我们将收到一个需要添加到DNS服务器的TXT记录。
记录将如下所示:

Please deploy a DNS TXT record under the name 
_acme-challenge.example.com with the following value: 
HejzlvXokaKoAq_xnr5LTplWbKYNScVH-ASy1vMYMGE
Before continuing, verify the record is deployed. 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Press Enter to Continue

将记录添加到DNS服务器的Web服务器域。

确认DNS服务器可用记录。

验证了已部署记录后,按Enter获取SSL。
我们应该如下取得反馈:

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 2017-10-28. To obtain a new or tweaked 
  version of this certificate in the future, simply run certbot 
  again. To non-interactively renew *all* of your certificates, run 
  "certbot renew" 
- 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

配置Nginx Web服务器使用Lets Encrypt Wildcard SSL

现在配置nginx Web服务器使用允许员加密通配符SSL

我们需要编辑nginx虚拟主机配置文件并使https启用如下:

sudo vim /etc/nginx/conf.d/example.com.conf

内容现在应该如下所示:

server { 
 listen 80; 
 listen [::]:80; 
 server_name *.example.com; 
 return 301 https://$host$request_uri; 
} 
server { 
 listen 443 ssl; 
 server_name *.example.com; 
 ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 
 ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; 
 include /etc/letsencrypt/options-ssl-nginx.conf; 
 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
 root /var/www/example.com; 
 index index.html; 
 location/{ 
   try_files $uri $uri/=404; 
 } 
}

让我们通过创建到启动期间的Nginx读取的启用位站点的链接来启用文件。

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

现在测试nginx配置,以确保所有设置都可以。

sudo nginx -t

如果nginx配置确定,则应获得如下所示的输出。

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 
nginx: configuration file /etc/nginx/nginx.conf test is successful

在重新加载nginx之后。

sudo systemctl restart nginx

配置Apache Web Server使用Lets Encrypt WildCard SSL

对于Apache Web服务器,请重复与Nginx相同的过程。
Apache的配置文件编辑为:

sudo vim /etc/apache2/sites-available/api.example.com.conf

具有如下SSL行。

SSLCertificateFile      /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/fullchain.pem

完成后,重新加载Apache

sudo systemctl restart apache2