Nextcloud 部署中的一些问题及解决

Nextcloud 是个优秀的私有云方案,但用起来总有各种各样的麻烦。在此记录一下解决的方法和未解决的问题吧!

环境:

  • Debian 11
  • MySQL 8.0.27
  • PHP 7.4.28
  • 环境的搭建基于lnmp脚本
一、Nginx 的配置

Nginx 的配置参考官方的配置文件,将关键位置更改一下即可。

upstream php-handler {
    #server 127.0.0.1:9000;
    server unix:/var/run/php/php7.4-fpm.sock; #此处可通过命令 find . -name *.sock 查找,在lnmp脚本安装下的位置为 /tmp/php-cgi.sock
}

server {
    listen 80;
    listen [::]:80;
    server_name cloud.example.com; #此处修改为你的域名

    # Enforce HTTPS
    return 301 https://$server_name$request_uri;
}

server {
    listen 443      ssl http2;
    listen [::]:443 ssl http2;
    server_name cloud.example.com; #此处修改为你的域名

    # Use Mozilla's guidelines for SSL/TLS settings
    # https://mozilla.github.io/server-side-tls/ssl-config-generator/
    ssl_certificate     /etc/ssl/nginx/cloud.example.com.crt; #此处修改为你的域名证书
    ssl_certificate_key /etc/ssl/nginx/cloud.example.com.key; #此处修改为你的域名证书

    # HSTS settings
    # 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=15768000; includeSubDomains; preload;" always;

    # set max upload size and increase upload timeout:
    client_max_body_size 512M;
    client_body_timeout 300s;
    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/wasm 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 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; #此处修改为你的Nextcloud配置文件所在位置

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

    # Rule borrowed from `.htaccess` to handle Microsoft DAV clients
    location = / {
        if ( $http_user_agent ~ ^DavClnt ) {
            return 302 /remote.php/home/wwwroot/lnmp01/domain/Suno/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 rules in this block are an adaptation of the rules
        # in `.htaccess` that concern `/.well-known`.

        location = /.well-known/carddav { return 301 /remote.php/dav/; }
        location = /.well-known/caldav  { return 301 /remote.php/dav/; }

        location /.well-known/acme-challenge    { try_files $uri $uri/ =404; }
        location /.well-known/pki-validation    { try_files $uri $uri/ =404; }

        # Let Nextcloud's API for `/.well-known` URIs handle all other
        # requests by passing them to the front-end controller.
        return 301 /index.php$request_uri;
    }

    # 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(?:$|/) {
        # Required for legacy support
        rewrite ^/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+|.+\/richdocumentscode\/proxy) /index.php$request_uri;

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

        fastcgi_max_temp_file_size 0;
    }

    location ~ \.(?:css|js|svg|gif|png|jpg|ico|wasm|tflite|map)$ {
        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 ~ \.wasm$ {
            default_type application/wasm;
        }
    }

    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
    }

    # Rule borrowed from `.htaccess`
    location /remote {
        return 301 /remote.php$request_uri;
    }

    location / {
        try_files $uri $uri/ /index.php$request_uri;
    }
}
二、php需要安装的模块

需要安装 gmpsodium 模块,可参考 LNMP安装PHP插件命令 此篇记录。

三、配置内存缓存

首先安装 redis 和 apcu 缓存插件

然后在 config.php 文件中增加以下内容

  'memcache.local' => '\\OC\\Memcache\\APCu',
  'memcache.distributed' => '\\OC\\Memcache\\Redis',
  'redis' => 
  array (
    'host' => '127.0.0.1',
    'port' => 6379, #redis默认端口
  ),
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'filesystem_check_changes' => true,
四、自动扫描文件更改

config.php 文件中增加以下内容

'filesystem_check_changes' => true,
五、文件生成缩略图

先安装ffmpeg以及imagemagick模块,ffmpeg的安装可参考 ffmpeg的安装

config.php 文件中增加以下内容

'enabledPreviewProviders' =>
array (
    0 => 'OC\\Preview\\Movie',
    1 => 'OC\\Preview\\PNG',
    2 => 'OC\\Preview\\JPEG',
    3 => 'OC\\Preview\\GIF',
    4 => 'OC\\Preview\\BMP',
    5 => 'OC\\Preview\\XBitmap',
    6 => 'OC\\Preview\\MP3',
    7 => 'OC\\Preview\\MP4',
    8 => 'OC\\Preview\\TXT',
    9 => 'OC\\Preview\\MarkDown',
    10 => 'OC\\Preview\\PDF',
  ),
六、错误”Apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder or the folder …..”

这个错误貌似是防跨目录设置引起的,我发现在我更改 WordPress 的一些设置,譬如 启用/停用插件 后,Nextcloud就出现了这样的错误,重启php-fpm又恢复正常。

在nginx.conf中关闭防跨目录相关内容后恢复正常。

放跨目录的关闭方法可参考 LNMP关闭防跨目录的方法

七、提示 cron 的运行有问题

我的web服务器用户为www,参考官方的文档进行以下调整设置 cron 作业以调用cron.php脚本:

crontab -u www -e

并附加以下内容,路径需自行修改:

*/5  *  *  *  * php -f /Nextcloud路径/cron.php

测试是否添加并安排了Cron作业:

crontab -u www -l

返回内容有:

*/5  *  *  *  * php -f /Nextcloud路径/cron.php

即设置成功。

八、smtp邮件服务器设置错误

如确认填写无误并排除邮件服务端问题后仍无法正常发信,则可能需要修改 config.php 中 'mail_smtpsecure' => 'ssl', 的内容,将 “ssl” 修改为 “tls” 后应该就能正常发信了。

九、PHP 的设置似乎有问题, 无法获取系统环境变量. 使用 getenv(\”PATH\”) 测试时仅返回空结果.

在php-fpm.conf中添加:

env[PATH] = /usr/local/bin:/usr/bin:/bin:/usr/local/php/bin
十、文件完整性检查不通过,.user.ini配置文件的hash不一致

在Nextcloud安装目录新建 .user.ini 文件,并在其中添加如下内容:

mbstring.func_overload=0
always_populate_raw_post_data=-1
default_charset='UTF-8'
output_buffering=0
十一、Opcache的推荐配置
[Zend Opcache]
zend_extension="opcache.so"
opcache.memory_consumption=128
opcache.interned_strings_buffer=8 #官方推荐配置值为8,但使用过程中可能还会建议调高
opcache.max_accelerated_files=10000
opcache.revalidate_freq=1
opcache.fast_shutdown=1
opcache.enable_cli=1
opcache.save_comments=1
十二、Internet Server Error :The server encountered an…….

这个问题似乎是由于APCu的配置引起的,当时我未能解决,这个错误出现之前我重启了lnmp的服务,随后Nextcloud 便出现了这个错误,在我重装后又恢复正常,重启lnmp后也没有再次出现这错误。

有解决方法是在apcu的配置文件或php.ini中添加如下一行并重启php-fpm :

apc.enable_cli = 1

在以后的使用中可能还有其他问题出现,届时我将会对本文继续增补。

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注