Nextcloudのセキュリティ&セットアップ警告の解消

本記事はQiitaに投稿した記事と同一の内容となります。

経緯

半年前にサーバーの移転に伴ってNextcloudも引っ越ししたのだが、いくつかエラーが起きていたので、致命的なのだけ修正して放置していた。

で、ようやっと先日重い腰を上げて残りの警告の修正に乗り出した。

{{ image(src=“alert.png”, alt=“エラーのスクショ”) }}

環境

データベースの更新

occ db:convert-filecache-bigintを実行しろと言われたのでそうする。

$ cd /path/to/nextcloud # Nextcloudのディレクトリへ移動

$ sudo -u nginx php occ db:convert-filecache-bigint # nginx ユーザーとして phpでコマンドを実行
This version of Nextcloud is not compatible with > PHP 7.3.<br/>You are currently running 7.4.8. # サポート外のphp7.4が呼び出されてしまい、エラー

# 念のためphp7.3を起動してから再度バージョンを指定してphpのコマンドを実行
$ sudo systemctl start php7.3-fpm.service 
$ sudo -u nginx php7.3 occ db:convert-filecache-bigint
Nextcloud or one of the apps require upgrade - only a limited number of commands are available
You may use your browser or the occ upgrade command to do the upgrade
Following columns will be updated:

* mounts.storage_id
* mounts.root_id
* mounts.mount_id

This can take up to hours, depending on the number of files in your instance!
Continue with the conversion (y/n)? [n] y

これで完了したらしい。

何を思ったかこの段階でなぜかNextcloudの更新をしてしまった。ので、ここからバージョンが18.07に変わる。

バージョンアップしたら追加でデータベースに修正があったらしくocc db:add-missing-indicesを実行するようにいわれたので対応。

$ sudo -u nginx php occ db:add-missing-indices # バージョンつけ忘れてたが実行できた。Nextcloud18.07でPHP7.4に対応したのだろうか?
Command "db:add-missing-indices" is not defined. #コマンドが定義されないといわれたがそのまま実行できた。
Do you want to run "db:add-missing-indices" instead?  (yes/no) [no]:
> yes

Check indices of the share table.
Check indices of the filecache table.
Check indices of the twofactor_providers table.
Check indices of the login_flow_v2 table.
Check indices of the whats_new table.
Check indices of the cards table.
Check indices of the cards_properties table.
Check indices of the calendarobjects_props table.
Adding calendarobject_calid_index index to the calendarobjects_props table, this can take some time...
calendarobjects_props table updated successfully.
Check indices of the schedulingobjects table.
Adding schedulobj_principuri_index index to the schedulingobjects table, this can take some time...
schedulingobjects table updated successfully.

環境変数の設定

Installation on Linux — Nextcloud latest Administration Manual latest documentation

php-fpmはデフォルトではいくつかの環境変数を無効化しているらしく、それを有効にする必要があるらしい。 具体的には以下の項目がデフォルトではコメントアウトされているので # を取り除くだけでOK。

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp   

今回はNextcloud以外のPHPのアプリケーションは使わなくなる方向性だったので問題なかったが、他のアプリケーションに影響しないのだろうか…?

HSTS

正直今回のような完全に自分専用のサイトについては設定する必要がない気もしたが、一応修正した。

HSTSとは

まずなんのことかわからないので調べた。 Wikipediaによると、 HTTP Strict Transport Security の略らしい。 セキュアなサイトを運用する場合、アクセスをHTTPSのみにして、HTTPでアクセスすると、HTTPSのアドレスにリダイレクトするというのが一般的な処理だと思う。 このリダイレクト自体はHTTPのため改ざんができ、別のサイトにリダイレクト先を差し替えたりといったことができてしまうことで、それを防ぐための仕組みがHSTSらしい。 一度HTTPSで通信したサイトにこれが設定されていると、以降ブラウザ側でHTTPのアドレスを指定した際にHTTPSに置き換えるとか。

Nginx の設定

nginxのサイト別の設定ファイルのHTTPS用の設定にadd_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;を追記する。

server {
    listen 443 ssl http2;
    # 中略
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    # 中略
}

参考

HTTP Strict Transport Security (HSTS) and NGINX - NGINX Hardening and security guidance — Nextcloud latest Administration Manual latest documentation

結果

{{ image(src=“result.png”, alt=“結果”) }} これで一安心。

他の言語