网站搜索

如何在 FreeBSD 中使用 SSL 和 Let's Encrypt 保护 Apache


在本教程中,我们将学习如何使用 FreeBSD 11 中的 Let's Encrypt 提供的 TLS/SSL 证书来保护 Apache HTTP 服务器。 x。我们还将介绍如何自动化 Lets’ Encrypt 的证书续订过程。

另请阅读:在 FreeBSD 上安装 Let’s Encrypt for Nginx

Apache Web 服务器使用 TLS/SSL 证书来加密终端节点之间的通信,或者更常见的是服务器和客户端之间的通信,以提供安全性。 Let’s Encrypt 提供 certbot 命令行实用程序,该应用程序可以帮助您免费获取受信任的证书。

要求:

  1. 安装 FreeBSD 11.x
  2. 安装 FreeBSD 后要做的 10 件事
  3. 如何在 FreeBSD 中安装 Apache、MariaDB 和 PHP

第 1 步:在 FreeBSD 上配置 Apache SSL

1. 在开始安装 certbot 实用程序并为 Apache 创建 TSL 配置文件之前,首先创建两个不同的目录,分别名为 sites-available通过发出以下命令在 Apache 根配置目录中 >sites-enabled

这两个目录的目的是为了方便系统中的虚拟主机配置管理,而无需每次添加新的虚拟主机时都修改Apache的httpd.conf主配置文件。

mkdir /usr/local/etc/apache24/sites-available
mkdir /usr/local/etc/apache24/sites-enabled

2. 创建两个目录后,使用文本编辑器打开 Apache httpd.conf 文件,并将以下行添加到文件末尾附近,如下所示。

nano /usr/local/etc/apache24/httpd.conf

添加以下行:

IncludeOptional etc/apache24/sites-enabled/*.conf

3. 接下来,通过在 modules.d< 中创建名为 020_mod_ssl.conf 的新文件来启用 Apache 的 TLS 模块/strong> 目录包含以下内容。

nano /usr/local/etc/apache24/modules.d/020_mod_ssl.conf

将以下行添加到文件 020_mod_ssl.conf

Listen 443
SSLProtocol ALL -SSLv2 -SSLv3
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLPassPhraseDialog  builtin
SSLSessionCacheTimeout  300

4. 现在,通过从开头删除主题标签,从 /usr/local/etc/apache24/httpd.conf 文件中取消注释 SSL 模块如下图所示:

LoadModule ssl_module libexec/apache24/mod_ssl.so

5. 接下来,在 sites-available 目录中为您的域创建 TLS 配置文件,最好使用您的域名称,如以下摘录:

nano /usr/local/etc/apache24/sites-available/bsd.lan-ssl.conf

将以下虚拟主机配置添加到文件 bsd.lan-ssl.conf 中。

<VirtualHost *:443>
    ServerName www.yourdomain.com
	ServerAlias yourdomain.com
                DocumentRoot "/usr/local/www/apache24/data/"
	SSLEngine on

	SSLCertificateFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/cert.pem"
	SSLCertificateKeyFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/privkey.pem"
	SSLCertificateChainFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem"

<FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
</FilesMatch>

<Directory "/usr/local/www/apache24/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>

	BrowserMatch "MSIE [2-5]" \
        nokeepalive ssl-unclean-shutdown \
        downgrade-1.0 force-response-1.0

	CustomLog "/var/log/apache/httpd-ssl_request.log" \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

	<Directory "/usr/local/www/apache24/data/">
            Options Indexes FollowSymLinks MultiViews
        #AllowOverride controls what directives may be placed in .htaccess files.       
                        AllowOverride All
        #Controls who can get stuff from this server file
                        Require all granted
        </Directory>
       
    ErrorLog "/var/log/apache/yourdomain.ssl-error.log"
    CustomLog "/var/log/apache/yourdomain.ssl-access_log" combined

</VirtualHost>

确保相应地替换 ServerNameServerAliasErrorLogCustomLog 语句中的域名变量。

第 2 步:在 FreeBSD 上安装 Lets’Encrypt

6. 在下一步中,发出以下命令以安装 Let's Encrypt 提供的 certbot 实用程序,该实用程序将用于获取 <为您的域提供strong>Apache TSL免费证书。

安装certbot时,屏幕上会显示一系列提示。使用下面的屏幕截图来配置 certbot 实用程序。此外,编译和安装 certbot 实用程序可能需要一些时间,具体取决于您的计算机资源。

cd /usr/ports/security/py-certbot
make install clean

7. 编译过程完成后,发出以下命令以更新 certbot 实用程序和 certbot 所需的依赖项。

pkg install py27-certbot
pkg install py27-acme

8. 要为您的域生成证书,请发出如下所示的命令。确保使用 -w 标志提供文件系统中存储网站文件的正确 Webroot 位置(域配置文件中的 DocumentRoot 指令)。如果您有多个子域,请使用 -d 标志添加它们。

certbot certonly --webroot -w /usr/local/www/apache24/data/ -d yourdomain.com -d www.yourdomain.com

获取证书时,请提供用于证书续订的电子邮件地址,按 a 同意 Let’s Encrypt 条款和条件,按 n 不共享 Let’s Encrypt 合作伙伴的电子邮件地址。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):[email 
There seem to be problems with that address. Enter email address (used for
urgent renewal and security notices)  If you really want to skip this, you can
run the client with --register-unsafely-without-email but make sure you then
backup your account key from /etc/letsencrypt/accounts   (Enter 'c' to cancel):[email 

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.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 Obtaining a new certificate Performing the following challenges: http-01 challenge for www.domain.com Using the webroot path /usr/local/www/apache24/data for all unmatched domains. Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /usr/local/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem. Your cert will expire on 2017-11-15. 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" - Your account credentials have been saved in your Certbot configuration directory at /usr/local/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

9. 获得域的证书后,您可以运行 ls 命令来列出所有证书的组件(链、私钥、证书),如下例所示。

ls -al /usr/local/etc/letsencrypt/live/www.yourdomain.com/

步骤 3:在 FreeBSD 上更新 Apache TLS 证书

10. 为了将 Let’s Encrypt 证书添加到您的网站,请打开您的域的 apache 配置文件并更新以下行以反映颁发的证书的路径。

nano /usr/local/etc/apache24/sites-available/bsd.lan-ssl.conf

添加以下 TLS 证书行:

SSLCertificateFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/cert.pem"
	SSLCertificateKeyFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/privkey.pem"
	SSLCertificateChainFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem"

11. 最后,通过为您的域 TLS 配置文件创建到 sites-enabled 目录的符号链接来启用 TLS 配置文件,检查 Apache 配置是否存在可能的语法错误,如果语法正确,通过发出以下命令重新启动 Apache 守护程序。

ln -sf /usr/local/etc/apache24/sites-available/bsd.lan-ssl.conf /usr/local/etc/apache24/sites-enabled/
apachectl -t
service apache24 restart

12. 要检查 Apache 服务是否正在侦听 HTTPS 端口 443,请发出以下命令来列出 httpd 网络套接字。

sockstat -4 | grep httpd

13.您可以通过 HTTPS 协议从浏览器导航到您的域名地址,以确认 Let’s Encrypt 证书已成功应用。

https://www.yourdomain.com

14. 要从命令行获取有关颁发的 Let's Encrypt 证书的额外信息,请使用 openssl 命令,如下所示。

openssl s_client -connect www.yourdomain.com:443

15. 您还可以通过移动设备验证流量是否使用 Let’s Encrypt CA 提供的有效证书进行加密,如下面的移动屏幕截图所示。

就这样!客户端现在可以安全地访问您的网站,因为服务器和客户端浏览器之间流动的流量是加密的。有关 certbot 实用程序的更复杂的任务,请访问以下链接:https://certbot.eff.org/