ウェブサイト検索

Apache を SSL で保護する方法と FreeBSD で暗号化しましょう


このチュートリアルでは、FreeBSD 11 のLet's Encryptによって提供されるTLS/SSL証明書を使用してApache HTTPサーバーを保護する方法を学びます。 ×です。 Lets’ Encrypt の証明書更新プロセスを自動化する方法についても説明します。

こちらもお読みください: FreeBSD に Let’s Encrypt for Nginx をインストールする

TLS/SSL 証明書は、セキュリティを確保するためにエンドノード間の通信、またはより一般的なサーバーとクライアント間の通信を暗号化するために Apache Web サーバーによって使用されます。 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 という名前の 2 つの異なるディレクトリを作成します。以下のコマンドを実行して、Apache ルート構成ディレクトリで >sites-enabled を実行します。

これら 2 つのディレクトリの目的は、新しい仮想ホストを追加するたびにメインの 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 モジュールを有効にします。 ディレクトリに次の内容が含まれます。

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 フラグを使用して、Web サイトのファイルがファイル システム (ドメイン構成ファイルの DocumentRoot ディレクティブ) に保存される正しい Webroot の場所を指定していることを確認してください。複数のサブドメインがある場合は、-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 証明書を Web サイトに追加するには、ドメインの 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 構成ファイルのシンボリックリンクを作成して 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 によって提供される有効な証明書でトラフィックが暗号化されているかどうかを確認することもできます。

それだけです!サーバーとクライアントのブラウザ間を流れるトラフィックが暗号化されるため、クライアントは安全に Web サイトにアクセスできるようになります。 certbot ユーティリティに関するより複雑なタスクについては、次のリンクにアクセスしてください: https://certbot.eff.org/