Ubuntu 16.04 架設 Nextcloud

需求環境:

PHP 7.0(含)以上

NginX或Apache

MySQL或MariaDB

SSL

本文以 NginX + PHP 7.1 + MariaDB 為架設環境

 

環境架設

參考下列教學,將環境架設好。

NginX + PHP 7.1  教學網址:https://vpn.tw/?p=119

Mariadb + phpMyAdmin 教學網址:https://vpn.tw/?p=107

NginX + Lets Encrypt(SSL) 教學網址:https://vpn.tw/?p=116

 

php環境調整

1.安裝Nextcloud需求的 php7.x-zip的延伸套件。

apt install php7.1-zip

2.編輯php設定檔。

vi /etc/php/7.1/fpm/pool.d/www.conf

3.找到下面這行。

listen = /run/php/php7.1-fpm.sock

4.改成下面。

listen = 127.0.0.1:9000

5.找到下面幾行,將前面的 『 ; 』去掉,後存檔離開。

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

6.重新啟動php。

/etc/init.d/php7.1-fpm restart

MARIADB建立資料庫

1.登入 MySQL。

mysql -u root -p

2.建立 MySQL 使用者。

CREATE DATABASE nextcloud;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON nextcloud.* TO 'nextcloud'@'localhost' IDENTIFIED BY 'nextcloudpwd';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON nextcloud.* TO 'nextcloud'@'localhost.localdomain' IDENTIFIED BY 'nextcloudpwd';
FLUSH PRIVILEGES;

 

NginX設定檔調整

1.備份設定檔

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

2.編輯設定檔

vi /etc/nginx/sites-available/default

3.官方設定檔參考,請將server_name及ssl_certificate、ssl_certificate_key換成自己的

upstream php-handler {
    server 127.0.0.1:9000;
}

server {
    listen 80;
    server_name cloud.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name www.example.com;

    ssl_certificate /etc/ssl/nginx/cloud.example.com.crt;
    ssl_certificate_key /etc/ssl/nginx/cloud.example.com.key;

    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;

    root /var/www/html;

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

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

    location /.well-known/acme-challenge { }

    location ^~ /nextcloud {

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

        # 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 /nextcloud {
            rewrite ^ /nextcloud/index.php$uri;
        }

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

        location ~ ^/nextcloud/(?: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 ~ ^/nextcloud/(?: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 /nextcloud/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 /nextcloud/index.php$uri$is_args$args;
            # Optional: Don't log access to other assets
            access_log off;
        }
    }
}

4.設定完成檢查設定檔有無錯誤

nginx -t

5.重新啟動nginx

/etc/init.d/nginx restart

 

架設Nextcloud

1.進入主機的webroot-path,一般為 /var/www/html。

cd /var/www/html

2.下載Nextcloud ZIP檔並解壓縮,目前最新版本為12版,但有BUG架設不起來,本文以11版為主。

wget https://download.nextcloud.com/server/releases/nextcloud-11.0.4.zip
unzip nextcloud-11.0.4.zip

3.給予nextcloud權限

chown -R www-data:www-data /var/www/html/nextcloud

4.編輯nextcloud的config.php檔,優化記憶體快取。

vi /var/www/html/nextcloud/config/config.php

5.在陣列裡加入下面,這行後存檔離開。

'memcache.local' => '\OC\Memcache\APCu',

6.開啟網頁輸入下列網址

https://你的網址/nextcloud

7.設定管理者帳號、密碼

使用MariaDB

資料庫 nextcloud

帳號 nextcloud

密碼 nextcloudpwd

位址 localhost:3306