标签: nextcloud

  • Nextcloud配置和优化

    Nextcloud配置和优化

    Nextcloud配置和优化


    Nextcloud数据库索引修复

    当Nextcloud数据库丢失索引时,可以通过命令行手动修复。请在Nextcloud安装根目录执行以下命令:

     sudo -u www php occ db:add-missing-indices

    例如,修复丢失的索引“mail_messages_msgid_idx”:

     sudo -u www php occ db:add-missing-indices

    修复后可以显著提高查询速度。

    Nextcloud配置文件(config.php)

     <?php
    $CONFIG = array(
      'objectstore' => array(
        'class' => '\\OC\\Files\\ObjectStore\\S3',
        'arguments' => array(
          'bucket' => 'nextcloud-*******', // 存储桶名称(空间名称)
          'autocreate' => false,
          'key'  => 'AKID***********', // 替换为用户的 SecretId
          'secret' => '**********', // 替换为用户的 SecretKey
          'hostname' => 'cos.<Region>.myqcloud.com', // 将 <Region> 修改为所属地域,如 ap-shanghai
          'use_ssl' => true,
        ),
      ),
      'instanceid' => 'oc82tpo2f',
      'passwordsalt' => '2GdQCVNsIVYF1KLP3fT3woH69X',
      'secret' => 'VSy0Y+/2wV7g4kvbiKSE0dSa2Yp1ajtvvZpgdV14Lsc',
      'trusted_domains' => array(
        0 => 'nextcloud.***.***',
      ), // nextcloud域名
      'datadirectory' => '/www/nextcloud/data', // data路径,请根据实际情况修改
      'dbtype' => 'mysql',
      'version' => '27.1.5.1',
      'overwrite.cli.url' => 'https://nextcloud.***.***/nextcloud',
      'dbname' => 'nextcloud',
      'dbhost' => 'localhost',
      'dbport' => '',
      'dbtableprefix' => 'oc_',
      'mysql.utf8mb4' => true,
      'dbuser' => 'nextcloud', // 数据库用户名
      'dbpassword' => '******', // 数据库密码
      'installed' => true,
      'maintenance' => false,
      'updater.secret' => '\$2y\$10\$0eTb6aJ4u3NAWwlQk2qkUOTlI3.0O6TGLwAQOCyUtH/wmCpC',
      'theme' => '',
      'loglevel' => 2,
      'filelocking.enabled' => true,
      'memcache.local' => '\\OC\\Memcache\\Redis',
      'memcache.locking' => '\\OC\\Memcache\\Redis',
      'redis' => array(
        'host' => '127.0.0.1',
        'port' => 6379,
        'timeout' => 0.0,
        'password' => '', // redis密码
      ),
      'mail_smtpmode' => 'smtp',
      'mail_sendmailmode' => 'smtp',
      'mail_from_address' => '****', // 请根据实际情况修改
      'mail_domain' => 'outlook.com', // 请根据实际情况修改
      'mail_smtpauth' => 1,
      'mail_smtphost' => 'smtp-mail.outlook.com', // 请根据实际情况修改
      'mail_smtpport' => '587', // 请根据实际情况修改
      'mail_smtpname' => '*****', // 请根据实际情况修改
      'mail_smtppassword' => '****', // 请根据实际情况修改
      'enabledPreviewProviders' => [
        'OC\Preview\MP3',
        'OC\Preview\TXT',
        'OC\Preview\MarkDown',
        'OC\Preview\OpenDocument',
        'OC\Preview\Krita',
        'OC\Preview\Imaginary',
      ],
      'preview_imaginary_url' => 'http://<url of imaginary>',
    );

    Nextcloud性能优化

    后台任务

    设置cron.php每5分钟运行一次,cron.php的具体路径在nextcloud安装的根目录下。

     crontab -u www -e
    */5 * * * * php -f /PathToNextcloud/cron.php

    PHP配置优化

    1. 注释掉php.ini中的output_buffering设置:
     ; output_buffering = 4096
    1. 启用OPcache以提高性能:
    opcache.enable=1
    opcache.enable_cli=1
    opcache.interned_strings_buffer=32
    opcache.max_accelerated_files=80000
    opcache.memory_consumption=128
    opcache.save_comments=1
    opcache.revalidate_freq=3
    1. 设置HSTS以提高安全性,在apache的配置文件中加入:
    <IfModule mod_headers.c>
      Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
    </IfModule>

    完整性检查

    如果Nextcloud文件完整性检查发现问题,可以根据文档中的指导进行修复。例如,.user.ini文件缺失可以创建该文件并设置适当的open_basedir,或者在宝塔面板中开启放跨站。

    完整config.php设置

    <?php
    $CONFIG = array(
      'instanceid' => 'xxxxxxxxxxx',
      'passwordsalt' => 'xxxxxxxxxxxxxxxxxx',
      'secret' => 'TNFR/+xxxxxxxxxxxxxxxxxxx/',
      'trusted_domains' => 
      array (
        0 => 'nextcloud.guohao.asia',
      ),
      'datadirectory' => '/www/wwwroot/nextcloud/data',
      'dbtype' => 'mysql',
      'version' => '24.0.5.1',
      'overwrite.cli.url' => 'https://nextcloud.guohao.asia', 
      'dbname' => 'xxxxxxxxxxxx',
      'dbhost' => 'localhost',
      'dbport' => '',
      'dbtableprefix' => 'oc_',
      'mysql.utf8mb4' => true,
      'dbuser' => 'xxxxxxxxxx',
      'dbpassword' => 'xxxxxxxxx',
      'installed' => true,
      'app_install_overwrite' => 
      array (
        0 => 'tencentcloudcosconfig',
        1 => 'files_external_onedrive',
      ),  
      // 缓存设置
      'memcache.local' => 'OC\\Memcache\\Redis',        
      'memcache.distributed' => 'OC\\Memcache\\Memcached',
      'redis' => [        #Redis
        'host' => 'localhost', #Redis 服务器
        'port' => 6379,     #Redis 端口
        'timeout' => 0.0,       #Redis 超时设定
        'password' => 'XXXXXXXXXXX',        #Redis 密码
        'dbindex' => 1,     #Redis 数据库选择
      ],
      'memcached_servers' => array(        #Memcached 服务器
        array('localhost', 11211),
      ),
      'memcached_options' => array(        #Memcached 选项
        \Memcached::OPT_CONNECT_TIMEOUT => 50,
        \Memcached::OPT_RETRY_TIMEOUT =>   50,
        \Memcached::OPT_SEND_TIMEOUT =>    50,
        \Memcached::OPT_RECV_TIMEOUT =>    50,
        \Memcached::OPT_POLL_TIMEOUT =>    50,
        \Memcached::OPT_COMPRESSION =>          true,
        \Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
        \Memcached::OPT_BINARY_PROTOCOL =>      true,
      ),
      'cache_path' => '/www/wwwroot/nextcloud/cache',        #缓存目录
      'cache_chunk_gc_ttl' => 60*60*24,        #缓存删除时间
      // 腾讯云COS  
      'objectstore' => 
      array (
        'class' => '\\OC\\Files\\ObjectStore\\S3',
        'arguments' => 
        array (
          'key' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
          'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
          'bucket' => 'nextcloud-000000000',
          'hostname' => 'cos.ap-beijing.myqcloud.com',
          'use_ssl' => true,
          'autocreate' => false,
        ),
      ),
      // 电话区号
      'default_phone_region' => 'CN',
    );

    解决Nextcloud无法更新应用

    config.php中添加以下内容以启用应用商店:

     'appstoreenabled' => true,
     'appstoreurl' => 'https://www.orcy.net/ncapps/v1/',

    更换为国内源(更新进度较官方略慢)。

    退出维护模式

  • Nextcloud在线升级后报错一例

    Nextcloud在线升级后报错一例

    以下为后台错误提示:

    数据库丢失了一些索引。由于给大的数据表添加索引会耗费一些时间,因此程序没有自动对其进行修复。您可以在 Nextcloud 运行时通过命令行手动执行“occ db:add-missing-indices”命令修复丢失的索引。索引修复后会大大提高相应表的查询速度。 Missing optional index “mail_messages_msgid_idx” in table “mail_messages”.

    解决:

    在Nextcloud安装根目录执行以下命令:

    sudo -u www php occ db:add-missing-indices

    image-20240221115800220

    此后问题解决。

  • 整合NextCloud及腾讯云COS搭建私有云

    整合NextCloud及腾讯云COS搭建私有云

    <?php
    $CONFIG = array(
      'objectstore' => array(
        'class' => '\\OC\\Files\\ObjectStore\\S3',
        'arguments' => array(
          'bucket' => 'nextcloud-*******', // 存储桶名称(空间名称)
          'autocreate' => false,
          'key'  => 'AKID***********', // 替换为用户的 SecretId
          'secret' => '**********', // 替换为用户的 SecretKey
          'hostname' => 'cos.<Region>.myqcloud.com', // 将 <Region> 修改为所属地域,如 ap-shanghai
          'use_ssl' => true,
        ),
      ),
      'instanceid' => 'oc82tpo2f',
      'passwordsalt' => '2GdQCVNsIVYF1KLP3fT3woH69X',
      'secret' => 'VSy0Y+/2wV7g4kvbiKSE0dSa2Yp1ajtvvZpgdV14Lsc',
      'trusted_domains' => array(
        0 => 'nextcloud.***.***',
    	), // nextcloud域名
      'datadirectory' => '/www/nextcloud/data', // data路径,请根据实际情况修改
      'dbtype' => 'mysql',
      'version' => '27.1.5.1',
      'overwrite.cli.url' => 'https://nextcloud.***.***/nextcloud',
      'dbname' => 'nextcloud',
      'dbhost' => 'localhost',
      'dbport' => '',
      'dbtableprefix' => 'oc_',
      'mysql.utf8mb4' => true,
      'dbuser' => 'nextcloud', // 数据库用户名
      'dbpassword' => '******', // 数据库密码
      'installed' => true,
      'maintenance' => false,
      'updater.secret' => '\$2y\$10\$0eTb6aJ4u3NAWwlQk2qkUOTlI3.0O6TGLwAQOCyUtH/wmCpC',
      'theme' => '',
      'loglevel' => 2,
      'filelocking.enabled' => true,
      'memcache.local' => '\\OC\\Memcache\\Redis',
      'memcache.locking' => '\\OC\\Memcache\\Redis',
      'redis' => array(
        'host' => '127.0.0.1',
        'port' => 6379,
        'timeout' => 0.0,
        'password' => '', // redis密码
      ),
      'mail_smtpmode' => 'smtp',
      'mail_sendmailmode' => 'smtp',
      'mail_from_address' => '****', // 请根据实际情况修改
      'mail_domain' => 'outlook.com', // 请根据实际情况修改
      'mail_smtpauth' => 1,
      'mail_smtphost' => 'smtp-mail.outlook.com', // 请根据实际情况修改
      'mail_smtpport' => '587', // 请根据实际情况修改
      'mail_smtpname' => '*****', // 请根据实际情况修改
      'mail_smtppassword' => '****', // 请根据实际情况修改
      'enabledPreviewProviders' => [
        'OC\Preview\MP3',
        'OC\Preview\TXT',
        'OC\Preview\MarkDown',
        'OC\Preview\OpenDocument',
        'OC\Preview\Krita',
        'OC\Preview\Imaginary',
      ],
      'preview_imaginary_url' => 'http://<url of imaginary>',
    );
    
  • NextCloud性能优化及维护

    NextCloud性能优化及维护

    NextCloud性能优化

    后台任务

    image-20220915140526069

    crontab -u www -e

    */5 * * * * php -f /PathToNextcloud/cron.php

    设置cron.php每5分钟运行一次,cron.php的具体路径在nextcloud安装的根目录下。

    cron.php应该被www用户执行。

    另外,宝塔面板用户也可以通过面板上的“计划任务”来执行cron.php,如:

    image-20220915164120009

    PHP configuration option output_buffering must be disabled

    注释掉php.ini中的; output_buffering = 4096

    image-20220915143527113

    缓存配置

    /config/config.php中添加如下代码:

      'memcache.local' => 'OC\\Memcache\\APCu',        #Memcached
    
      'memcache.distributed' => 'OC\\Memcache\\Memcached',
    
      'redis' => [        #Redis
    
          'host' => 'localhost', #Redis 服务器
    
          'port' => 6379,     #Redis 端口
    
          'timeout' => 0.0,       #Redis 超时设定
    
          'password' => '########',       #Redis 密码
    
          'dbindex' => 1,     #Redis 数据库选择
    
      ],
    
      'memcached_servers' => array(        #Memcached 服务器
    
          array('localhost', 11211),
    
      ),
    
      'memcached_options' => array(        #Memcached 选项
    
          \Memcached::OPT_CONNECT_TIMEOUT => 50,
    
          \Memcached::OPT_RETRY_TIMEOUT =>   50,
    
          \Memcached::OPT_SEND_TIMEOUT =>    50,
    
          \Memcached::OPT_RECV_TIMEOUT =>    50,
    
          \Memcached::OPT_POLL_TIMEOUT =>    50,
    
          \Memcached::OPT_COMPRESSION =>          true,
    
          \Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
    
          \Memcached::OPT_BINARY_PROTOCOL =>      true,
    
      ),
    
      'cache_path' => '/www/wwwroot/nextcloud/cache',        #缓存目录
    
      'cache_chunk_gc_ttl' => 60*60*24,        #缓存删除时间

    代码中有些内容需要根据具体情况进行修改替换。建议添加在最后,Nextcloud搭配了COS之类的,建议放在COS配置之前。

    PHP 的 OPcache 模块未载入。推荐开启获得更好的性能。 PHP OPcache 模块没有正确配置。

    安装OPcache,在php.ini中加入如下内容:

    opcache.enable=1
    opcache.enable_cli=1
    opcache.interned_strings_buffer=32
    opcache.max_accelerated_files=80000
    opcache.memory_consumption=128
    opcache.save_comments=1
    opcache.revalidate_freq=3

    特别注意的是,如果使用宝塔面板,最好在最后一行结束后用回车换行,否则宝塔还会接着最后一行的最后一个字符添加其他内容。

    “Strict-Transport-Security” HTTP 头未设为至少 “15552000” 秒。为了提高安全性,建议启用 HSTS

    apache的配置文件中加入:

    <IfModule mod_headers.c>
          Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
    </IfModule>

    一些文件未通过完整性检查。有关如何解决这一问题的进一步信息可在 文档中找到。(无效文件列表… / 重新扫描…)

    点开无效文件列表:

    Technical information
    =====================
    The following list covers which files have failed the integrity check. Please read
    the previous linked documentation to learn more about the errors and how to fix
    them.
    
    Results
    =======
    - core
     - FILE_MISSING
      - .user.ini
     - EXTRA_FILE
      - .well-known/acme-challenge/dNIrvBYgSweGs1H0NW2rAMZOp0xKOKJgOdeqzJdJt9g
    
    Raw output
    ==========
    Array
    (
        [core] => Array
            (
                [FILE_MISSING] => Array
                    (
                        [.user.ini] => Array
                            (
                                [expected] => 4843b3217e91f8536cb9b52700efb20300290292cf6286f92794d4cec99df286afeb7dd6c91b1be20bc55eda541eef230a5c5e7dcd46c189edd0ed1e80c6d3f5
                                [current] => 
                            )
    
                    )
    
                [EXTRA_FILE] => Array
                    (
                        [.well-known/acme-challenge/dNIrvBYgSweGs1H0NW2rAMZOp0xKOKJgOdeqzJdJt9g] => Array
                            (
                                [expected] => 
                                [current] => c5ba196825088a37caca22449c2548cf59df02f166fdfc973652b6ce886f84259f2ce6349860a9a8874648207ff37f372ff87a4f08153737dcc5d3deaa61672e
                            )
    
                    )
    
            )
    
    )

    可以看到是.user.ini文件不存在,好在这个文件是用来放跨站的配置文件,在宝塔面板里直接开启放跨站就好,或者创建文件后写入open_basedir=/PathToNextcloud/:/tmp/,其中/PathToNextcloud/是Nextcloud的安装目录;

    而另外一个文件是多余的,直接删掉(这个文件是申请SSL证书时自动生成的)。

    完整config.php设置

    <?php
    $CONFIG = array (
      'instanceid' => 'xxxxxxxxxxx',
      'passwordsalt' => 'xxxxxxxxxxxxxxxxxx',
      'secret' => 'TNFR/+xxxxxxxxxxxxxxxxxxx/',
      'trusted_domains' => 
      array (
        0 => 'nextcloud.guohao.asia',
      ),
      'datadirectory' => '/www/wwwroot/nextcloud/data',
      'dbtype' => 'mysql',
      'version' => '24.0.5.1',
      'overwrite.cli.url' => 'https://nextcloud.guohao.asia',
      'dbname' => 'xxxxxxxxxxxx',
      'dbhost' => 'localhost',
      'dbport' => '',
      'dbtableprefix' => 'oc_',
      'mysql.utf8mb4' => true,
      'dbuser' => 'xxxxxxxxxx',
      'dbpassword' => 'xxxxxxxxx',
      'installed' => true,
      'app_install_overwrite' => 
      array (
        0 => 'tencentcloudcosconfig',
        1 => 'files_external_onedrive',
      ),  
    
    #缓存设置 
      'memcache.local' => 'OC\\Memcache\\Redis',        
      'memcache.distributed' => 'OC\\Memcache\\Memcached',
      'redis' => [        #Redis
          'host' => 'localhost', #Redis 服务器
          'port' => 6379,     #Redis 端口
          'timeout' => 0.0,       #Redis 超时设定
          'password' => 'XXXXXXXXXXX',        #Redis 密码
          'dbindex' => 1,     #Redis 数据库选择
      ],
      'memcached_servers' => array(        #Memcached 服务器
          array('localhost', 11211),
      ),
      'memcached_options' => array(        #Memcached 选项
          \Memcached::OPT_CONNECT_TIMEOUT => 50,
          \Memcached::OPT_RETRY_TIMEOUT =>   50,
          \Memcached::OPT_SEND_TIMEOUT =>    50,
          \Memcached::OPT_RECV_TIMEOUT =>    50,
          \Memcached::OPT_POLL_TIMEOUT =>    50,
          \Memcached::OPT_COMPRESSION =>          true,
          \Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
          \Memcached::OPT_BINARY_PROTOCOL =>      true,
      ),
    
      'cache_path' => '/www/wwwroot/nextcloud/cache',        #缓存目录
      'cache_chunk_gc_ttl' => 60*60*24,        #缓存删除时间
    
    #腾讯云COS  
      'objectstore' => 
      array (
        'class' => '\\OC\\Files\\ObjectStore\\S3',
        'arguments' => 
        array (
          'key' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
          'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
          'bucket' => 'nextcloud-000000000',
          'hostname' => 'cos.ap-beijing.myqcloud.com',
          'use_ssl' => true,
          'autocreate' => false,
        ),
      ),
    
      #电话区号
      'default_phone_region' => 'CN',
    );

    解决nextcloud无法更新应用

    在==config.php==中添加如下内容:

     'appstoreenabled' => true,
     'appstoreurl' => 'https://www.orcy.net/ncapps/v1/',

    更换为国内源(更新进度较官方略慢)

    由“维护模式”改出

    在nextcloud根目录下执行sudo -u www php occ maintenance:mode --off

AI 助手