LoginSignup
16
22

More than 3 years have passed since last update.

ラズパイwebサーバをhttpsに対応させた

Last updated at Posted at 2019-12-09

はじめに

以前、ラズパイでwebサーバをつくる記事を書いたのですが、httpしか対応していませんでした。

検証用サーバなのでこれで十分なのですが、アクセスした時にアドレスバーに安全ではありませんって出てくるの、なんか気持ち的に嫌じゃないですか。

サーバのhttps化の記事は複数ありますが、ラズパイ固有の問題点とか、自分用にまとめておきます。

ちなみにwebサーバは

  • Apache2.4.38(Raspbian)

です。

参考記事

HTTPS (Let's Encypt) をRaspberry Pi (Stretch)で設定
Let's Encrypt 証明書でhttps化
Let'sEncryptの取得&自動更新設定してみた(CentOS7.1&Apache2.4.6)

Let's Encryptで証明書を取得

まず、Let's Encryptを利用するためのクライアントソフトを入れます。

ここでいきなり躓いたのが、ソフトの入れ方にRaspbian固有の問題があるらしくて、

git clone https://github.com/certbot/certbot /usr/local/certbot

であったり、

wget https://dl.eff.org/certbot-auto

でソースを落とすと、エラーが出て詰みます。(笑)

理由は以下が参考になりました。

RaspbianでLet's Encrypt(certbot)の自動更新に失敗する

aptでインストールすることにしました。

sudo apt install certbot

そして、以下を実行

sudo certbot certonly
Saving debug log to /var/log/letsencrypt/letsencrypt.log

How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Spin up a temporary webserver (standalone)
2: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Plugins selected: Authenticator webroot, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [E-mail address]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing 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
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): 3ch0.mydns.jp
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for 3ch0.mydns.jp
Input the webroot for 3ch0.mydns.jp: (Enter 'c' to cancel): /var/www/
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/3ch0.mydns.jp/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/3ch0.mydns.jp/privkey.pem
   Your cert will expire on 2020-03-07. 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"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making 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/donate
   Donating to EFF:                    https://eff.org/donate-le

対話的に操作可能です。/etc/letsencrypt/live/[ドメイン名]ディレクトリに鍵等の各種ファイルが生成されています。

これで証明書は取得できました。

sslの有効化

次に、sslを有効化していきます。

まず、以下を実行します。

sudo a2enmod ssl
sudo a2ensite default-ssl

そのあと、/etc/apache2/site-available/defaults-ssl.confに以下のように追記します。

/etc/apache2/site-available/defaults-ssl.conf
                #   SSL Engine Switch:
                #   Enable/Disable SSL for this virtual host.
                SSLEngine on

                #   A self-signed (snakeoil) certificate can be created by installing
                #   the ssl-cert package. See
                #   /usr/share/doc/apache2/README.Debian.gz for more info.
                #   If both key and certificate are stored in the same file, only the
                #   SSLCertificateFile directive is needed.
                #SSLCertificateFile     /etc/ssl/certs/ssl-cert-snakeoil.pem
                #SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
                SSLCertificateFile      /etc/letsencrypt/live/3ch0.mydns.jp/fullchain.pem #fullchain.pemへのパス!!
                SSLCertificateKeyFile /etc/letsencrypt/live/3ch0.mydns.jp/privkey.pem #privkey.pemへのパス!!

                #   Server Certificate Chain:
                #   Point SSLCertificateChainFile at a file containing the
                #   concatenation of PEM encoded CA certificates which form the
                #   certificate chain for the server certificate. Alternatively
                #   the referenced file can be the same as SSLCertificateFile
                #   when the CA certificates are directly appended to the server
                #   certificate for convinience.
                #SSLCertificateChainFile /etc/apache2/ssl.crt/server-ca.crt
                SSLCertificateChainFile /etc/letsencrypt/live/3ch0.mydns.jp/chain.pem #chain.pemへのパス!!


apache2を再起動します。
sudo service apache2 restart

ちなみに、このファイルの初めの方にDocument rootの設定があり、これをきちんと設定する必要があります。

これに気づかなくて10分くらい悶えてた。(なんでDocument rootって一元的に管理できないんだろう。)

証明書更新の自動化

証明書の更新をする必要があるのですが、証明書の有効期限は90日。
そのため、定期的に更新するように自動化します。

以下のようなシェルスクリプトを作成しました。

/etc/cron.monthly/letsencrypt
#!/bin/sh

certbot renew --deploy-hook "service apache2 restart" 

certbot renewコマンドは過去に取得した証明書のうち、有効期限が30日を切ったものを更新するコマンドです。

--post-hookではなく--deploy-hookを使うのは、コマンドが成功した時のみapacheを再起動させるためです。

また、動作の確認をしたい場合、certbot renewの制限回数に引っかかってしまうといけないので、--dry-runオプションを付けて実行すると良いと思います。(1敗)

参考記事

レート制限 - Let's Encrypt

cronの設定をします。

crontab -u root -eで設定ファイルを以下の通りに記述します。

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

00 12 01 * * root /etc/cron.monthly/letsencrypt >/dev/null 2>&1 #これを追記

毎月1日、12時に実行します。

以上で自動更新の設定は完了です。

cronが動いているかどうかは
service cron statusで確認できます。

おわり

https://でアクセスできるようになりました。うれしいね。

16
22
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
22