無料のSSL証明書を使ってサイトをHTTPS化する方法

無料のSSL証明書を使ってサイトをHTTPS化する方法をご紹介します。

SSL証明書はLet’s Encryptを使用し、WebサーバーはApache Httpdを使用しています。

証明書のダウンロード

まずは Let’s Encrypt から証明書をダウンロードします。

証明書のダウンロードの際には、公開ディレクトリとドメインの指定が必要です。ダウンロードするコマンドは次のとおりです。

# certbot certonly --webroot -w {公開ディレクトリ} -d {ドメイン名}

実行すると次のような出力がされます。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Obtaining a new certificate

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/early2home.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/early2home.com/privkey.pem
Your cert will expire on 2020-06-26. 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: https://eff.org/donate-le

無事に完了している場合、/etc/letsencrypt/live/に証明書がダウンロードされます。確認してみます。

# ls -l /etc/letsencrypt/live/
total 2
-rw-r--r-- 1 root root 740 Mar 28 11:16 README
drwxr-xr-x 2 root root 88 Mar 28 13:32 early2home.com

無事に出来ていることが確認できました。

証明書の設定

次のファイルに設定を記述します。

/etc/httpd/conf.d/ssl.conf
<VirtualHost *:443>
  ServerName early2home.com
  DocumentRoot "公開したいディレクトリ"
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/early2home.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/early2home.com/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/early2home.com/chain.pem
</VirtualHost>

を付与します。

デフォルトで設定してある次の要素

<VirtualHost _default:443>

<VirtualHost *:443>

に変更します。

先ほど変更した次の要素

<VirtualHost *:443>

の前に次の記述を追加します。

NameVirtualHost *:443
<VirtualHost *:443>

これでSSL証明書のダウンロードと設定が完了です。Webサーバーを再起動して動作確認します。

確認

Webサーバーを再起動する次のコマンドを実行します。

# service httpd restart

https://ドメイン にアクセスして無事にサイトが公開されれば成功です。これでサイトのHTTPS化(SSL対応)が完了しました。