見出し画像

FreeBSDサーバでLet's Encryptを使う

サイトのSSL化(https化)は証明書を発行してもらってインストールして、期限前に更新して…と金銭的コストのみならず人的コストも大きかったのですが、世のSSL化と軌を一にして無償のSSL証明書取得スキームであるLet's Encrypt(以降LE)が出来て便利になりました。

個人的には、何でもかんでもSSL化ってあんまり好きじゃないんですけどね

というわけでLEを利用してサイトのSSL証明書を取得しhttps化する流れをまとめておきます。

パッケージの取得

pkg install py38-certbot

確かこれで行けるはず。依存関係で必要なものがあれば自動的にやってくれます。

証明書の発行

# certbot --apache certonly

これでまずは初回の証明書発行が行われます。

Enter email address (used for urgent renewal and security notices)

と聞かれるので、連絡が付くメールアドレスを入れます。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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を入力。
そうすると、次に

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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を入力。
その次は、

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: ほげほげ
2: ふがふが
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):

と言われるので、証明書を作成したいサイト(複数選択可)の番号を選択します。なお、この段階の前までにhttpでサイトにアクセスできる環境(DNS的にもね)が完成していないとダメだと思う。
HTTPS化したいサイトの番号を入力すると、

Successfully received certificate.
Certificate is saved at: /usr/local/etc/letsencrypt/live/ほげほげ/fullchain.pem
Key is saved at: /usr/local/etc/letsencrypt/live/ほげほげ/privkey.pem
This certificate expires on 2022-06-05.
These files will be updated when the certificate renews.

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.

とか言われて無事証明書の作成と保存が完了します。なお、ここに掛かれてる通り、証明書の期限はそんなに長くないので、期限が切れる前に自動で更新するようにします。その辺りは後述。

Apacheの設定

Apache側の設定は二か所あります。
まず、/usr/local/etc/apache24/httpd.conf の中のsslモジュールに関するコメントアウトを外します。あと、443番をListenするようにします。

#LoadModule ssl_module libexec/apache24/mod_ssl.so

LoadModule ssl_module libexec/apache24/mod_ssl.so

Listen 80

Listen 80
Listen 443

次はサイトの設定です。
VirtualHostを使わず運用している人もいるかと思いますが、今後の拡張を考えればVirtualHostの中にサイト設定突っ込んでいった方が良いです。
というか、httpとhttpsの両方を立てる時点でVirtualHostにしないとダメですしね。

というわけで、/usr/local/etc/apache24/extra/httpd-vhosts.conf の中の該当する部分を↓な感じで設定。

<VirtualHost *:443>
DocumentRoot "/home/ほげほげ/public_html"
ServerName ほげほげ:443
ServerAdmin 俺様@俺様ドメイン
ErrorLog "/var/log/httpd-error.log"
TransferLog "/var/log/httpd-access.log"
SSLEngine on
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /home/ほげほげ/public_html>
Options Indexes FollowSymLinks SymLinksIfOwnerMatch
Require all granted
SSLOptions +StdEnvVars
</Directory>
BrowserMatch "MSIE [2-5]"nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
CustomLog "/var/log/httpd-ssl_request.log""%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
ServerAlias ほげほげ
SSLCertificateFile /usr/local/etc/letsencrypt/live/ほげほげ/fullchain.pem
SSLCertificateKeyFile /usr/local/etc/letsencrypt/live/ほげほげ/privkey.pem
Include /usr/local/etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

ついでに、http接続の旧サイトに来たアクセスをmod_rewriteでhttpsサイトにリダイレクトする設定を80番ポートのvirtualhostディレクティブに入れておくと、ブラウザが「この接続は危険です」とか言わなくなるのでおススメ。(何度も言うけど、個人的には何でもかんでもhttps化するのは好きじゃないんですけどね)

RewriteEngine on
RewriteCond %{SERVER_NAME} =ほげほげ
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

まあこんな感じかな。もちろん事前にhttpd.confで

LoadModule rewrite_module libexec/apache24/mod_rewrite.so

とコメントアウトしとかなきゃダメですよ。

これでめでたしめでたし。
…じゃなかった。証明書の更新をcronに仕込んでおかないと。

/etc/crontabの編集

こちらの80番ポートが使用中だと更新に失敗するので、次のような感じでcrontabに一行追加しておきましたよ。

10 2 1 * * root /usr/bin/killall httpd ; /usr/local/bin/certbot renew ; /usr/local/etc/rc.d/apache24 start

あとは適宜アクセスしてみたりログを確認したりしてチェックすること。

以上。

この記事が気に入ったらサポートをしてみませんか?