目次:
Let's Encryptは、Internet Security Research Group(ISRG)によって作成された認証局です。 手動による証明書の作成、検証、インストール、および更新を排除するように設計された完全に自動化されたプロセスにより、無料のSSL証明書を提供します。
Let's Encryptによって発行された証明書は、今日すべての主要なブラウザーによって信頼されています。
このチュートリアルでは、Ubuntu 18.04でcertbotツールを使用してLet's EncryptでApacheを保護する方法について、順を追って説明します。
前提条件
このチュートリアルを続行する前に、次の前提条件を満たしていることを確認してください。
- パブリックサーバーのIPを指すドメイン名。
example.com
を使用しexample.com
。ドメインのApache仮想ホストを使用してApacheがインストールされています。
Certbotをインストールする
Certbotは、Let's Encrypt SSL証明書を取得および更新し、Webサーバーを構成するためのタスクを自動化できる、フル機能の使いやすいツールです。 certbotパッケージは、デフォルトのUbuntuリポジトリに含まれています。
パッケージリストを更新し、certbotパッケージをインストールします。
sudo apt update
sudo apt install certbot
Strong Dh(Diffie-Hellman)グループを生成
Diffie–Hellmanキー交換(DH)は、セキュリティで保護されていない通信チャネルを介して暗号キーを安全に交換する方法です。 セキュリティを強化するために、2048ビットDHパラメーターの新しいセットを生成します。
sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
必要に応じて、サイズを最大4096ビットまで変更できますが、その場合、システムエントロピーによっては生成に30分以上かかる場合があります。
Let's Encrypt SSL証明書の取得
ドメインのSSL証明書を取得するには、
${webroot-path}/.well-known/acme-challenge
ディレクトリに要求されたドメインを検証するための一時ファイルを作成することで機能するWebrootプラグインを使用します。 Let's Encryptサーバーは、一時ファイルに対してHTTP要求を行い、要求されたドメインがcertbotが実行されているサーバーに解決されることを検証します。
より簡単にするために、
.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
コードの重複を回避するには、次の2つの構成スニペットを作成します。
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
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 Stapling、HTTP Strict Transport Security(HSTS)を有効にし、セキュリティに重点を置いたHTTPヘッダーをほとんど強制しません。
構成ファイルを有効にする前に、次を発行して
mod_ssl
と
mod_headers
両方が有効になっていることを確認してください。
sudo a2enmod ssl
sudo a2enmod headers
次に、次のコマンドを実行してSSL構成ファイルを有効にします。
sudo a2enconf letsencrypt
sudo a2enconf ssl-params
HTTP / 2モジュールを有効にすると、サイトがより高速で堅牢になります。
sudo a2enmod
変更を有効にするために、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: - 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 2018-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:
証明書ファイルを入手したので、次のようにドメイン仮想ホスト構成を編集します。
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://
を使用してWebサイトを開くことができ、緑色のロックアイコンが表示されます。
SSL証明書を自動更新しましょう
Let's Encryptの証明書は90日間有効です。 有効期限が切れる前に証明書を自動的に更新するために、certbotパッケージは1日に2回実行される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
エラーがない場合、更新プロセスが成功したことを意味します。
結論
このチュートリアルでは、Let's Encryptクライアントcertbotを使用して、ドメインのSSL証明書をダウンロードしました。 また、コードの重複を避けるためにApacheスニペットを作成し、証明書を使用するようにApacheを構成しました。 チュートリアルの最後で、自動証明書更新用のcronjobをセットアップしました。
Apache Ubuntu certbot sslを暗号化しましょうこの投稿は、how-to-install-lamp-stack-on-ubuntu-18-04シリーズの一部です。
このシリーズの他の投稿:
•Ubuntu 18.04にApacheをインストールする方法•Ubuntu 18.04にApache仮想ホストを設定する方法•Ubuntu 18.04にLet's Encryptを使用してApacheを保護する•Ubuntu 18.04にMySQLをインストールする方法•Ubuntu 18.04にPHPをインストールする方法