8.ubuntuにNextCloudをインストール

スポンサーリンク

本日はNext Cloudをインストールします。似たサービスにDropboxがありますが、Next Cloudはフリーでオープンソースである。
まずはNext Cloud用にnginxの設定ファイルを作成します。ファイルの内容はサンプルファイルを使い回すことにします。サンプルファイルのドメイン名を変更し、ワードプレスが動作したPHPの記述を追記したものとなります。

sudo vi /etc/nginx/sites-available/nextcloud.minokamo.tokyo

内容
server {
listen 80;
listen [::]:80;

server_name nextcloud.minokamo.tokyo;

root /var/www/nextcloud.minokamo.tokyo;
index index.php index.html;

location / {
try_files $uri $uri/ =404;
}

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        }
}

設定ファイルの記述でルートとして設定したディレクトリを新規で作成し、そのディレクトリの所有権とパーミッションを変更します。

cd /var/www
ls -l
sudo mkdir nextcloud.minokamo.tokyo
sudo chown -R www-data:www-data nextcloud.minokamo.tokyo
sudo chmod -R 755 nextcloud.minokamo.tokyo

次のコマンドを入力して、先に作成した設定ファイルのシンボリックリンクをsites-enabledというフォルダに作成します。また所有権も変更します。シンボリックリンクとはショートカットみたいなものでnginxをインストールする記事で解説しています。
https://minokamo.tokyo/2020/12/17/3001/

sudo ln -s /etc/nginx/sites-available/nextcloud.minokamo.tokyo /etc/nginx/sites-enabled/nextcloud.minokamo.tokyo
sudo chown -h www-data:www-data /etc/nginx/sites-enabled/nextcloud.minokamo.tokyo

Next Cloud導入前にSSLの設定を先におこなうことにしました。
https://minokamo.tokyo/2020/12/19/3084/

sudo certbot --nginx -d nextcloud.minokamo.tokyo

それではNext Cloudをダウンロードします。わかりやすいようにダウンロードする場所を作業ディレクトリのtmpとしました。ファイルのURLはNext Cloudの公式サイトで確認してきます。対象ファイルを右クリックして「リンクのアドレスをコピー」で取得してきます。

cd /tmp
wget https://download.nextcloud.com/server/releases/nextcloud-20.0.4.zip

ダウンロード完了後に解凍します。ubuntu minimal 20.04にはunzipがインストールされていないのでインストールしておきます。

sudo apt install unzip
unzip nextcloud-20.0.4.zip

解凍すると、nextcloudというディレクトリが作成され、その下の階層にファイルやディレクトリが存在している状態です。nextcloudというディレクトリを除く下の階層のファイルとディレクトリをすべてドキュメントルートとして設定したディレクトリにコピーします。また、コピー先の属性にならないので再度、所有権の変更をします。

sudo cp -r /tmp/nextcloud/. /var/www/nextcloud.minokamo.tokyo
cd /var/www
sudo chown -R www-data:www-data nextcloud.minokamo.tokyo

次はNext Cloudが使用するデータベースを作成します。
https://minokamo.tokyo/2020/12/25/3112/

sudo mariadb
create database nextcloud;
grant all on nextcloud.* to 'user1'@'localhost' identified by 'password';
exit;

再度、nginxの設定ファイルを開きます。
先ほどおこなったSSLの設定内容が記載されています。
ここでやるべきことはNext Cloudが動作する記述をすることです。公式ページに例があるので、それを参考に記述します。今回はNext Cloudがウェブルートにある設定なので上段を参考にします。ドメインの下にディレクトリがあるURLの場合は下段を参考にします。SSLの記述例はすでに記述してあるので、サンプルのSSLの内容を記述しないようにします。

sudo vi /etc/nginx/sites-available/nextcloud.minokamo.tokyo

次のコマンドを入力します。
nginx: [emerg] duplicate location
nginx: [emerg] “try_files” directive is duplicate
のようなエラーが発生したら見直して修正します。

sudo nginx -t

修正できたらnginxを再起動してブラウザで確認します。

sudo systemctl restart nginx

しかし次のエラーが発生しました。

This version of Nextcloud is not compatible with > PHP 7.4.
You are currently running 8.0.0.

現在のPHPのバージョンは8なのは確かです。いろいろ調べてみるとphp8ではnextcloud20が動作しないという海外のサイトを見つけました。nextcloud21がもうすぐリリースされるようですが、その時はphp8もサポートされるとGitHubのページに記載されていました。
phpのバージョンを下げることも考えましたがリリースされるのを待つことにしました。

最後に使用したnginxの設定ファイルの内容

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

server {
listen 80;
listen [::]:80;

server_name nextcloud.minokamo.tokyo;

root /var/www/nextcloud.minokamo.tokyo;
index index.php index.html;

    return 301 https://$server_name$request_uri;

#location / {
#        try_files $uri $uri/ /index.php$request_uri;
}

server {
    listen [::]:443 ssl http2; # managed by Certbot
    listen 443 ssl http2; # managed by Certbot

server_name nextcloud.minokamo.tokyo;

    ssl_certificate /etc/letsencrypt/live/nextcloud.minokamo.tokyo/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/nextcloud.minokamo.tokyo/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    # 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;

    # Pagespeed is not supported by Nextcloud, so if your server is built
    # with the `ngx_pagespeed` module, uncomment this line to disable it.
    #pagespeed off;

    # HTTP response headers borrowed from Nextcloud `.htaccess`
#add_header Strict-Transport-Security "max-age=31536000" always;
    add_header Referrer-Policy                      "no-referrer"   always;
    add_header X-Content-Type-Options               "nosniff"       always;
    add_header X-Download-Options                   "noopen"        always;
    add_header X-Frame-Options                      "SAMEORIGIN"    always;
    add_header X-Permitted-Cross-Domain-Policies    "none"          always;
    add_header X-Robots-Tag                         "none"          always;
    add_header X-XSS-Protection                     "1; mode=block" always;

    # Remove X-Powered-By, which is an information leak
    fastcgi_hide_header X-Powered-By;

    # Path to the root of your installation

root /var/www/nextcloud.minokamo.tokyo;

    # Specify how to handle directories -- specifying `/index.php$request_uri`
    # here as the fallback means that Nginx always exhibits the desired behaviour
    # when a client requests a path that corresponds to a directory that exists
    # on the server. In particular, if that directory contains an index.php file,
    # that file is correctly served; if it doesn't, then the request is passed to
    # the front-end controller. This consistent behaviour means that we don't need
    # to specify custom rules for certain paths (e.g. images and other assets,
    # `/updater`, `/ocm-provider`, `/ocs-provider`), and thus
    # `try_files $uri $uri/ /index.php$request_uri`
    # always provides the desired behaviour.
    index index.php index.html /index.php$request_uri;

    # Default Cache-Control policy
    expires 1m;

    # Rule borrowed from `.htaccess` to handle Microsoft DAV clients
    location = / {
        if ( $http_user_agent ~ ^DavClnt ) {
            return 302 /remote.php/webdav/$is_args$args;
        }
 }
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # Make a regex exception for `/.well-known` so that clients can still
    # access it despite the existence of the regex rule
    # `location ~ /(\.|autotest|...)` which would otherwise handle requests
    # for `/.well-known`.
    location ^~ /.well-known {
        # The following 6 rules are borrowed from `.htaccess`

        location = /.well-known/carddav     { return 301 /remote.php/dav/; }
        location = /.well-known/caldav      { return 301 /remote.php/dav/; }
        # Anything else is dynamically handled by Nextcloud
        location ^~ /.well-known            { return 301 /index.php$uri; }

        try_files $uri $uri/ =404;
    }
    # Rules borrowed from `.htaccess` to hide certain paths from clients
    location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/)  { return 404; }
    location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console)              { return 404; }

    # Ensure this block, which passes PHP files to the PHP process, is above the blocks
    # which handle static assets (as seen below). If this block is not declared first,
    # then Nginx will encounter an infinite rewriting loop when it prepends `/index.php`
    # to the URI, resulting in a HTTP 500 error response.
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
       }
    location ~ \.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_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_param modHeadersAvailable true;         # Avoid sending the security headers twice
        fastcgi_param front_controller_active true;     # Enable pretty urls
        fastcgi_pass php-handler;

        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }
    location ~ \.(?:css|js|svg|gif)$ {
        try_files $uri /index.php$request_uri;
        expires 6M;         # Cache-Control policy borrowed from `.htaccess`
        access_log off;     # Optional: Don't log access to assets
    }

    location ~ \.woff2?$ {
        try_files $uri /index.php$request_uri;
        expires 7d;         # Cache-Control policy borrowed from `.htaccess`
        access_log off;     # Optional: Don't log access to assets
    }
}

コメント

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