Nextcloud

Обновлено 14 апреля 2021

Nextcloud

Введение

Nextcloud - веб-приложение для синхронизации данных, общего доступа к файлам и удалённого хранения документов в "облаке".

Подготовка LXC контейнера

Мы рекомендуем выполнить установку Nextcloud в отдельном контейнере, настроив его согласно инструкции.

Установка и настройка PostgreSQL

Установите и настройте PostgreSQL согласно руководству. Вместо базы данных dbtest из примера создайте базу nextcloud, а вместо пользователя test создайте nextcloud.

Установка и настройка Nginx

Установите и настройте веб-сервер Nginx и выполните настройку PHP-FPM согласно руководству.

Установка Nextcloud

Далее описан процесс установки Nextcloud из исходного кода. Последующие обновления будут выполняться из Web-интерфейса или командной строки самого сервиса.

Загрузка и распаковка Nextcloud

Для распаковки архива Nextcloud вам понадобится архиватор unzip. Если необходимый пакет отсутствует в системе, установите его:

emerge -a app-arch/unzip

Скачайте и распакуйте сервис, создав необходимые пути:

mkdir -p /var/calculate/www/nextcloud/{upload,save}

cd /var/calculate/www/nextcloud

wget https://download.nextcloud.com/server/releases/latest.zip

unzip latest.zip

mv nextcloud htdocs

rm latest.zip

Назначьте необходимые права:

chown -R nginx. .

Настройка Nginx для Nextcloud

Настройте Nginx обслуживания для доменного имени cloud.example.org:

/etc/nginx/sites-enabled/cloud.example.org.conf
upstream php-handler {
    server unix:/run/php-fpm.socket;
}
server {
    listen 80;
    server_name cloud.example.org;
    # Path to the root of your installation
    root /var/calculate/www/nextcloud/htdocs/;
    # Logs
    access_log /var/log/nginx/cloud.example.org.access.log main;
    error_log /var/log/nginx/cloud.example.org.error.log;
    # Max upload size
    client_max_body_size 10G;
    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;

    # WARNING: Only add the preload option once you read about
    # the consequences in https://hstspreload.org/. This option
    # will add the domain to a hardcoded list that is shipped
    # in all major browsers and getting removed from this list
    # could take several months.
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
    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;

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

    # Для получения сертификата ssl
    location ~ /.well-known {
               allow all;
    }

    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;
    }

    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\/.+|oc[ms]-provider\/.+|.+\/richdocumentscode\/proxy)\.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;
        # Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        # Enable pretty urls
        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|woff2?|svg|gif)$ {
        try_files $uri /index.php$uri$is_args$args;
        add_header Cache-Control "public, max-age=15778463";
        # WARNING: Only add the preload option once you read about
        # the consequences in https://hstspreload.org/. This option
        # will add the domain to a hardcoded list that is shipped
        # in all major browsers and getting removed from this list
        # could take several months.
        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;
    }
}

Настройка PHP-FPM

Выполните настройку переменных окружения PHP-FPM:

/etc/php/fpm-php7.4/fpm.d/www.conf
; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

Выполните настройку OPcache PHP-FPM:

/etc/php/fpm-php7.4/php.ini
[PHP]
; Maximum amount of memory a script may consume
; http://php.net/memory-limit
memory_limit = 512M

[opcache]
; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=1

; How often (in seconds) to check file timestamps for changes to the shared
; memory storage allocation. ("1" means validate once per second, but only
; once per request. "0" means always validate)
opcache.revalidate_freq=1

Презапустите Nginx и PHP-FPM, чтобы изменения вступили в силу:

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
/etc/init.d/nginx reload
nginx | * Checking nginx configuration ...                                    [ ok ]
nginx | * Refreshing nginx configuration ...                                  [ ok ]
/etc/init.d/php-fpm restart
php-fpm | * Stopping PHP FastCGI Process Manager ...                          [ ok ]
php-fpm | * Testing PHP FastCGI Process Manager config ...                    [ ok ]
php-fpm | * Starting PHP FastCGI Process Manager ...                          [ ok ]

Настройка Nextcloud

Добавьте имя ресурса cloud.example.org, работающего на IP-адресе 192.168.0.1, в DNS-сервер локальной сети или отредактируйте строку:

/etc/hosts
192.168.0.1 cloud.example.org

Откройте в браузере сайт http://cloud.example.org и выполните заключительную настройку Nextcloud.

Настройка HTTPS

Получение сертификата Let's Encrypt

Получите сертификат домена cloud.example.org для Nginx согласно руководству.

Настройка поддержки HTTPS в Nginx

Настройте Nginx для поддержки HTTPS согласно руководству.

Настройка HTTPS для Nextcloud

Настройте Nginx-обслуживание для доменного имени cloud.example.org:

/etc/nginx/sites-enabled/cloud.example.org.conf
upstream php-handler {
    server unix:/run/php-fpm.socket;
}

server {
    listen 80;
    server_name cloud.example.org;
    rewrite ^ https://$server_name$request_uri? permanent;
}

server {
    listen 443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/cloud.example.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cloud.example.org/privkey.pem;
    include ssl.conf;
    server_name cloud.example.org;
    # Path to the root of your installation
    root /var/calculate/www/nextcloud/htdocs/;
    # Logs
    access_log /var/log/nginx/cloud.example.org.access.log main;
    error_log /var/log/nginx/cloud.example.org.error.log;
    # Max upload size
    client_max_body_size 10G;
    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;

    # WARNING: Only add the preload option once you read about
    # the consequences in https://hstspreload.org/. This option
    # will add the domain to a hardcoded list that is shipped
    # in all major browsers and getting removed from this list
    # could take several months.
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
    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;

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

    # Для получения сертификата ssl
    location ~ /.well-known {
               allow all;
    }

    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;
    }

    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\/.+|oc[ms]-provider\/.+|.+\/richdocumentscode\/proxy)\.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;
        # Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        # Enable pretty urls
        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|woff2?|svg|gif)$ {
        try_files $uri /index.php$uri$is_args$args;
        add_header Cache-Control "public, max-age=15778463";
        # WARNING: Only add the preload option once you read about
        # the consequences in https://hstspreload.org/. This option
        # will add the domain to a hardcoded list that is shipped
        # in all major browsers and getting removed from this list
        # could take several months.
        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;
    }
}

Настройка кэширования

Redis - программное обеспечение, реализующее сервис кэширования данных в оперативной памяти на основе хеш-таблицы. Для его подключения установите необходимые пакеты:

emerge dev-db/redis dev-php/pecl-redis virtual/pkgconfig

Добавьте Redis в автозагрузку, запустите его и перезапустите PHP-FPM:

rc-update add redis
 * service redis added to runlevel default
/etc/init.d/redis start
php-fpm | * Starting redis ...                                                [ ok ]
/etc/init.d/php-fpm restart
php-fpm | * Stopping PHP FastCGI Process Manager ...                          [ ok ]
php-fpm | * Testing PHP FastCGI Process Manager config ...                    [ ok ]
php-fpm | * Starting PHP FastCGI Process Manager ...                          [ ok ]

Добавьте настройки Redis в Nextcloud:

/var/calculate/www/nextcloud/htdocs/config/config.php
<?php
$CONFIG = array (
  'instanceid' => 'secret',
  'passwordsalt' => 'secret',
  'secret' => 'secret',
  'trusted_domains' =>
  array (
    0 => 'cloud.example.org',
  ),
  'datadirectory' => '/var/calculate/www/nextcloud/htdocs/data',
  'overwrite.cli.url' => 'https://cloud.example.org',
  'dbtype' => 'pgsql',
  'version' => '13.0.2',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'nextcloud',
  'dbpassword' => 'secret',
  'installed' => true,
  'maintenance' => false,
  'memcache.local' => '\\OC\\Memcache\\Redis',
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'redis' => array(
    'host' => 'localhost',
    'port' => 6379,
  ),
);

По умолчанию Redis использует 64Мб памяти. Для работы Nextcloud этого может быть недостаточно, что может вызвать ошибку OOM command not allowed when used memory > 'maxmemory'. Настройте максимальное количество памяти, используемое Redis:

/etc/redis.conf

...
maxmemory 256mb

Заключение

Nextcloud настроен и готов к работе! Но это только начало пути. Впереди вас ждет открытие удивительных возможностей вашего персонального облака Nextcloud. Частично вам в этом поможет статья Работа с Nextcloud.

Заметка

Подписывайтесь на нашу группу @nextcloud@calculate.social в Федеративной сети Mastodon