CentOS7でLNMP環境にNextcloudをインストールする手順

前提として、すでにLNMP環境がインストールされていることとします。インストール方法については、ここでは詳述しません。以前のブログ記事を参照してください。

1:Nextcloudファイルのダウンロード

https://nextcloud.com/install/

2:解凍

unzip latest.zip -d /home/

3:Nginx設定ファイルの設定

/usr/local/nginx/conf/vhostディレクトリに新しいファイル:nextcloud.confを作成し、以下の内容を記入してください。ルートディレクトリ、SSLアドレス、ドメイン情報は適宜変更してください。

ここで言うvhostとは、/usr/local/nginx/conf/nginx.conf内のinclude vhost/*.conf;を指します。実際の環境に合わせて修正してください。

upstream php-handler {
    server 127.0.0.1:9000;
}

server {
    listen 80;
    listen [::]:80;
    server_name 192.168.3.131;
    # httpsを強制
    return 301 https://$server_name:443$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name 192.168.3.131;

    # MozillaのSSL/TLS設定ガイドラインを使用
    # https://mozilla.github.io/server-side-tls/ssl-config-generator/
    # 注意: 以下の一部設定は冗長な場合があります
    ssl_certificate /usr/local/nginx/cert/nextcloud.crt;
    ssl_certificate_key /usr/local/nginx/cert/nextcloud.key;

    # セキュリティ関連ヘッダーの追加
    # Strict-Transport-Securityヘッダーを有効にする前に、このトピックをよく読んでください
    #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
    #
    # 警告: preloadオプションを有効にする前に、https://hstspreload.org/での結果を確認してください
    # このオプションはドメインをハードコードされたリストに追加し、そのリストから削除するには
    # 数ヶ月かかる場合があります
    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;
    add_header Referrer-Policy no-referrer;
    add_header X-Frame-Options 'SAMEORIGIN';
    add_header Strict-Transport-Security 'max-age=15552000';
    # X-Powered-Byを削除(情報漏洩の防止)
    fastcgi_hide_header X-Powered-By;

    # インストールのルートパス
    root /home/nextcloud;

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

    # 次の2つのルールはuser_webfingerアプリにのみ必要です
    # このアプリを使用する予定がある場合はコメントを外してください
    #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

    # 次のルールはSocialアプリにのみ必要です
    # このアプリを使用する予定がある場合はコメントを外してください
    #rewrite ^/.well-known/webfinger /public.php?service=webfinger last;

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

    # 最大アップロードサイズの設定
    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    # gzipを有効にするがETagヘッダーは削除しない
    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;

    # サーバーがngx_pagespeedモジュールでビルドされている場合はコメントを外してください
    # このモジュールは現在サポートされていません
    #pagespeed off;

    location / {
        rewrite ^ /index.php;
    }

    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\/.+|oc[ms]-provider\/.+)\.php(?:$|\/) {
        fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;
        set $path_info $fastcgi_path_info;
        try_files $fastcgi_script_name =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param HTTPS on;
        # セキュリティヘッダーを二重に送信しないようにする
        fastcgi_param modHeadersAvailable true;
        # 美しいURLを有効にする
        fastcgi_param front_controller_active true;
        fastcgi_pass php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) {
        try_files $uri/ =404;
        index index.php;
    }

    # js、css、mapファイルのキャッシュ制御ヘッダーを追加
    # このブロックはPHPブロックの下にあることを確認してください
    location ~ \.(?:css|js|woff2?|svg|gif|map)$ {
        try_files $uri /index.php$request_uri;
        add_header Cache-Control "public, max-age=15778463";
        # セキュリティ関連ヘッダーを追加(上記のものと重複します)
        # Strict-Transport-Securityヘッダーを有効にする前に、このトピックをよく読んでください
        #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
        #
        # 警告: preloadオプションを有効にする前に、https://hstspreload.org/での結果を確認してください
        # このオプションはドメインをハードコードされたリストに追加し、そのリストから削除するには
        # 数ヶ月かかる場合があります
        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;
        add_header Referrer-Policy no-referrer;

        # オプション: アセットへのアクセスをログに記録しない
        access_log off;
    }

    location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ {
        try_files $uri /index.php$request_uri;
        # オプション: 他のアセットへのアクセスをログに記録しない
        access_log off;
    }
}

4:SSLの設定

ここでの証明書パスは、上記の設定フォルダーのパスに対応します。

[root@localhost src]# openssl req -new -x509 -days 365 -nodes -out /usr/local/nginx/cert/nextcloud.crt -keyout /usr/local/nginx/cert/nextcloud.key
Generating a 2048 bit RSA private key
.........+++
........................+++
writing new private key to '/usr/local/nginx/cert/nextcloud.key'
-----
証明書リクエストに組み込む情報の入力を求められます。
入力するのは識別名(DN)と呼ばれるものです。
多くのフィールドがありますが、一部は空白のままにすることもできます。
一部のフィールドにはデフォルト値があります。
'.' を入力すると、そのフィールドは空白のままになります。
-----
Country Name (2 letter code) [XX]:china
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shanghai
Locality Name (eg, city) [Default City]:minhang
Organization Name (eg, company) [Default Company Ltd]:jbs
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:admin
Email Address []:admin@admin.com

5:データベースの作成

[root@localhost apps]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1054
Server version: 10.3.35-MariaDB-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]><strong> create user 'nextcloud'@'%' identified by 'password123'</strong><strong>;</strong>
Query OK, 0 rows affected (0.015 sec)

MariaDB [(none)]> <strong>grant all privileges on nextcloud.* to 'nextcloud'@'%'</strong><strong> with grant option;</strong>
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]><strong> flush privileges;</strong>
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]><strong> exit</strong>
Bye

6:ウェブサイトへのアクセスと設定

プロンプトに従って入力し、設定が完了すればホームページにアクセスできます。

http://192.168.3.131

タグ: CentOS LNMP Nextcloud nginx SSL

5月18日 07:36 投稿