安卓系统

让我们在Debian 9上加密来保护Apache

Microsoft Azure OpenDev—June 2017

Microsoft Azure OpenDev—June 2017

目录:

Anonim

让我们加密是由Internet安全研究组(ISRG)创建的证书颁发机构。 它通过完全自动化的过程提供免费的SSL证书,该过程旨在消除手动创建,验证,安装和更新证书的过程。

从加密之日起,Let's Encrypt颁发的证书有效期为90天,并受到当今所有主要浏览器的信任。

本教程将指导您完成使用Debian 9上的certbot工具获得免费的Let's Encrypt的过程。我们还将展示如何配置Apache以使用新的SSL证书并启用HTTP / 2。

先决条件

在继续本教程之前,请确保满足以下先决条件:

  • 以具有sudo特权的用户身份登录。具有一个指向服务器公共服务器IP的域名。 我们将使用 example.com .Apache安装。 您域的apache虚拟主机。 您可以按照以下说明详细了解如何创建一个。

安装Certbot

Certbot是功能齐全且易于使用的工具,可以自动执行用于获取和续订Let's Encrypt SSL证书的任务。 certbot软件包包含在默认的Debian存储库中。

使用以下命令更新软件包列表并安装certbot软件包:

sudo apt update sudo apt install certbot

产生强Dh(Diffie-Hellman)组

Diffie-Hellman密钥交换(DH)是一种在不安全的通信通道上安全地交换加密密钥的方法。

要生成一组新的2048位DH参数,请运行:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048 如果愿意,您可以将大小最多更改为4096位,但是在这种情况下,生成时间可能会超过30分钟,具体取决于系统的熵。

获取“让我们加密SSL”证书

为了获得我们域的SSL证书,我们将使用Webroot插件,该插件的工作方式是在 ${webroot-path}/.well-known/acme-challenge 目录中创建一个用于验证请求的域的临时文件。 Let's Encrypt服务器向临时文件发出HTTP请求,以验证请求的域是否解析为certbot运行的服务器。

为了简化 /var/lib/letsencrypt ,我们将所有针对 .well-known/acme-challenge HTTP请求映射到一个目录 /var/lib/letsencrypt

以下命令创建目录并使它对于Apache服务器可写。

sudo mkdir -p /var/lib/letsencrypt/.well-known sudo chgrp www-data /var/lib/letsencrypt sudo chmod g+s /var/lib/letsencrypt

为避免重复代码,请创建以下两个配置片段:

/etc/apache2/conf-available/letsencrypt.conf

Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/" AllowOverride None Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/" AllowOverride None Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS /etc/apache2/conf-available/ssl-params.conf

SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 SSLHonorCipherOrder On Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" Header always set X-Frame-Options DENY Header always set X-Content-Type-Options nosniff # Requires Apache >= 2.4 SSLCompression off SSLUseStapling on SSLStaplingCache "shmcb:logs/stapling-cache(150000)" # Requires Apache >= 2.4.11 SSLSessionTickets Off SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"

上面的代码片段使用了Cipherli.st推荐的削片程序,启用了OCSP装订,HTTP严格传输安全性(HSTS)并仅实施了一些针对安全性的HTTP标头。

在启用配置文件之前,请通过发出以下 mod_headers 确保同时启用了 mod_ssl mod_headers

sudo a2enmod ssl sudo a2enmod headers

启用HTTP / 2模块,这将使您的网站更快,更健壮:

sudo a2enmod

通过运行以下命令来启用SSL配置文件:

sudo a2enconf letsencrypt sudo a2enconf ssl-params

重新加载Apache配置以使更改生效:

sudo systemctl reload apache2

使用带有Webroot插件的Certbot工具来获取SSL证书文件:

sudo certbot certonly --agree-tos --email [email protected] --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com

如果成功获得SSL证书,certbot将显示以下消息:

IMPORTANT NOTES: IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will expire on 2019-01-17. 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 lose your account credentials, you can recover through e-mails sent to [email protected]. - 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:

有了证书文件后,请按如下所示编辑域虚拟主机配置:

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

ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration

使用上述配置,我们将强制HTTPS并从www重定向到非www版本。 随意根据您的需要调整配置。

重新加载Apache服务以使更改生效:

sudo systemctl reload apache2

使用 https:// 打开您的网站,您会注意到一个绿色的锁图标。

自动更新让我们加密SSL证书

让我们加密的证书有效期为90天。 要在证书过期之前自动续订证书,certbot程序包会创建一个cronjob,该程序每天运行两次,并将在证书过期前30天自动续订任何证书。

证书更新后,我们还必须重新加载Apache服务。 将 --renew-hook "systemctl reload apache2" 附加到 /etc/cron.d/certbot 文件中,所以看起来像下面这样:

/etc/cron.d/certbot

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload apache2"

要测试续订过程,请使用certbot --dry-run 开关:

sudo certbot renew --dry-run

如果没有错误,则表示更新过程成功。

结论

在本教程中,您使用了“让我们加密”客户端certbot来获取您域的SSL证书。 您还创建了Apache代码段以避免重复代码,并配置了Apache以使用证书。 在本教程的最后,您已经设置了cronjob来自动更新证书。

apache debian让我们加密certbot ssl

这篇文章是如何在Debian 9系列上安装LAMP Stack的一部分。

本系列的其他文章:

•如何在Debian 9上安装Apache•如何在Debian 9上安装PHP•如何在Debian 9上设置Apache虚拟主机•如何在Debian 9上安装MariaDB•在Debian 9上通过加密让Apache安全