如何在 Ubuntu 20.04 LTS 上使用 Let’s Encrypt SSL 安装 Apache

Linux命令3年前 (2021)发布 AI观察员
162 0 0

Let’s Encrypt 是一个为网站提供免费 SSL 证书的证书颁发机构。Let’s Encrypt 提供的所有 SSL 证书均可用于生产/商业用途,无需任何成本或费用。本指南将告诉您有关安装 Apache Web 服务器、安装 Certbot、使用 Certbot 生成 SSL 证书以及创建其他 SSL 配置以从 SSL 测试 SSL 实验室获得 A+ 的信息。如何在 Ubuntu 20.04 LTS 上使用 Let's Encrypt SSL 安装 Apache

Ubuntu 20.04 LTS Focal Fossa 上使用 Let’s Encrypt SSL 安装 Apache

步骤 1. 首先,通过apt在终端中运行以下命令确保所有系统包都是最新的。

sudo apt update
sudo apt upgrade

步骤 2. 在 Ubuntu 20.04 上安装 Apache。

默认情况下,Apache 在 Ubuntu 20.04 基础存储库中可用。现在我们通过运行以下命令在 Ubuntu 系统上安装 Apache Web 服务器:

sudo apt install apache2 apache2-utils

在开始配置之前,请确保 Apache 服务正在您的系统上运行。运行以下命令检查 apache 服务状态:

sudo systemctl status apache2
sudo systemctl start apache2
sudo systemctl enable apache2

步骤 3. 配置防火墙。

通过运行以下给定的命令,允许访问防火墙上的端口 80:

sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

步骤 4. 配置 Apache 虚拟主机。

首先,创建一个根目录来保存您网站的文件:

sudo mkdir -p /var/www/html/your-domain.com/

然后,更改目录的所有权和组:

sudo chown -R www-data:www-data /var/www/html/your-domain.com/

让我们创建一个 Apache 虚拟主机来为您网站的 HTTP 版本提供服务:

sudo nano /etc/apache2/sites-available/your-domain.com.conf

添加以下行:

<VirtualHost *:80>

   ServerName your-domain.com
   ServerAlias www.your-domain.com
   ServerAdmin admin@your-domain.com
   DocumentRoot /var/www/html/your-domain.com

   ErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log
   CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combined

   <Directory /var/www/html/your-domain.com>
      Options FollowSymlinks
      AllowOverride All
      Require all granted
   </Directory>

</VirtualHost>

保存并关闭,然后重新启动 Apache 网络服务器以进行更改:

sudo a2ensite your-domain.com.conf
sudo a2enmod ssl rewrite
sudo systemctl restart apache2

步骤 5. 使用 Let’s Encrypt SSL Free Certificate 保护 Apache。

在安装 Certbot 之前,我们为您的系统启用 Universe 存储库:

sudo apt install software-properties-common
sudo add-apt-repository universe
sudo apt update

接下来,在您的 Ubuntu 系统上安装 Certbot for Apache:

sudo apt install certbot python3-certbot-apache

最后,使用 Certbot 命令创建一个 Let’s Encrypt 证书并配置 Apache 以使用该证书:

sudo certbot --apache

输出:

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://your-domain.com and
https://www.your-domain.com

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

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/idroot.us/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/your-domain.com/privkey.pem
Your cert will expire on 2021-10-05. 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"
- 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

步骤 5. 自动续订 Certbot SSL。

Let’s Encrypt 证书的有效期为 90 天,您必须确保它们按时更新。由于systemdCertbot 客户端提供的服务,续订过程现在是自动化的。要测试此续订过程是否正常工作,您可以运行:

sudo certbot renew --dry-run

输出:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/your-domain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for your-domain.com and www.your-domain.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded: 
  /etc/letsencrypt/live/your-domain.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

步骤 7. 测试 SSL。

您现在可以转到 ssllabs.com/ssltest/并在您的域上运行 SSL 测试:

如何在 Ubuntu 20.04 LTS 上使用 Let's Encrypt SSL 安装 Apache

感谢您使用本教程在 Ubuntu 20.04 LTS Focal Fossa 系统上安装 Apache 网络服务器 Let’s Encrypt SSL。如需其他帮助或有用信息,我们建议您查看Apache 官方网站

© 版权声明

相关文章

天猫U特购  京东优惠购        京东优惠    天猫优惠