LoginSignup
5
2

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-07-20

経緯

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

で、ようやっと先日重い腰を上げて残りのエラー修正に乗り出したので、それについての話。
警告のスクショ

環境

  • AWS Lightsail
  • Ubuntu 18.04
  • PHP7.3
    • 別のアプリの要求で PHP7.4 も稼働中だが、Nextcloudは7.3を使っていた。
  • Nextcloudのバージョン: 17.07 -> 18.07(作業途中で更新してしまった。)
  • ウェブサーバー: nginx
    • nginxのユーザー名: nginx(本来はwww-dataにするのが慣例だったらしい。)

データベースの更新

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。

/etc/php/7.3/fpm/pool.d/www.conf
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;を追記する。

/etc/nginx/conf.d/nextcloud.conf
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

結果

結果のスクショ

これで一安心。

5
2
0

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
5
2