世界に公開

2021/3/16,2021/07/07


webサーバ([nginx]http)

サーバ証明書を発行するためには、webサーバが稼働していてhttp://.....でアクセスできることが必要条件である。
webサーバはapache httpでもnginxでもよい。 「Raspberry Pi版Misskeyインストール方法詳説」(以下「インストール方法詳細」)にはnginxをインストールしている。いままでapache httpを使っていたのでどうかと思うが、手間をかけたくないので素直に「インストール方法詳細」に従ってnginxをインストールする。

#!/bin/bash
#管理者権限で実行 nginxをインストール
apt install -y nginx ufw certbot
systemctl restart nginx;systemctl status nginx;

#firewallを設定
# sshは30秒に6回以上の接続があるとき、攻撃を受けたとみなして接続を不許可にする
# http,httpsを許可
ufw enable;ufw default deny
ufw limit 22;ufw allow 80;ufw allow 443
#ufw delete allow 443; #間違ったルールを削除する
ufw status;#ルールを確認
このままでは「Welcome to nginx!」になるので、index.nginx-debian-htmlを無効化して、index.htmlをつくる。
サーバー名はmisskey.mydomain.org(仮称)とした。適宜自分のドメインに読み替えてほしい。
/etc/nginx/sites-enabled/defaultを修正する。
修正前修正後
index index.html index.htm index.nginx-debian.html; index index.html;
server_name _; server_name misskey.mydomain.org;

index.htmlはタイプしてもいいが、間違えそうなので、サンプルを用意した。
#!/bin/bash
#管理者権限で実行 /var/www/html/index.htmlをつくる
wget -O /var/www/html/index.html http://www.maibun.org/~nt/technicalnote/raspberryPiOS/Misskey/sample.html

SSL証明書はどこにお願いするか?

取得済のドメインを使用することにして、 お付き合いの長いレジストラでSSL証明書を手配しようと思ったが、ちょっといいお値段だった。
SSL証明書をどこに手配するか問題だが、現在の手札は3枚。
世界に公開するものにオレオレ証明書はないだろう。
  1. Let's Encryptさん (https://letsencrypt.org/)で証明書を手配
    無料だが、証明書の有効期限が90日、再発行可能(自動更新の方法もあるらしい)
    「再発行分からお代をいただきます」というサービスもあるだろうが、ここは無料

  2. 「インストール方法詳細」に従い、CloudFlareさん(https://www.cloudflare.com/ja-jp/)で証明書を手配
    「インストール方法詳細」によるとCloudflareがDNS,リバースプロキシ,CDN(contents delivery network)を提供してくれるとあるが、DNSは自ドメインを取得する際に設定しているし、CDNをお願いするほどトラフィックは輻輳しないと想定している。(そのような高トラフィック環境ならRaspberryPiは使わないだろう?)
    Cloudflareでいろいろお願いする手順(登録項目)などを事前に知りたいが、そのような情報は見つからなかった。 CloudFlareさんにユーザとして登録したが、
    現在の使用しているレジストラにログインして、「DNSを書き換えろ」と言ってくる。登録しているDNS(レジストラのDNSを優先的に使用)を修正して、「CloudFlareのDNSを最優先にしろ」だそうだ。
    今動いているものは、触りたくないので保留している。
    Misskey的にはCloudFlareありでもなしでもhttps://が使えれば文句はないのだろう?

  3. 高くてもお付き合いのあるレジストラで証明書を手配
     選択肢はあるが
この3つから検討する。

証明書を入手

検討の結果、A.Let's Encryptさんで手配した。
Misskeyはmisskey.mydomain.org(仮称)とした。適宜自分のドメインに読み替えてほしい。
#管理者権限で実行
certbot certonly --webroot -w /var/www/html -d misskey.mydomain.org
以下、このようなやり取りが続く。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): ----メールアドレスを入力 xxxxxxxx@gmail.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 at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A  ---- 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: N ---- Y,Nどちらでもいい。
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for misskey.mydomain.org
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/misskey.mydomain.org/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/misskey.mydomain.org/privkey.pem
   Your cert will expire on 2021-06-16. 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/が作られて、この下にSSL証明書やプライベートキーが生成される。
大切なものなので紛失しないようにどこかにバックアップしておこう。
/etc/letsencrypt/の構造を確認する。
#管理者権限で実行
tree /etc/letsencrypt/
このような構造になっている。
fullchain.pemとprivkey.pemが後で必要になるので、パス,ファイル名をメモする。
/etc/letsencrypt/
├── accounts
│   └── acme-v02.api.letsencrypt.org
│       └── directory
│           └── 0123456789abcdefghijklmnopqrstuv
│               ├── meta.json
│               ├── private_key.json
│               └── regr.json
├── archive
│   └── misskey.mydomain.org
│       ├── cert1.pem
│       ├── chain1.pem
│       ├── fullchain1.pem
│       └── privkey1.pem
├── cli.ini
├── csr
│   └── 0000_csr-certbot.pem
├── keys
│   └── 0000_key-certbot.pem
├── live
│   ├── README
│   └── misskey.mydomain.org
│       ├── README
│       ├── cert.pem -> ../../archive/misskey.mydomain.org/cert1.pem
│       ├── chain.pem -> ../../archive/misskey.mydomain.org/chain1.pem
│       ├── fullchain.pem -> ../../archive/misskey.mydomain.org/fullchain1.pem
│       └── privkey.pem -> ../../archive/misskey.mydomain.org/privkey1.pem
├── renewal
│   └── misskey.mydomain.org.conf
└── renewal-hooks
    ├── deploy
    ├── post
    └── pre

webサーバ([nginx]https)

https://でサービスを開始するための設定を行う。
nginxの設定ファイルをつくる。misskey.nginxもとにして、サーバー名と証明書のファイル名を書き換える。
#管理者権限で実行
#SSL証明書とプライベートキーを探す フルパス名を確認
find /etc -name fullchain.pem;find /etc -name privkey.pem
#misskey.confをコピー
cp /home/misskey/misskey/docs/examples/misskey.nginx /etc/nginx/conf.d/misskey.conf
#テキストエディタvi,emacs,nano等で編集--管理者はgeditが使えない
vi /etc/nginx/conf.d/misskey.conf
修正は4ヶ所ある。
修正前修正後
server {
    listen 80;
    listen [::]:80;
    server_name example.tld;  [このserver_nameを書き換える]
server {
    listen 80;
    listen [::]:80;
    server_name misskey.mydomain.org;
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.tld;  [このserver_nameを書き換える]
    ssl_session_cache shared:ssl_session_cache:10m;

    # To use Let's Encrypt certificate  [この2行を書き換える]
    ssl_certificate     /etc/letsencrypt/live/example.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.tld/privkey.pem;
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name misskey.mydomain.org;
    ssl_session_cache shared:ssl_session_cache:10m;

    # To use Let's Encrypt certificate
    ssl_certificate     /etc/letsencrypt/live/misskey.mydomain.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/misskey.mydomain.org/privkey.pem;
#管理者で実行
#nginx *.confファイルの記述を確認
nginx -t
問題がなければ、Misskeyを起動してみる。スマホまたはPCのwebブラウザからhttps://misskey.mydomain.org/にアクセスする。Misskeyログイン画面が出ると成功。
#ユーザmisskeyで実行
cd ~/misskey/
NODE_ENV=production npm start
webブラウザで動作を確認した後、misskeyを一度終了する。

Misskeyデーモンをつくる

Raspberry Piを再起動すると、Misskeyは停止する。
Raspberry Pi起動時にMisskeyも自動起動させたい。Misskey起動手順をデーモン化して、systemctlに登録しよう。
misskey.serviceをつくるタイプしてもいいが、間違えそうなのでサンプルを用意した。 ダウンロードして編集してもよい
#管理者で実行
touch /etc/systemd/system/misskey.service
chmod 777 /etc/systemd/system/misskey.service
cat >> EOF < /etc/systemd/system/misskey.service
[Unit]
Description=Misskey daemon

[Service]
Type=simple
User=misskey
ExecStart=/usr/bin/npm start
WorkingDirectory=/home/misskey/misskey
Environment="NODE_ENV=production"
TimeoutSec=60
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=misskey
Restart=always

[Install]
WantedBy=multi-user.target
EOF
#管理者で実行
#これをダウンロードして編集してもよい
wget -O /etc/systemd/system/misskey.service http://www.maibun.org/~nt/technocalnote/rasperryPiOS/Misskey/misskey.service
chmod 777 /etc/systemd/system/misskey.service
#編集するとき
#vi /etc/systemd/system/misskey.service

デーモンを起動してみる。
systemctl daemon-reload

#misskeyを再起動、状態を確認、20秒後にもう一度状態を確認
systemctl restart misskey;systemctl status misskey;sleep 20;systemctl status misskey;

#misskeyの起動が確認できたのち、misskeyを自動起動に設定
systemctl enable misskey
ここまで問題がなければ、RaspberryPiを再起動し、misskeyが自動起動することを確認する。
#管理者で実行
reboot

証明書更新

2021/07/07,2021/8/4追記

証明書が自動更新されると思う。有効期限などを確認する。
certbot certificates
以下のように有効期限が延長されていることを確認する。
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
  Certificate Name: misskey.mydomain.org
    Domains: misskey.mydomain.org
    Expiry Date: 2021-10-01 23:30:56+00:00 (VALID: 85 days)
    Certificate Path: /etc/letsencrypt/live/misskey.mydomain.org/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/misskey.mydomain.org/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

自動更新ができなかったときはマニュアルで更新を試みる。
更新期日まで30日以上あるときは、更新されない。
certbot renew


いつ自動更新したか気になるときは/var/log/letsencrypt/letsencrypt.logを見る。 2020-08-02に証明書が失効するが、2021-07-04に自動更新を試みている(auto-renewing...)。
less /var/log/letsencrypt/letsencrypt.log

2021-07-04 09:29:21,259:DEBUG:certbot.storage:Should renew, less than 30 days before certificate expiry 2021-08-02 11:56:23 UTC.
2021-07-04 09:29:21,260:INFO:certbot.renewal:Cert is due for renewal, auto-renewing...

(このあといろいろあり、自動更新が完了する。)

証明書を更新した後は必ず、nginxを再起動する。
再起動しないととても残念なことになる。
サーバ側で新しい証明書を取得しても、nginxは古い証明書を使い続けるので クライアント側に「証明書が期限切れです」みたいな表示がでる。
systemctl restart nginx;systemctl status nginx

Raspberry Pi版misskeyインストール方法詳説  https://join.misskey.page/ja/wiki/developers/installation/raspberryPi

Raspberry Pi版misskeyインストール方法詳説  https://misskey-site.com/posts/raspberry-pi版Misskeyインストール方法詳説

Misskey構築の手引き  https://github.com/misskey-dev/misskey/blob/master/docs/setup.ja.md

Server World -- Debian10 Buster -- WEBサーバ -- Nginx:SSL/TLSの設定 -- SSL証明書を取得する(Let's Enctypt)
https://www.server-world.info/query?os=Debian_10&p=ssl&f=2


さくらのVPS cerbot-autoの更新  https://manual.sakura.ad.jp/vps/startupscript/certbot-caution.html