検索

Certbot 簡単Let’s Encrypt Nginx

FreeBSDのNginxで https SSL証明書を取得した時の記録です. Certbot で超簡単!
let's_encrypt

webサイトの暗号化 SSL

Let’s Encrypt(暗号化しましょう)ということですね.

webブラウザの URL の文字の始まりが、

http://     —>    https://

と ‘ s ‘ が入るだけですが、インターネットの通信内容を暗号化しましょうということのようです.

webブラウザでもhttp:// の場合の表示は「安全ではありません」と表示されます.

WEBサイトの SSL 公開までの準備

このサイトは、OS FreeBSD で Web-server Nginxでwebの公開すると仮定しています.

まずは、サイトに表示するページを作成します.

Nginx の設定を行なって

 listen       80;

 ( 80 は port 番号で一般的に暗号化されていないウエブデータ ( WWW ) となります. 443 は暗号化された port になります. )

webページを表示できるように nginx.conf を設定してあるとします.

FreeBSD Certbot で Let's Encrypt の方法のあらすじ

FreeBSD に pkg を使って Certbot をインストールします

				
					# pkg install py38-certbot-nginx
				
			

実際に認証局にアクセスして証明書の発行と設定ファイルを自動編集します.

コマンド certbot (に) –nginx (のオプション) -d ドメイン名 を入力します.

				
					# certbot --nginx -d freebsd.i-mks.site
				
			

英語の質問に答えるだけですが、

e-mail と Y と Y を入力するだけで、証明書を発行し勝手にサーバー内の nginx.conf のファイルを https:// でアクセスできるように編集されます.

http:// ではwebページが表示されなくなるので注意してください.

nginx を再起動して 設定を反映します..

				
					# service nginx restart
				
			

文字にすると、これだけで、サーバードメインの https:// で通信できるのです.

ただ、証明書の有効期間は3ヶ月らしいので、自動更新するようにしましょう.

/etc に periodic.conf という設定ファイルを作って、下のように weekly_certbot_enable=”YES” と記して保存して下さい.

				
					# vi /etc/periodic.conf

weekly_certbot_enable="YES"
				
			

これだけで、終わりです.

今回は、3度目でしたが、問題なくできたのですが、サーバーの更新まで数年間触らないため、方法を忘れてしまうので、健忘録としてサイトにアップしました.

次は、もう少し詳しく記してみます.

ここからは Certbot で Let's Encrypt の詳しい説明はここから

certbotをインストールしよう

FreeBSDでpkgを使ってpy38-certbot-nginx-1.18.0をインストールしてみよう

				
					#pkg install py38-certbot-nginx
				
			
				
					This port installs the "standalone" client only, which does not use and is not the certbot-auto bootstrap/wrapper script. The simplest form of usage to obtain certificates is:
# sudo certbot certonly --standalone -d , [domain2, ... domainN]>

NOTE:

The client requires the ability to bind on TCP port 80 or 443 (depending
on the --preferred-challenges option used). If a server is running on that
port, it will need to be temporarily stopped so that the standalone server
can listen on that port to complete the challenge authentication process.

For more information on the 'standalone' mode, see:

https://certbot.eff.org/docs/using.html#standalone

The certbot plugins to support apache and nginx certificate installation
will be made available in the following ports:

* Apache plugin: security/py-certbot-apache
* Nginx plugin: security/py-certbot-nginx

In order to automatically renew the certificates, add this line to
/etc/periodic.conf:

weekly_certbot_enable="YES"

More config details in the certbot periodic script:

/usr/local/etc/periodic/weekly/500.certbot-3.8
				
			

————- 翻訳 ———————–

このポートは、「スタンドアロン」クライアントのみをインストールします。
certbot-autoブートストラップ/ラッパースクリプトではありません。
証明書を取得するための最も簡単な使用方法は次のとおりです。

 #sudo certbot certonly –standalone -d 、[domain2、… domainN]>

ノート:

クライアントには、TCPポート80または443にバインドする機能が必要です(状況によって異なります)
使用される–preferred-challengesオプションで)。サーバーがその上で実行されている場合
ポート、スタンドアロンサーバーが
そのポートでリッスンして、チャレンジ認証プロセスを完了することができます。
「スタンドアロン」モードの詳細については、以下を参照してください。

  https://certbot.eff.org/docs/using.html#standalone

Apacheおよびnginx証明書のインストールをサポートするcertbotプラグイン
次のポートで利用できるようになります。

 * Apacheプラグイン:security / py-certbot-apache

 * Nginxプラグイン:security / py-certbot-nginx

証明書を自動的に更新するには、この行をに追加します

/etc/periodic.conf:
    weekly_certbot_enable = “YES”

certbot定期スクリプトの構成の詳細:

    /usr/local/etc/periodic/weekly/500.certbot-3.8

———

nginx.conf のバックアップ

nginx.conf を編集して port 80 でウエブサイトにアクセスできたら、nginx.conf などの付随するファイル ( conf.d ) をバックアップしておきましょう.

certbot -nginx コマンドを実行すると、nginx.conf のファイルを port 443 からアクセスするように編集されるからです.

				
					# cd /usr/local/etc/nginx
# cp nginx.conf nginx.conf.backup
# cp -R conf.d conf.d.backup
				
			

※ nginx.conf でserver デレクティブを include しているファイルはバックアップする対象にしましょう.

この場合、conf.d ディレクトリ ( フォルダー ) をバックアップしています.

Certbot で証明書発行の手順

ここからの操作は root 権限で行って下さい.

注意 ) sudo をインストールされている方は、コマンドの前にsudo を入れる必要があるかもしれません.

コマンド certbot –nginx -d freebsd.i-mks.site

-d は証明して欲しいドメイン名です.

				
					# certbot --nginx -d freebsd.i-mks.site
				
			

一つの証明書で 複数のドメインを登録する場合

-d のドメイン名を一度に複数並べることで登録可能になります.

				
					# certbot --nginx -d tabi.i-mks.site -d freebsd.i-mks.site
				
			

Let's Encrypt に無事つながると

サーバーの設定やルーターの設定が間違っていた場合は、エラーメッセージで certbot コマンドが止まりますので、その場合は、設定を確認してからもう一度試してみて下さい.

ルーターの 静的 IP マスカレード設定を サーバーの IP の対応ポートの 80 ( WWW ) と 443 は必要です.

証明書の取得

				
					Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
				
			

デバッグログを/var/log/letsencrypt/letsencrypt.logに保存します

メールアドレスを入力してください(緊急の更新とセキュリティ通知に使用されます)

name@address.com

メールの登録をしておくと、証明書の更新案内等がくるので、設定した方が良いと思います.

				
					- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
				
			

– – – – – – 翻訳 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

利用規約をお読みください

https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf。 絶対です

ACMEサーバーに登録するために同意します。 同意しますか?

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

(Y)は/(N)または: Y

				
					- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, 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 our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
				
			

– – – – – 翻訳 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

最初の証明書が正常に発行されたら、

創設者であるElectronicFrontierFoundationとあなたのメールアドレスを共有してください

Let’sEncryptプロジェクトのパートナーおよび非営利団体

Certbotを開発しますか? ウェブを暗号化する私たちの仕事についてあなたにメールを送りたいのですが、

EFFのニュース、キャンペーン、およびデジタルの自由をサポートする方法。

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

(Y)は/(N)または: Y

				
					NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.
				
			

翻訳 

次のステップ:

-証明書は、有効期限が切れる前に更新する必要があります。 Certbotはバックグラウンドで証明書を自動的に更新できますが、その機能を有効にするための手順を実行する必要がある場合があります。 手順については、https://certbot.org/renewal-setupを参照してください。

これで、Certbot による証明書の発行と、Nginx の設定まで、自動的にできています.

素晴らしい!

NEXT STEP: の自動更新設定は、もう少し後にします.

Nginx 再起動

Certbot によって変更された Nginx の設定を反映します.

				
					# /usr/local/etc/rc.d/nginx restart
または
# service nginx restart
				
			

Certbotの自動更新の検証

自動更新を設定する前に、証明書を正しく更新できることをテストすることが重要です。

更新プロセスをテストするには、certbotでドライランを行うことができます。

				
					# certbot renew --dry-run
				
			

(success)の文字が見えれば成功です.

バックグラウンドで証明書を自動的に更新1

Let’s Encryptの証明書は90日間のみ有効です。

証明書は、有効期限が切れる前に更新する必要があります。

Cartbot  インストール時のメッセージに

証明書を自動的に更新するには、この行をに追加します

/etc/periodic.conf:
    weekly_certbot_enable = “YES”

とメッセージがあったので、利用します.

/etc にファイルがなかったので、periodic.conf ファイルを作成します.

				
					# vi /etc/periodic.conf

weekly_certbot_enable="YES"
				
			

バックグラウンドで証明書を自動的に更新 2

更新1の方法でうまく動作しない場合は更新2を試します.

正午と真夜中にcertbot renewコマンドを毎日2回実行するようにcronに指示します。

certbot renewは、システム上の証明書の有効期限が近づいているかどうかを確認し、必要に応じて更新しようとします。

				
					# crontab -e 

0 0,12 * * * /usr/local/bin/certbot renew
				
			

自動更新プロセスが失敗した場合、Let’s Encryptは指定した電子メールにメッセージを送信し、証明書の有効期限が切れようとしているときに警告します。

新サーバーに移行した場合

Cartbot を使った Let’s Encrypt の手法は、同じURL で新しいサーバーにサイトを更新した場合にも問題なく動作しました. 

手動で、証明書を移行する方法もあるかと思いますが、 [ certbot –nginx -d ドメイン名 ] を動作させた方が簡単だと思います.

SSL証明書にドメインを追加するには

すでに、SSL証明書が設定されており、新たに ( サブ ) ドメインを追加する方法は  –expand オプションを付けて登録します.

下の例では、すでに登録されているドメイン freebsd.i-mks.site で 追加登録するドメインを tuika.i-mks.site とすると

				
					# certbot --expand -d freebsd.i-mks.site -d tuika.i-mks.site
				
			

さらに、-d ドメイン と続けて複数個登録可能です.

まとめ

Let’s Encrypt の証明書の発行やサーバーの設定には、多くの知識が必要でしたが、Certbot のおかげで、とても簡単でした.

個人で、WEBサーバーを運営する場合には、必須の設定になってきた暗号化ですが、無料の認証局やツールのおかげで、簡単にできることに感謝します.

今回は、HTTP-01 チャレンジで SSL証明書を発行しましたが、DNS-01 チャレンジならば、ドメインの登録だけでサブドメインの登録が必要ありません.
しかし、証明書を更新するたびにDNS のTXT を書き換える必要があるので自動更新が難しいですね.

参考リンク

もくじ