CentOS7にNextcloudインストール

2017.05.26追記  Nextcloudバージョン12がリリースに伴いインストール手順を追加

Nextcloud構成環境

CentOS7.3のminmalインストール環境にNextcloudをインストールする手順です。

今回この構成で「Nextcloud」を構築していきます。

  • php7.1
  • nginx (SSL証明書はLet's Encrypt)
  • php-fpm
  • MariaDB
  • Nextcloud

サーバについて

「Nextcloud」を構築する環境は、簡単にインターネットからも使えるようにしたかったのでVPSを使用することにしました。
あと、自宅サーバの場合ハードディスクの容量が大きいのでVPSよりは多くのデータを保存しておけるのですが、サーバの機器故障リスクや火事が怖いのでVPSを選択したという理由もあります。

VPSは普段よく使っていて慣れている下記のなかから、今回は「ConoHa」のVPS上に環境を構築してみました。

ドメインについて

「Nextcloud」をインターネットに公開して使用する場合、独自ドメインをまだ持っていないのであればお名前.comとかで、簡単に取得できるので検討してみてください。

ドメインを持っていたほうがURLを覚えるの簡単だし、知り合いとデータ共有をする場合にURLを伝えるのも楽ですよ。

php 7.1

「Nextcloud」の環境を構築するにあたり、まずはphp7.1をインストールしていきます。

php7.1をインストールするには「epelリポジトリ」と「remi」リポジトリが必要なので、最初にそれらのリポジトリを追加してからphp7.1のインストールを行っていきます。

epelリポジトリ追加

「yum」で「epel」リポジトリを追加した後に、「--enablerepo」で明示的に指定しない限り「epel」リポジトリを使用しないように「/etc/yum.repos.d/epel.repo」の設定を行っておきます。

※「epel」と「remi」リポジトリの追加方法についての詳しい説明はこちらのページで解説していますので、興味があれば参照してみてください。

# yum -y install epel-release
# cd /etc/yum.repos.d
# cp -p epel.repo epel.repo_yyyymmdd
# vi epel.repo
編集前
enabled=1
編集後
enabled=0

remiリポジトリ追加

php7.1を「yum」でインストールするために必要な「remi」リポジトリをインストールします。

# rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

php7.1インストール

php7.1をインストールする際にその他に必要なパッケージもあわせてインストールしてしまいます。

# yum install --enablerepo=epel,remi-php71 php php-mysql php-mbstring php-intl php-mcrypt php-imagick php-xml php-zip php-process php-apcu php-gd php-opcache

2017.05.25追記 php-opcacheもインストールするパッケージに追加

php.ini設定

「Nextcloud」で必要なタイムゾーンの設定を行います。

# cp -p /etc/php.ini /etc/php.ini_yyyymmdd
# vi /etc/php.ini
編集前
;date.timezone =
編集後
date.timezone = "Asia/Tokyo"

nginx

「nginx」は公式サイトのリポジトリを使用してインストールを行っていきます。

nginxリポジトリ作成

今回は「mainline」版をインストールしていきますので、それ用のリポジトリを作成します。

※「nginx」のリポジトリ作成についてはこちらのページで解説ししているので参考にしてみてください。

# vi /etc/yum.repos.d/nginx.repo

設定内容

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

nginxインストール

作成したnginx用のリポジトリを使用してインストールを行っていきます。

# yum --enablerepo=nginx install nginx

自動起動設定

「nginx」のインストールが完了しましたら、とりあえず起動させてから、自動起動設定を行っておきます。

# systemctl start nginx
# systemctl enable nginx

「Nextcloud」用の設定ファイル作成は「Nextcloud」をインストールした後に行います。

php-fpm

「php-fpm」のインストールと設定を行っていきます。

php-fpmインストール

「yum」を使用して「php-fpm」をインストールしていきます。

# yum install --enablerepo=remi-php71 php-fpm

設定

「php-fpm」の設定ファイルである「/etc/php-fpm.d/www.conf」に設定を行っていきます。

# cd /etc/php-fpm.d/
# cp -p www.conf www.conf_yyyymmdd
# vi www.conf

設定内容

設定変更箇所は「php-fpm」の実行ユーザとグループを「nginx」に変更するのと、環境設定env部分のコメントを外して設定を有効にするという内容になります。

実行ユーザ・グループ設定前
user = apache
group = apache
実行ユーザ・グループ設定後
user = nginx
group = nginx
環境設定設定前
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp
環境設定設定後
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

起動・自動起動設定

設定が完了しましたら「php-fpm」の起動と、自動起動設定を行います。

# systemctl start php-fpm
# systemctl enable php-fpm

セッション情報格納ディレクトリ所有者変更

phpのセッション情報が格納されるディレクトリ(/var/lib/php/session)の所有者が「root:apache」となっているため、このままでは「nginx」がセッション情報を格納できないため、所有者を「nginx:nginx」に変更します。

# cd /var/lib/php 
# chown -R nginx:nginx session/

変更後は「php-fpm」を再起動しておきます。

# systemctl restart php-fpm

MariaDB

データベースサーバである「MariaDB」のインストールと、初期設定および「Nextcloud」用データベースの作成を行っていきます。

MariaDBインストール

「yum」で「MariaDB」をインストールしていきます。

# yum install mariadb mariadb-server

MariaDB設定

「UTF8」を使用できるようする設定と、index指定時のエラー対策を行っていきます。

※index指定時のエラー対策については下記サイトを参考とさせていただきました。
http://qiita.com/k-kawa@github/items/49effa6b295d4c90fac0

# cp -p /etc/my.cnf.d/server.cnf /etc/my.cnf.d/server.cnf_yyyymmdd
# vi /etc/my.cnf.d/server.cnf

設定内容

[mysqld]部分に設定を追記します。

[mysqld]
character-set-server = utf8
innodb_large_prefix = on
innodb_file_format = barracuda
innodb_file_per_table = true

MariaDBの起動と自動起動設定

「server.cnf」の設定が完了しましたら「MariaDB」の起動と、自動起動の設定をあわせて行います。

# systemctl start mariadb
# systemctl enable mariadb

初期設定

次は「mysql_secure_installation」を実行して「MariaDB」の初期設定を行っていきます。

詳しい説明はこちらのページで解説していますので、興味があれば参照してみてください。

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):<--「Enter」キーを押下
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] <--「y」または「Enter」キーを押下
New password:       <--「MariaDB」のrootパスワードを設定
Re-enter new password:  <--パスワードの再入力
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] <--「y」または「Enter」キーを押下
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] <--「y」または「Enter」キーを押下
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] <--「y」または「Enter」キーを押下
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] <--「y」または「Enter」キーを押下
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

データベース作成

初期設定が完了しましたら、「Nextcloud」用のデータベースを作成します。

# mysql -u root -p
Enter password:

データベースの作成方法は「CREATE DATABASE 」でデータベースを作成後、「GRANT ALL PRIVILEGES ON」で作成したデータベースの管理ユーザを作成し「FLUSH PRIVILEGES」でユーザ情報の反映するという手順となります。

> CREATE DATABASE データベース名 ;
> GRANT ALL PRIVILEGES ON データベース名.* TO 管理ユーザ名@localhost IDENTIFIED BY 'パスワード';
> FLUSH PRIVILEGES;
> quit

作成例

今回は「Nextcloud」用のデータベースを下記の内容で作成しました。

  • データベース名 nextcloud
  • 管理ユーザ名 nextadmin
  • パスワード nextpass
MariaDB [(none)]> CREATE DATABASE nextcloud ;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON nextcloud.* TO nextadmin@localhost IDENTIFIED BY 'nextpass';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> quit

※実際に作成するときは、データベースの名前や管理ユーザ名は適宜指定していただいてかまいませんので、第3者に推測されないようなユーザとパスワードを設定してください。

Nextcloud

「Nextcloud」自体のインストールはとても簡単で、公式サイトからソースをダウンロードし解凍後、webサーバで公開するディレクトリに設置するという作業になります。

ソースのダウンロード

「Nextcloud」の公式サイト(https://nextcloud.com)にアクセスし、画面右上の「Download」をクリックします。

「Get Nextcloud Server」の「Downdload」をクリックします。

「Download Nextcloud」をクリックすることでzipでアーカイブされたソースをダウンロードすることが出来ますが、今回はtar.bz2形式でアーカイブされているソースと、データチェックに使用するのmd5ファイルやPGP関連のファイルもダウンロードしたいため、「Details and Download options」をクリックしてメニューを表示させます。(SHA256でチェックを行いたい場合はそちらの方をダウンロードして使用してください)

そこからダウンロードを行うファイルのURLを確認し、wgetでデータをダウンロードしていきます。

必要なデータを今回はrootのホームディレクトリにダウンロードしていきます。

「wget」がインストールされていない場合は、「yum install wget」でインストールしてください。

# cd /root
# wget https://download.nextcloud.com/server/releases/nextcloud-12.0.0.tar.bz2
# wget https://download.nextcloud.com/server/releases/nextcloud-12.0.0.tar.bz2.md5
# wget https://download.nextcloud.com/server/releases/nextcloud-12.0.0.tar.bz2.asc
# wget https://nextcloud.com/nextcloud.asc

データの確認

ダウンロードしてきたmd5とPGPのファイルを使用して、「Nextcloud」のデータ整合性と署名の確認をしていきます。

md5sumで確認

まずはダウンロードしてきたデータに破損がないか、改ざんされていないかの確認をします。

# md5sum -c nextcloud-12.0.0.tar.bz2.md5 < nextcloud-12.0.0.tar.bz2
nextcloud-12.0.0.tar.bz2: OK

「nextcloud-12.0.0.tar.bz2: OK」と表示されれば問題ありません。

日本語で表示される場合は下記のように表示されます。

# md5sum -c nextcloud-12.0.0.tar.bz2.md5 < nextcloud-12.0.0.tar.bz2
nextcloud-12.0.0.tar.bz2: 完了

「nextcloud-12.0.0.tar.bz2: 完了」といったメッセージが表示されれば問題ありません。

GPGで確認

次にGPGでパッケージの署名を確認していきます。

まずは「gpg --inport」で電子署名ファイルをインポートします。

# gpg --import nextcloud.asc
gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key A724937A: public key "Nextcloud Security " imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

日本語で表示される場合は下記のように表示されます。

# gpg --import nextcloud.asc
gpg: ディレクトリ「/root/.gnupg」が作成されました
gpg: 新しいコンフィグレーション・ファイル「/root/.gnupg/gpg.conf」ができまし た
gpg: *警告*: 「/root/.gnupg/gpg.conf」のオプションはこの実行では、まだ有効に なりません
gpg: 鍵リング「/root/.gnupg/secring.gpg」ができました
gpg: 鍵リング「/root/.gnupg/pubring.gpg」ができました
gpg: /root/.gnupg/trustdb.gpg: 信用データベースができました
gpg: 鍵A724937A: 公開鍵"Nextcloud Security "をインポ ートしました
gpg:         処理数の合計: 1
gpg:           インポート: 1  (RSA: 1)

つぎに「pgp --verify」を使って確認を行います。

# gpg --verify nextcloud-12.0.0.tar.bz2.asc nextcloud-12.0.0.tar.bz2
gpg: Signature made Mon 22 May 2017 05:33:42 PM JST using RSA key ID A724937A
gpg: Good signature from "Nextcloud Security "
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 2880 6A87 8AE4 23A2 8372  792E D758 99B9 A724 937A

「Good signature from "Nextcloud Security "」と表示されれば問題ありません。

日本語で表示される場合は下記のように表示されます。

# gpg --verify nextcloud-12.0.0.tar.bz2.asc nextcloud-12.0.0.tar.bz2
gpg: 2017年05月22日 05時33分42秒 JSTにRSA鍵ID A724937Aで施された署名
gpg: "Nextcloud Security "からの正しい署名
gpg: *警告*: この鍵は信用できる署名で証明されていません!
gpg:       この署名が所有者のものかどうかの検証手段がありません。
主鍵のフィンガー・プリント: 2880 6A87 8AE4 23A2 8372  792E D758 99B9 A724 937A

「gpg: "Nextcloud Security "からの正しい署名」といったメッセージが表示されれば問題ありません。

解凍と設置

ダウンロードしてきたファイルに問題が無ければ、データを解凍をしてwebサーバで公開させるためのディレクトリにデータを設置していきます。

解凍

今回は「tar.bz2」形式のアーカイブをダウンロードしてきたので、解凍するために「bzip2」をインストールしてから解凍を行っていきます。

# yum install bzip2
# tar -xvjf nextcloud-12.0.0.tar.bz2

設置

今回は「/var/www/nextcloud」に解凍した「Nextcloud」を設置して、そこをwebで公開させていきます。

データを設置しましたら、所有者を「nginx」に変更しておきます。

# cp -Rp nextcloud /var/www/
# chown -R nginx:nginx /var/www/nextcloud/

nginx設定

「Nextcloud」への接続は暗号化させたほうがセキュリティ上望ましいので、SSL証明書を取得しSSL対応もあわせて行っていきます。

SSL証明書取得

ドメインを取得していてインターネット上に公開する場合、SSL証明書は無料であれば「Let's Encrypt」で取得することが出来ます。

※取得方法はこちらのページで説明しています。

ローカルネット上で使用するのであれば「自己証明」したオレオレ証明書とかを使用するのがお手軽だと思います。

※取得方法はこちらのページで説明しています。(apacheでの設定手順を解説していますが、証明書の作成はnginxでも変わりありませんので、証明書の作成ディレクトリを適当に読み替えて作業を行ってください)

設定ファイル作成

SSLの証明書取得が完了しましたら、「Nextcloud」用の設定ファイルを作成しています。

公式ページのドキュメントに設定例があるので、それをもとに設定ファイルを作成していきます。

https://docs.nextcloud.com/server/11/admin_manual/installation/nginx_nextcloud_9x.html

# vi /etc/nginx/conf.d/nextcloud.conf

設定内容

設定ファイル内の下記項目については、環境毎に異なるので適宜設定をしてください。

設定項目 内容
server_name サーバ名 (httpとhttpsの2か所あります)
ssl_certificate SSL証明書のフルパス
ssl_certificate_key SSL証明書の秘密鍵フルパス
root Nextcloudの設置場所(ドキュメントルート)
upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php5-fpm.sock;
}

server {
    listen 80;
    server_name サーバ名;
    # enforce https
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name サーバ名;

    ssl_certificate SSL証明書;
    ssl_certificate_key SSL証明書の秘密鍵;

    # 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;";
    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 /var/www/nextcloud; (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;

    # Disable gzip to avoid the removal of the ETag header
    gzip off;

    # 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/.+|core/templates/40[34])\.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=7200";
        # 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;";
        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」を再起動します。

# systemctl restart nginx

nextCloud初期設定

「nginx」の設定が完了しましたら、次は「Nextcloud」の初期設定を行っていきます。

webブラウザで「https://サーバ名」にアクセスすることで、初期設定画面にアクセスすることができるので、管理者アカウントの設定やデータベースへの接続設定をなどの初期設定を行っていきます。

管理者アカウントの作成

「Nextcloud」の管理アカウントを設定するので、設定したい「ユーザ名」と「パスワード」を入力してください。

ストレージとデータベース設定

「ストレージとデータベース」をクリックすると設定欄が表示されるので下記項目を設定してください。

設定項目 内容
データフォルダ Nextcloudディレクトリ/data
本手順の場合は /var/www/nextcloud/data
データベースの指定 MySQL/MariaDBを選択
データベースのユーザ名 Nextcloud用データベース管理ユーザ名
本手順の場合は nextadmin
データベースのパスワード Nextcloud用データベース管理ユーザのパスワード
本手順の場合は nextpass
データベース名 Nextcloud用に作成したデータベース名
本手順の場合は nextcloud
データベースサーバ localhost(変更なし)

各項目の設定が完了しましたら「セットアップを完了します」をクリックします。

セットアップが完了しましたら、下記のような画面が表示されます。

APCu設定(メモリキャッシュ設定)

初期設定が完了しましたら、「Nextcloud」の設定ファイルである「config.php」ファイルにAPCuの設定を行っていきます。

# cd /var/www/nextcloud/config
# cp -p config.php config.php_yyyymmdd
# vi config.php

設定内容

「$CONFIG = array」部分の一番最後にでも下記の設定を追加してください。

'memcache.local' => '\OC\Memcache\APCu',
設定例

一部設定を伏せていますが、こんな感じで設定を追加します。

<?php
$CONFIG = array (
  'instanceid' => 'xxxxxxxxx',
  'passwordsalt' => 'xxxxxxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'trusted_domains' =>
  array (
    0 => 'conoha.server-memo.net',
  ),
  'datadirectory' => '/var/www/nextcloud/data',
  'overwrite.cli.url' => 'https://conoha.server-memo.net',
  'dbtype' => 'mysql',
  'version' => '12.0.0.29',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'nextadmin',
  'dbpassword' => 'nextpass',
  'logtimezone' => 'UTC',
  'installed' => true,
  'memcache.local' => '\OC\Memcache\APCu', <--追加設定
);

Opcache設定

2017.05.26.追記

Opcacheの設定もあわせて行っていきます。

# cd /etc/php.d/
# cp -p 10-opcache.ini 10-opcache.ini_yyyymmdd
# vi 10-opcache.ini

設定内容

編集箇所は下記の3箇所になります。

変更前
;opcache.enable_cli=0
opcache.max_accelerated_files=4000
;opcache.revalidate_freq=2
変更後
opcache.enable_cli=1
opcache.max_accelerated_files=10000
opcache.revalidate_freq=1

設定反映

設定を反映させるために「nginx」と「php-fpm」の再起動を行います。

# systemctl restart nginx
# systemctl restart php-fpm

以上で「Nextcloud」の環境構築作業は完了となりますので、webブラウザで「https://サーバ名」に再度アクセスしてみてください。

おつかれさまでした。

コメント

タイトルとURLをコピーしました