CentOSで無料SSL証明書「Let’s Encrypt」を導入・自動更新する方法
以前、XREAに無料SSL証明書を導入してみましたという記事を投稿したのですが、思いのほか需要があるネタらしく、当サイトでは和楽一筋関連の次にアクセス数が多い記事になっています。
Webサイトをhttps化していない場合、セキュリティが不十分であるだけでなく、ブラウザに警告表示が出たり、Googleなどの検索エンジンにランクを下げられるという話もあり、今後は個人サイトであってもhttps化(SSLサーバ証明書の導入)が重要になってきます。
昔は有料のSSL証明書しかなく、個人が利用するには敷居が高いものでしたが、今は無料のサービスがあります。
以前自分が自宅サーバに無料SSL証明書を入れた際、いろいろと苦労した点があったため、今回はLinuxのひとつであるCentOSに無料SSL証明書「Let’s Encrypt」をインストールして、証明書の自動更新設定まで実施する方法を備忘録としてまとめました。
目次
無料SSLサーバ証明書「Let’s Encrypt」とは?
Let’s Encryptは、非営利団体ISRG (Internet Security Research Group)が運営しているプロジェクトです。
SSL/TLSサーバ証明書の無料発行や、証明書の発行・設定・更新の自動化を通して、httpsなどの安全な通信の普及を目指しています。
2016年にサービスが始まったそうです。
英語の公式サイトはこちら。日本語だとLet’s Encrypt総合ポータルという非公式解説サイトに詳しい情報があります。
前提
今回の記事は、以下の前提でまとめています。
- サーバのOSとして、CentOS 6系がインストール済
- CentOS 7系の場合、少しコマンドが変わります
- Webサーバソフトとして、nginxがインストール済
- Apacheでも、ほぼ同じ手順です
- ウェブサイトが公開済で、インターネットからサーバにhttp/httpsアクセスできること
- ドメインの所有者であることを認証する際、http/httpsアクセスが必要になります
- ルータやファイアウォールのポート設定に注意
- 独自ドメインを保有していること
SSLサーバ証明書の導入
まずは、サイトにSSLサーバ証明書を導入していきます。
(1)ツールのダウンロード・インストール
- サーバにログインして、rootユーザになります。
1$ su - - 関連ツールがEPELリポジトリに入っているため、インストールします。
1# yum install epel-release - Let’s Encryptでは、「Certbot」というクライアントソフトウェアを使って証明書の取得・更新を行います。
Certbotをダウンロードして、実行権限を付与。
123# cd ~# wget https://dl.eff.org/certbot-auto# chmod a+x certbot-auto - Certbotを実行。
1# ./certbot-auto
これを実行すると、パッケージ管理システムyumによって関連ツールが自動でインストールされた後、設定画面に移ります。
しばらく待ちましょう。
インストール時に確認を求められたら「y」を入力します。 - 「完了しました!」というメッセージのあと、設定に進みます。
(2)SSLサーバ証明書の設定
- 自分の環境の場合、Apacheとnginxのどちらを使うか質問されたので、「2」を入力。
123456How would you like to authenticate and install certificates?-------------------------------------------------------------------------------1: Apache Web Server plugin - Beta (apache)2: Nginx Web Server plugin - Alpha (nginx)-------------------------------------------------------------------------------Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 - メールアドレスを入力します。
証明書の期限など、重要なお知らせが届くので、必ず連絡がとれるメールアドレスにしましょう。
12Enter email address (used for urgent renewal and security notices) (Enter 'c' tocancel): xxxxxxxxx@xxxxxx.com - 規約に同意するよう求められます。「A」を入力。
1234567-------------------------------------------------------------------------------Please read the Terms of Service athttps://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agreein order to register with the ACME server athttps://acme-v01.api.letsencrypt.org/directory-------------------------------------------------------------------------------(A)gree/(C)ancel: A - メールアドレスを電子フロンティア財団に共有するか質問されます。
財団からメールが届くらしいのですが、不要なので「N」を入力。
1234567-------------------------------------------------------------------------------Would you be willing to share your email address with the Electronic FrontierFoundation, a founding partner of the Let's Encrypt project and the non-profitorganization that develops Certbot? We'd like to send you email about EFF andour work to encrypt the web, protect its users and defend digital rights.-------------------------------------------------------------------------------(Y)es/(N)o: N - Webサーバに複数のサイトが同居している場合、どのサイトをHTTPS化するか質問されます。
対象の番号を入力。
1234567Which names would you like to activate HTTPS for?-------------------------------------------------------------------------------1: example01.jonkara.net2: example02.jonkara.net-------------------------------------------------------------------------------Select the appropriate numbers separated by commas and/or spaces, or leave inputblank to select all options shown (Enter 'c' to cancel): 1
少し待つと、自動で証明書の取得が進みます。
nginxの設定ファイルには、必要な設定が自動追加されます。
123456Obtaining a new certificatePerforming the following challenges:tls-sni-01 challenge for example01.jonkara.netWaiting for verification...Cleaning up challengesDeployed Certificate to VirtualHost /etc/nginx/conf.d/example01.jonkara.net.conf for set(['example01.jonkara.net']) - HTTPSへのリダイレクト設定をするか指定します。
好きな方を選んで大丈夫ですが、Certbotに設定をいじられたくなければ「1」にしておきましょう。
12345678Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.-------------------------------------------------------------------------------1: No redirect - Make no further changes to the webserver configuration.2: Redirect - Make all requests redirect to secure HTTPS access. Choose this fornew sites, or if you're confident your site works on HTTPS. You can undo thischange by editing your web server's configuration.-------------------------------------------------------------------------------Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1 - 「Congratulations!」と表示されたら、設定完了です。
証明書の取得も終わり、使用できる状態になっています。
ブラウザでサイトを開いて確認してみましょう。
123456-------------------------------------------------------------------------------Congratulations! You have successfully enabled https://example01.jonkara.netYou should test your configuration at:https://www.ssllabs.com/ssltest/analyze.html?d=example01.jonkara.net------------------------------------------------------------------------------- - (参考)最後に重要事項が表示されます。
証明書や秘密鍵ファイルの保存場所などが書いてあるので、気になる人は確認してみましょう。
123456789101112131415161718IMPORTANT NOTES:- Congratulations! Your certificate and chain have been saved at:/etc/letsencrypt/live/example01.jonkara.net/fullchain.pemYour key file has been saved at:/etc/letsencrypt/live/example01.jonkara.net/privkey.pemYour cert will expire on 2018-01-09. To obtain a new or tweakedversion of this certificate in the future, simply run certbot-autoagain with the "certonly" option. To non-interactively renew *all*of your certificates, run "certbot-auto renew"- Your account credentials have been saved in your Certbotconfiguration directory at /etc/letsencrypt. You should make asecure backup of this folder now. This configuration directory willalso contain certificates and private keys obtained by Certbot somaking 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/donateDonating to EFF: https://eff.org/donate-le
【メモ】
半年ほど前にこの設定をした際、androidスマートフォンのChromeブラウザでサイトを表示すると、「この接続ではプライバシーが保護されていません」「このサイトのセキュリティ証明書には問題があります」というエラーが表示されることがありました。nginxの設定ファイルで証明書ファイル「cert.pem」を指定している部分を、中間CA証明書も含む「fullchain.pem」に置き換えると解消しました。
ssl_certificate /etc/letsencrypt/live/example01.jonkara.net/cert.pem;
↓
ssl_certificate /etc/letsencrypt/live/example01.jonkara.net/fullchain.pem;最新のCertbotでは、この不具合は改善されているようです。
SSLサーバ証明書の自動更新設定
以上でSSL証明書の取得ができましたが、Let’s Encryptの証明書の有効期限は3ヶ月となっていて、期限が過ぎると使えなくなります。
3ヶ月毎に手作業で取得し直すのは大変なので、期限が切れる前に自動で更新する設定を入れましょう。
(1)証明書更新のコマンド
証明書の更新にはこんなコマンドを使います。
1 |
certbot-auto renew --post-hook "service nginx restart" |
renewが、証明書を更新するオプションです。
–post-hookでは、証明書が更新された場合に実行するコマンドを指定します。
このコマンドの場合、もし証明書の期限が近ければ更新をして、新しい証明書を有効にするためにnginxを再起動します。
証明書の期限に余裕があれば、何も更新されず、–post-hookのコマンドも実行されません。
(2)cron設定
このコマンドを定期的に自動実行するため、cronにコマンドを登録します。
- cronの編集画面を表示。
1# crontab -e - 以下の設定を追加します。
ここでは、毎週日曜日の3時50分に、certbot-autoコマンドを実行するよう指定しました。
12PATH=/sbin:/bin:/usr/bin50 3 * * 0 /root/certbot-auto renew --post-hook "service nginx restart" - 設定されたことを確認。
1# crontab -l
【注意】
cronの設定でPATHを指定しないと、「HookCommandNotFound: Unable to find post-hook command service in the PATH」というエラーになる場合があるので注意です。
まとめ
以上で、無料SSL証明書を導入し、自動更新の設定までできました。
Certbotの指示に従って進めるだけで簡単にSSL証明書の設定ができるので、ぜひ試してみましょう。