Meaningless Notebook

我輩は雑記帖である。名はまだない。


CentOS 7.3 に Nextcloud をインスト-ルする。

OSS 版の Aipo が 2018 年 03 月 31 日をもって提供終了するので替わりに Nextcloud を導入する事にした。

尚、本手順は外部公開をしない設定なのでセキュリティ-が緩い (HTTP で行う、SELinux の Webインターフェイス経由で更新を有効にしたままにする等)。

外部公開する場合は、Nextcloud 公式サイトのドキュメントを参考にする事。

手順は下記の通り。

  1. 必要なモジュ-ルをインスト-ルする
  2. 各モジュールのパラメ-タを設定する
  3. Nextcloud を配置・設定する
  4. 補足 テキストファイル参照時に文字化けする場合
  5. 補足 プロキシ環境下で使用する場合
  6. 補足 NFS 領域を使用する場合
  7. 補足 LDAP 認証を使用する場合

1.必要なモジュ-ルをインスト-ルする

1.PHP 7.1 をインスト-ル

1.epel リポジトリインスト-ル

yum install epel-release

2.remi リポジトリインスト-ル

rpm --httpproxy プロキシサ-バのIPアドレス 又は ホスト名 --httpport プロキシサ-バのポ-ト番号 -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

3.PHP インスト-ル

yum install --enablerepo remi-php71 php php-fpm php-gd php-json php-mysql php-curl php-mbstring php-process php-intl php-mcrypt php-imagick php-xml php-zip php-apcu php-opcache

2.Nginx をインスト-ル

1.Nginx リポジトリの追加

『/etc/yum.repos.d/nginx.repo』を新規追加する。

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2.Nginx インスト-ル

yum install nginx

3.MariaDB をインスト-ル

1.MariaDB リポジトリの追加

『/etc/yum.repos.d/MariaDB.repo』を新規追加する。

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

2.MariaDB インスト-ル

yum install MariaDB-server

3.その他のモジュ-ルをインスト-ル

下記コマンドを実行して、インスト-ルする。

yum install wget bzip2

2.各モジュールのパラメ-タを設定する

1.PHP 関連

1.『/etc/php.ini』を修正する。

902行目付近

;date.timezone =
date.timezone = "Asia/Tokyo"

2.『/etc/php-fpm.d/www.conf』を修正する。

24行目付近

; RPM: apache Choosed to be able to access some dir as httpd
; user = apache
user = nginx
; RPM: Keep a group allowed to write in log dir.
; group = apache
group = nginx

40行目付近

; Note: This value is mandatory.
; listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock

51行目付近

;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0660
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

392行目付近 (コメントアウトから有効行にする)

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

3.『/etc/php.d/10-opcache.ini』を修正する。

8行目付近

;opcache.enable_cli=0
opcache.enable_cli=1

19行目付近

;opcache.max_accelerated_files=4000
opcache.max_accelerated_files=10000

38行目付近

;opcache.revalidate_freq=2
opcache.revalidate_freq=1

4.ワ-クディレクトリ所有権設定

下記コマンドを実行して、所有権を apache から nginx へ変更する。

chown -R root:nginx /var/lib/php/*

5.サ-ビス起動 及び 自動起動設定

systemctl start php-fpm
systemctl enable php-fpm

2.MariaDB関連

1.『/etc/my.cnf.d/server.cnf』を修正する。

12行目付近

[mysqld]
innodb_large_prefix=on
innodb_file_format=barracuda
innodb_file_per_table=true

2.サ-ビス起動 及び 自動起動設定

systemctl start mariadb
systemctl enable mariadb

3.下記コマンドを実行して、MariaDB の初期設定をする。

mysql_secure_installation

*

root のパスワ-ド設定や、デフォルトで設定されている匿名ユ-ザ-、テスト DB の削除実施を対話ダイアログで問われる。

4.Nextcloud 用の DB を作成する。

下記コマンドを実行して、MariaDB へ root でログインする。

Nextcloud 用の DB 及び ユ-ザ-を作成する。

CREATE USER 'Nextcloud用ユ-ザ-名'@'localhost' IDENTIFIED BY 'Nextcloud用ユ-ザ-のパスワ-ド';
CREATE DATABASE Nextcloud用DB名 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL ON Nextcloud用DB名.* TO 'Nextcloud用ユ-ザ-名'@'localhost' IDENTIFIED BY 'Nextcloud用ユ-ザ-のパスワ-ド';

下記コマンドを実行して、MariaDB のポ-ト番号を確認する。

show variables like 'port';

下記コマンドを実行して、MariaDB からログアウトする。

quit

3.Nginx 関連

1.『/etc/nginx/conf.d/default.conf』を『default.conf.backup』にリネ-ムする。

2.『/etc/nginx/conf.d/server.conf』を新規作成する。

upstream php-handler {
    server unix:/var/run/php-fpm/php-fpm.sock;
}

server {
    listen 80;
    listen [::]:80;
    server_name ホスト名;

    # Add headers to serve security related headers
    # Before enabling Strict-Transport-Security headers please read into this
    # topic first.
    # add_header Strict-Transport-Security "max-age=15768000;
    # includeSubDomains; preload;";
    #
    # WARNING: Only add the preload option once you read about
    # the consequences in https://hstspreload.org/. This option
    # will add the domain to a hardcoded list that is shipped
    # in all major browsers and getting removed from this list
    # could take several months.
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header X-Download-Options noopen;
    add_header X-Permitted-Cross-Domain-Policies none;

    # Path to the root of your installation
    root Nextcloudを配置するドキュメントル-ト;

    location = /robots.txt {
       allow all;
       log_not_found off;
       access_log off;
    }

    # The following 2 rules are only needed for the user_webfinger app.
    # Uncomment it if you're planning to use this app.
    # rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    # rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json
    # last;

    location = /.well-known/carddav {
      return 301 $scheme://$host/remote.php/dav;
    }
    location = /.well-known/caldav {
      return 301 $scheme://$host/remote.php/dav;
    }

    # set max upload size
    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    # Enable gzip but do not remove ETag headers
    gzip on;
    gzip_vary on;
    gzip_comp_level 4;
    gzip_min_length 256;
    gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
    gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;

    # Uncomment if your server is build with the ngx_pagespeed module
    # This module is currently not supported.
    # pagespeed off;

    location / {
        rewrite ^ /index.php$uri;
    }

    location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
        deny all;
    }
    location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
        deny all;
    }

    location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+)\.php(?:$|/) {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        #fastcgi_param HTTPS on;
        #Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        fastcgi_param front_controller_active true;
        fastcgi_pass php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ ^/(?:updater|ocs-provider)(?:$|/) {
        try_files $uri/ =404;
        index index.php;
    }

    # Adding the cache control header for js and css files
    # Make sure it is BELOW the PHP block
    location ~ \.(?:css|js|woff|svg|gif)$ {
        try_files $uri /index.php$uri$is_args$args;
        add_header Cache-Control "public, max-age=15778463";
        # Add headers to serve security related headers (It is intended to
        # have those duplicated to the ones above)
        # Before enabling Strict-Transport-Security headers please read into
        # this topic first.
        # add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
        #
        # WARNING: Only add the preload option once you read about
        # the consequences in https://hstspreload.org/. This option
        # will add the domain to a hardcoded list that is shipped
        # in all major browsers and getting removed from this list
        # could take several months.
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Robots-Tag none;
        add_header X-Download-Options noopen;
        add_header X-Permitted-Cross-Domain-Policies none;
        # Optional: Don't log access to assets
        access_log off;
    }

    location ~ \.(?:png|html|ttf|ico|jpg|jpeg)$ {
        try_files $uri /index.php$uri$is_args$args;
        # Optional: Don't log access to other assets
        access_log off;
    }
}

*

公式サイトが提供している Nginx サンプル設定に、下記修正を施した内容。

1.HTTP 未使用
2.fastcgi_param_HTTPS on をコメントアウト

3.サ-ビス起動 及び 自動起動設定

systemctl start nginx
systemctl enable nginx

4.パケットフィルタリング設定

firewall-cmd --permanent --add-service=http
firewall-cmd --reload

3.Nextcloud を配置・設定する

1.適当なディレクトリへ移り (/usr/local/src 等)、 下記コマンドを実行して Nextcloud を取得する。

wget https://download.nextcloud.com/server/releases/nextcloud-バ-ジョン番号.tar.bz2
wget https://download.nextcloud.com/server/releases/nextcloud-バ-ジョン番号.tar.bz2.sha256
wget https://download.nextcloud.com/server/releases/nextcloud-バ-ジョン番号.tar.bz2.asc
wget https://nextcloud.com/nextcloud.asc

2.チェックサムの確認

sha256sum -c nextcloud-バ-ジョン番号.tar.bz2.sha256 < nextcloud-バ-ジョン番号.tar.bz2

*

正常なら”完了”と出力される。

3.署名確認

1.公開鍵インポ-ト

gpg --import nextcloud.asc

2.署名確認

gpg --verify nextcloud-バ-ジョン番号.tar.bz2.asc nextcloud-バ-ジョン番号.tar.bz2

*

正常なら”Nextcloud Security security@nextcloud.com”からの正しい署名”と出力される。

4.Nextcloud 展開・配置

1.下記コマンドを実行して、Nextcloud を展開する。

tar -xjf nextcloud-バ-ジョン番号.tar.bz2

2.設置

mv ./nextcloud Nextcloudを配置するドキュメントル-ト

3.所有権設定

chown -R nginx:nginx Nextcloudを配置したドキュメントル-ト

5.SELinux 設定

1.ラベル再設定

下記コマンドを実行して、配置した Nextcloud ディレクトリの SELinux ラベルを再設定する。

restorecon -R Nextcloudを配置したドキュメントル-ト

2.ポリシ-設定

以下のポリシ-をOnにする。

httpd_unified

ポリシ-のパラメ-タ値を確認するコマンド

getsebool -a | grep キ-ワ-ド

ポリシ-のパラメ-タ値を設定するコマンド

setsebool -P ポリシ-名=on

6.Web ブラウザから初期設定をする。

Web ブラウザから『http://Nextcloud インスト-ルマシンの IPアドレス 又は ホスト名』にアクセスして画面ダイアログに従って入力する。

7.APCu 設定

『nextcloud/config/config.php』に追記修正する。

$CONFIG = array (
  中略
  'memcache.local' => '\\OC\\Memcache\\APCu',
);

8.バックグラウンドジョブ設定

下記コマンドを実行して、nginx ユ-ザ-の cron ジョブを設定する。

crontab -u nginx -e

下記1行を追記する。

*/15 * * * * php -f Nextcloudドキュメントル-ト/cron.php

下記コマンドを実行して、nginx ユ-ザ-の cron ジョブを確認する。

crontab -u nginx -l

Web ブラウザより管理者で Nextcloud にログインし、『設定』⇒『Administratorion 基本設定』のバックグラウンドジョブに『Cron』を選択する。

4.補足 テキストファイル参照時に文字化けする場合

1.『nextcloud/apps/files_texteditor/lib/Controller/FileHandlingController.php』を修正する。

100行目付近

 /**
 $encoding = mb_detect_encoding($fileContents . 'a', 'UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII', true);
 */
 $encoding = mb_detect_encoding($fileContents . 'a', 'SJIS-win, UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII', true);

2.下記コマンドを実行して、サ-ビスを再起動する。

systemctl restart php-fpm
systemctl restart nginx

5.補足 プロキシ環境下で使用する場合

1.『nextcloud/config/config.php』に追記修正する。

$CONFIG = array (
    中略
  'proxy' => 'プロキシサ-バ-の IPアドレス 又は ホスト名:ポ-ト番号',
);

2.ポリシ-設定

以下のポリシ-をOnにする。

httpd_can_network_connect
httpd_can_network_relay

ポリシ-のパラメ-タ値を確認するコマンド

getsebool -a | grep キ-ワ-ド

ポリシ-のパラメ-タ値を設定するコマンド

setsebool -P ポリシ-名=on

3.下記コマンドを実行して、サ-ビスを再起動する。

systemctl restart php-fpm
systemctl restart nginx

6.補足 NFS 領域を使用する場合

1.ポリシ-設定

以下のポリシ-をOnにする。

httpd_use_nfs

ポリシ-のパラメ-タ値を確認するコマンド

getsebool -a | grep キ-ワ-ド

ポリシ-のパラメ-タ値を設定するコマンド

setsebool -P ポリシ-名=on

2.外部ストレ-ジ設定

1.アプリ有効化

Web ブラウザより管理者で Nextcloud にログインし、『アプリ』⇒『External storage support 』を有効化する。

2.外部ストレ-ジ設定

Web ブラウザより管理者で Nextcloud にログインし、『設定』⇒『Administration 外部ストレ-ジ』を選択して、設定する。

7.補足 LDAP 認証を使用する場合

1.php-ldap インスト-ル。

yum install --enablerepo remi-php71 php-ldap

2.LDAP 認証設定

1.アプリ有効化

Web ブラウザより管理者で Nextcloud にログインし、『アプリ』⇒『LDAP user and group backend 』を有効化する。

2.LDAP 認証設定

Web ブラウザより管理者で Nextcloud にログインし、『設定』⇒『Administration LDAP / AD 統合』を選択して、設定する。

ファイル管理がメインだけど、アプリにメ-ル、カレンダー、タスクってあるから Aipo の替わりとして結構使えそう。

参考元

  1. CentOS7にnginxでNextcloudをインストール
  2. Nextcloud 13 Administration Manual
  3. mariadb mysql ポート番号を調べる
  4. プロキシ設定まとめ
  5. CentOS7 に 旧 APC (apcu & opcache)を導入する
  6. CentOS6.3にphpshをインストールする
  7. owncloud (Ver. 8.1.1) texteditor Shift-JIS の文字化け対処
  8. 祝日のリストが欲しい

Amazon