zuntan02のはてなブログ

備忘録的なものです。時々職場の技術者ブログにも転記してますが、メインはこちらで。

【AWS】Nextcloud環境をAmazonLinuxに構築

■基礎情報

Nextcloud
Dropbox的なクラウドストレージを作成できるツール。以前デファクトスタンダードだったownCloudから派生したもの。
Webブラウザ経由でのアクセスのほか、Windows/OSX/Android/iPhone用にクライアントアプリが公開されている。
www.webprofessional.jp

【今回の作業概要】

AWS EC2のAmazonLinuxにNextcloudをインストール

【インストール手順】

AmazonLinuxに以下の手順でインストールしていきます。

MySQL
# インストール
yum install -y mysql56-server.x86_64 mysql56-devel.x86_64 mysql56.x86_64

# 起動
/etc/init.d/mysqld start

# 初期設定
/usr/bin/mysql_secure_installation

# Set root password? [Y/n]
New password: <MySQLのrootパスワード>
あとは初期値のままで抜ける。

# 自動起動
chkconfig mysqld on
chkconfig --list mysqld
Nginx
yum install nginx

# 自動起動確認
chkconfig nginx on
chkconfig --list nginx

SSLが推奨されているので、SSL証明書を用意しておきます。

php
# 必要となるモジュールをまとめてインストールする
yum install memcached memcached-devel
yum install php56 php56-devel php56-fpm php56-gd php56-mbstring php56-mcrypt php56-mysqlnd php56-pdo php56-pecl-memcached php-pecl-apcu php56-imagick php56-xml

# 設定ファイルで timezone 設定
cp -p /etc/php.ini /etc/php.ini.org
vi /etc/php.ini
date.timezone = Asia/Tokyo
PHP-FPM 実行ユーザ設定
cp -p /etc/php-fpm-5.6.d/www.conf /etc/php-fpm-5.6.d/www.conf.org
vi /etc/php-fpm-5.6.d/www.conf

※diff -u での比較結果
#  ; Note: The user is mandatory. If the group is not set, the default user's group
#  ;       will be used.
#  ; 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
# 
#  ; The address on which to accept FastCGI requests.
#  ; Valid syntaxes are:


# PHP-FPM 自動起動設定
chkconfig php-fpm on
chkconfig --list php-fpm

# 起動・停止テスト
service php-fpm start
/etc/init.d/nginx restart
service php-fpm stop

Nextcloud インストール

# MySQLでNextcloud用DB作成
mysql -uroot -p -A
CREATE DATABASE nextcloud;
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost' IDENTIFIED BY 'hogehoge';
FLUSH PRIVILEGES;
\q

# ドキュメントルート作成
mkdir -p /var/www/nc

# NextCloud11パッケージの取得と展開
cd /usr/local/src
wget https://download.nextcloud.com/server/releases/nextcloud-11.0.0.zip
unzip nextcloud-11.0.0.zip

# ドキュメントルートへの配置
cp -r nextcloud /var/www/nc
cd /var/www/nc
chown -R nginx:nginx ./nextcloud/
nginxの設定

# 参照
# https://docs.nextcloud.com/server/11/admin_manual/installation/nginx_nextcloud_9x.html
上記を参照に、ssl周りのスコアを上げる設定を追記しています。

/etc/nginx/conf.d/hoge.fuga.conf

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

server {
    listen 80;
    server_name hoge.fuga;
    # enforce https
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name hoge.fuga;

    ssl_certificate /etc/nginx/ssl/hoge.fuga.2016.pem;
    ssl_certificate_key /etc/nginx/ssl/hoge.fuga.2016.key;

    # remove vulnerable versions(SSLv2/SSLv3)
    ssl_protocols TLSv1.2;

    # Cipher Suites for LogJamAttack
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
    ssl_prefer_server_ciphers on;


    # 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-Frame-Options "SAMEORIGIN";
    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/nc/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;

    error_page 403 /core/templates/403.php;
    error_page 404 /core/templates/404.php;

    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(?:$|/) {
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        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-Frame-Options "SAMEORIGIN";
        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;
    }

service nginx configtest
service nginx restart
service php-fpm restart

Web経由で初期設定開始

https://hoge.fuga/

f:id:zuntan02:20170120190042p:plain


初期設定以降で解決した問題は以下の通り。

owncloudにログインしようとするとリダイレクトループでログインできない

http://entropiajp.hatenablog.com/entry/2014/02/25/180000
セッションファイルの所有者を確認します。
PHPのセッションファイルはデフォルトで/var/lib/php/session下に保存されるので、所有者をnginxの実行ユーザに変更する。
chown -R nginx:nginx /var/lib/php/5.6/session


セキュリティ&セットアップ警告対応

■管理画面で以下のエラーあり
# 警告対応その1
# PHPのシステム環境変数が正しく設定されていないようです。getenv("PATH") コマンドで
# テストして何も値を返さないことを確認してください。
# PHP設定の注意事項と php-fpmを利用する場合のサーバー向け設定を
# インストールドキュメントで確認してください。
# https://docs.nextcloud.com/server/11/admin_manual/installation/source_installation.html#php-fpm-tips-label

vi /etc/php-fpm.d/www.conf
次の5行のコメントアウトを外します。
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp


# 警告対応その2
# メモリキャッシュが設定されていません。パフォーマンスを向上するために、可能であれば memcache を設定してください。
# より詳しい情報については、を参照してください。
# https://docs.nextcloud.com/server/11/admin_manual/configuration_server/caching_configuration.html

# memcached起動

chkconfig memcached on
service memcached start
ps ax | grep memcached

vi /var/www/nc/nextcloud/config/config.php
=========
  'memcache.local' => '\OC\Memcache\APCu',
  'memcache.distributed' => '\OC\Memcache\Memcached',
  'memcached_servers' => array(
       array('localhost', 11211),
       ),
=========

→管理画面でエラーが出なくなった(すべてのチェックに合格しました。)ことを確認。

以上