从零搭建 NextCloud 记录

2020-11-05 1571点热度 1人点赞

NextCloud 作为著名的开源私有云,一直饱受好评,而许多搭建过程也一直饱受"好评"进而都开始使用 docker 。本文记录本站搭建的过程,期间埋坑一堆


1 、选材

从头开始,那么肯定要选最新的系统了,CentOS 8 安排上! Ubuntu 20.04 安排上!

先到官网看看,哦,LAMPR,不行啊,Apache 动态去管理云盘静态资源?怎么想都亏爆啊

继续看看,脚本安装,啊这估计是给白板机准备的,像我们这些老油条肯定得是【自定义】

先准备下环境吧,CentOS 8 Ubuntu 20.04 + Nginx v1.18 + PHP-FPM v7.4 + Mysql 8.0 + Redis 5 + NextCloud


2 、搭建

2.1 、初逢

最开始我是直接把 Wordpres 的 LNMP 组件直接放上用,结果……惨遭毒打

刚开始看来还是很良心的,CentOS 还有案例支持,多好,赶紧安排上 [ 文档链接 ] 然后打出 GG

NextCloud 调用了大量的 PHP 相关组件,这些远远不是 Wordpres 所需要的那一丢丢的 PHP 组件所能比拟的。

Docker 环境的官方 PHP 包也缺少大量组件,必须额外增加。目前没有什么办法能让我在短时间安装好 NextCloud 巨量的组件支持 并且还不会报错,所以放弃 最终发现其实都是参数配置出错

无奈之下,只能先放弃了 Docker 环境 与 生产环境 直接搭建的想法


2.2 、开始

老司机一般为了保证开车平稳,都是用的最熟悉的东西

那在 Linux 中,大家最常见的东西,肯定是 包管理器 apt-get

apt install nginx-full mysql-server mysql-client redis redis-server

PHP 中间摸爬滚打太多,干脆最后安装上的包都整理在这里好了。注意 PHP 版本是 7.4 的

apt install php-bcmath php-common php-curl php-fpm php-gd php-gmp php-igbinary php-imagick php-imap php-json php-mbstring php-redis php-zip
[关联] apt install php7.4 php7.4-bcmath php7.4-cli php7.4-common php7.4-curl php7.4-fpm php7.4-gd php7.4-gmp php7.4-imagick php7.4-imap php7.4-intl php7.4-json php7.4-ldap php7.4-mbstring php7.4-memcached php7.4-msgpack php7.4-mysql php7.4-opcache php7.4-readline php7.4-redis php7.4-xml php7.4-zip

如果涉及到多版本的 PHP 可使用下列命令切换版本
sudo update-alternatives --config php

如果不小心安装错误的版本,由于 apt-get 默认卸载不清除配置文件,所以需要补充 --purge 清理
(小心误删软件安装目录下的文件)
apt --purge remove php
apt install --reinstall php

那么按照该环境开始改配置。本文所使用的配置文件位置如下

Nginx 部分
用户 nginx:nginx
配置文件 /etc/nginx/nginx.conf
站点文件 /etc/nginx/site-enabled/nextcloud.conf
站点路径 /opt/nextcloud/
存储路径 /opt/nextcloud_data/

PHP-FPM 部分
用户 nginx:nginx
配置文件 /etc/php/7.4/fpm/php.ini;/etc/php/7.4/fpm/pool.d/www.conf
链接文件 /etc/php/7.4/fpm/conf.d/*.ini
会话文件 /var/lib/php/sessions
传输文件 /var/run/php-fpm.sock

Mysql 部分
用户 mysql:mysql
配置文件 /etc/mysql/mysql.conf.d/mysqld.cnf
数据文件 /var/lib/mysql/
传输文件 /var/run/mysqld/mysqld.sock

Redis 部分
用户 redis:redis
配置文件 /etc/redis/redis.conf
数据文件 /var/lib/redis/dump.rdb
传输文件 /var/run/redis/redis.sock

2.3 、 Nginx 配置

Nextcloud 官方给了 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 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
    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 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;

    # 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/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(?:$|/) {
        # 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;
    }

    location ~ \.(?:css|js|svg|gif|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 ~ \.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
    }

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

注意需要修改 root 参数,指向有效的本地文件

root /opt/nextcloud;

额外建议分离 站点日志 ,需要增加在 server{} 中

access_log  /var/log/nginx/nextcloud.log  main;
error_log  /var/log/nginx/nextcloud-error.log;

2.4 、 Mysql 配置

首先优先在 Mysql 配置文件中增加如下字段,并重启 Mysql 服务器
前三条字段是限制储存数据格式,同时允许 4bit emoji 字符写入数据库。
第四个是关闭 binlog 的产生(集群则不需要关闭)

[mysqld]
character_set_server = utf8mb4
collation_server = utf8mb4_general_ci
innodb_file_per_table=1
skip-log-bin

然后登陆数据库,并初始化 'root'@'localhost' 密码

mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

增加数据库用户 cloudmanager 并配置密码为 MyNewPass2!
增加数据库 nextclouddb 并为 cloudmanager 附加权限

CREATE USER 'cloudmanager'@'localhost' IDENTIFIED BY 'MyNewPass2!';
CREATE DATABASE IF NOT EXISTS nextclouddb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES ON nextclouddb.* TO 'cloudmanager'@'localhost';
FLUSH PRIVILEGES;

退出并使用新用户登录并测试数据库访问情况,测试好后退出

mysql -uroot -p
show databases;

2.5 、 PHP-FPM 配置

首先我们需要配置 php.ini 这个主体文件。

在这里其实有较多配置,但是我使用的大部分都不是主要的,关注主要的内容即可

memory_limit = 512M
error_log = /var/log/php7.4_error.log
post_max_size = 100M
upload_max_filesize = 100M
date.timezone = PRC
pdo_mysql.default_socket="/var/run/mysqld/mysqld.sock"
mysqli.default_port = 3306
mysqli.default_socket = "/var/run/mysqld/mysqld.sock"
session.save_handler = redis
session.save_path = "unix:/var/run/redis/redis.sock?auth=Your@RedisPass"
opcache.enable=1
opcache.enable_cli=1
opcache.jit_buffer_size=256M
opcache.jit=function
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=30
opcache.save_comments=1
opcache.enable_file_override=1
opcache.huge_code_pages=1

接着我们需要配置 www.conf 的 php-fpm 线程文件,同样也是关注主要内容即可

注意其中下面五条其实和上面的 php.ini 重复,主要是在使用集群时需要自定义配置
中间 slowlog 和 request_slowlog_timeout 是慢查询日志,如果 php 出现 3s 以上的操作会记录

user = nginx
group = nginx
listen = /var/run/php-fpm.sock
listen.backlog = 2048
pm.max_children = 120
pm.start_servers = 12
pm.min_spare_servers = 6
pm.max_spare_servers = 18
slowlog = /var/log/php7.4-$pool-slow.log
request_slowlog_timeout = 3
php_admin_value[max_input_time] = 300
php_admin_value[max_execution_time] = 300
php_admin_value[error_log] = /var/log/php74-$pool-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = redis
php_value[session.save_path]    = "unix:/var/run/redis/redis.sock?auth=Your@RedisPass"
php_value[soap.wsdl_cache_dir]  = "/tmp"

2.6 、 Redis 配置

在 Redis 中也需要同样配置,一共五条。
关闭 TCP 端口
开放 Socket 传输文件,以及赋予权限为 rw-rw----
配置 Redis 密码 本例为 Your@RedisPass 上方曾经用到
增加 Redis 最大分配内存

port 0
unixsocket /var/run/redis/redis.sock
unixsocketperm 660
requirepass Your@RedisPass
maxmemory 512mb

配置完后,我们还需要额外给 php-fpm 访问 redis.sock 的权限,方式是为 nginx 增加权限组

usermod -G redis nginx

如果 Redis 运行时提示一些内容,可以检查一下相关提示并根据提示进行优化操作。

Redis 测试命令

redis-cli -s /var/run/redis/redis.sock
auth Your@RedisPass
keys *

2.7 、 NextCloud 权限配置

解压 NextCloud 文件至 /opt/nextcloud/ 可以检查到 /opt/nextcloud/version.php 存在

unzip nextcloud.zip
mv ./nextcloud /opt/
chown -R nginx:nginx /opt/nextcloud/

创建存储路径 /opt/nextcloud_data/ 并附加权限

mkdir /opt/nextcloud_data
chown -R nginx:nginx /opt/nextcloud_data/

2.8 、 NextCloud 配置

上方的内容汇总,直接在初始化界面填入即可

用户 : 
密码 : 

存储路径 : /opt/nextcloud_data

数据库用户 cloudmanager
数据库密码 MyNewPass2!
数据库库名 nextclouddb
数据库主机 localhost

生成的配置文件类似如下

   'instanceid' => 'qwertyuiop',
   'passwordsalt' => 'asdfghjkl',
   'secret' => 'zxcvbnm',
   'trusted_domains' =>
   array (
     0 => 'cloud.domain.com',
   ),
   'datadirectory' => '/opt/nextcloud_data',
   'dbtype' => 'mysql',
   'version' => '21.0.1.1',
   'overwrite.cli.url' => 'https://cloud.domain.com',
   'dbname' => 'nextclouddb',
   'dbhost' => 'localhost',
   'dbport' => '',
   'dbtableprefix' => 'oc_',
   'mysql.utf8mb4' => true,
   'dbuser' => 'cloudmanager',
   'dbpassword' => 'MyNewPass2!',
   'installed' => true,
   'debug' => false,
   'loglevel' => 2,

3 、奇葩 BUG 汇总

3.0 、排查方案

首先优先开启 log 模式

php.ini     ->  display_errors on(默认 应该 off)
redis.conf  ->  loglevel debug(默认 notice)
config.php  ->  'debug' => true, 'loglevel' => 0, (默认 false , 2)
nextcloud   ->  /opt/nextcloud_data/nextcloud.log  (也可能在站点目录里)

3.1 、无限登录循环

现象:是登录按钮点击后,页面刷新,又回来没变
原理:本质是 Redis 写入 session 失败,所以登录失效
处置:检查 PHP 与 Redis 关联的配置即可。

本人 OS:chmod 644 /etc/redis/redis.conf 坑一年


3.2 、消息传递被阻拦

现象:消息通知清理失败,新建出错,删除出错,编辑资料自动失效,却有一些小东西可以用
原理:NextCloud 使用了 method 除常规 (GET|HEAD|POST) 外的 (PUT|DELETE) 操作
处置:取消限制 method 的规则


3.3 、 Nextcloud 应用商店打不开或下载速度慢

现象:应用商店打不开,下载新应用超时
原理:NextCloud 官方源位于国外,直连的访问速度和响应延迟均存在一定问题。
处置:在 config.php 中增加第三方镜像源 [ 链接 ]

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

3.4 、用户已被禁用

现象:登陆时弹出提示 This account is disabled
原理:NextCloud 有密码过期限制,参考 [ 链接 ]
处置:重置密码并启用用户即可

cd /opt/nextcloud/
sudo -u nginx php occ user:resetpassword user_id
sudo -u nginx php occ user:enable user_id

3.5 、 Nextcloud 更新失败

现象:Nextcloud 更新失败
原理:关于第四步 step4:Downloading 存在下载过慢导致失败的可能性,默认 PHP 是 30s 超时,参考 [ 链接 ]
处置:手动下载文件并使用命令行继续进行升级

站点路径 /opt/nextcloud/
存储路径 /opt/nextcloud_data/
标记路径 /opt/nextcloud_data/updater-*/.step
文件路径 /opt/nextcloud_data/updater-*/downloads/nextcloud-*.zip
启动更新器,并等待进度到达 Downloading 时停止 ( Ctrl+C 或者关闭 Shell )
sudo -u nginx php updater/updater.phar

Nextcloud Updater - version: ***

Current version is ***.

Update to Nextcloud *** available. (channel: "stable")
Following file will be downloaded automatically: https://download.nextcloud.com/server/releases/nextcloud-***.zip
Open changelog ↗

Steps that will be executed:
[✔] Check for expected files
[✔] Check for write permissions
[✔] Create backup
[?] Downloading
[ ] Verify integrity
[ ] Extracting
[ ] Enable maintenance mode
[ ] Replace entry points
[ ] Delete old files
[ ] Move new files in place
[ ] Done
此时标记文件内容应为
{"state":"start","step":4}
修改标记文件,修改为
{"state":"end","step":4}
然后覆盖下载文件 (注意更正相关文件路径和文件名)
cp ~/nextcloud-*.zip /opt/nextcloud_data/updater-*/downloads/nextcloud-*.zip
再次启动更新器
sudo -u nginx php updater/updater.phar

Nextcloud Updater - version: ***

Current version is ***.

Update to Nextcloud *** available. (channel: "stable")
Following file will be downloaded automatically: https://download.nextcloud.com/server/releases/nextcloud-***.zip
Open changelog ↗

Steps that will be executed:
[✔] Check for expected files
[✔] Check for write permissions
[✔] Create backup
[✔] Downloading
[ ] Verify integrity
[ ] Extracting
[ ] Enable maintenance mode
[ ] Replace entry points
[ ] Delete old files
[ ] Move new files in place
[ ] Done

Continue update? [y/N] y

Info: Pressing Ctrl-C will finish the currently running step and then stops the updater.

[✔] Check for expected files
[✔] Check for write permissions
[✔] Create backup
[✔] Downloading
[✔] Verify integrity
[✔] Extracting
[✔] Enable maintenance mode
[✔] Replace entry points
[✔] Delete old files
[✔] Move new files in place
[✔] Done

Update of code successful.

Should the "occ upgrade" command be executed? [Y/n] y
如果需要清理更新进程,可以使用命令
sudo -u nginx php occ maintenance:repair

3.6 、此实例不支持 SVG 警告

此实例中的 php-imagick 模块不支持 SVG 。为了获得更好的兼容性,建议安装它

出现此错误的原因是本机 libmagickcore 默认安装的是普通版本,而 SVG 支持需要用到 extra 版本

root@localhost:~# dpkg -l libmagickcore* | grep 'ii'
ii  libmagickcore-6.q16-6:amd64       8:6.9.11.60+dfsg-1.3ubuntu0.22.04.3 amd64        low-level image manipulation library -- quantum depth Q16
root@localhost:~# apt install libmagickcore-6.q16-6-extra
root@localhost:~# dpkg -l libmagickcore* | grep 'ii'
ii libmagickcore-6.q16-6:amd64 8:6.9.11.60+dfsg-1.3ubuntu0.22.04.3 amd64 low-level image manipulation library -- quantum depth Q16
ii libmagickcore-6.q16-6-extra:amd64 8:6.9.11.60+dfsg-1.3ubuntu0.22.04.3 amd64 low-level image manipulation library - extra codecs (Q16)

4 、其他配置

4.1 、 NextCloud 集群配置

在 站点文件/config/config.php 中增加如下字段,支持 redis 集群。

  'memcache.distributed' => '\OC\Memcache\Redis',
  'redis' => [
    'host' => '/var/run/redis/redis.sock',
    'port' => 0,
    'dbindex' => 0,
    'password' => '4e1fa509f485e548e789f9ddba6eba62d68037467f23dbe362077eba6a592f6c',
    'timeout' => 1.5,
  ],
  'memcache.locking' => '\OC\Memcache\Redis',

注意,config.php 的上下格式是有优先级的。少的时候可能会直接报错

在 php.ini 中增加 redis session 慢写入和锁定的配置

redis.session.locking_enabled=1
redis.session.lock_retries=-1
redis.session.lock_wait_time=10000

在 Mysql 配置文件中也需要增加一部分,比如去掉之前的

[mysqld]
#skip-log-bin
transaction_isolation = READ-COMMITTED
binlog_format = ROW
#innodb_large_prefix=on                #mysql8 失效
#innodb_file_format=barracuda          #mysql8 失效
innodb_buffer_pool_size=256M
innodb_io_capacity=4000

4.2 、 Docker 下去除 index.php 的链接字样

当你看到 domain.com/index.php/hello.txt 这样的 URL 感到十分苦恼,可以通过修改配置文件撤掉该字样。

第一种方案是修改 URL 重写规则,建议采用此方案 [ 链接 ]

<IfModule mod_rewrite.c>
  Options -MultiViews
  RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]
  RewriteRule ^core/preview.png$ index.php [PT,E=PATH_INFO:$1]
  RewriteCond %{REQUEST_FILENAME} !\.(css|js|svg|gif|png|html|ttf|woff|ico|jpg|jpeg)$
  RewriteCond %{REQUEST_FILENAME} !core/img/favicon.ico$
  RewriteCond %{REQUEST_FILENAME} !/remote.php
  RewriteCond %{REQUEST_FILENAME} !/public.php
  RewriteCond %{REQUEST_FILENAME} !/cron.php
  RewriteCond %{REQUEST_FILENAME} !/core/ajax/update.php
  RewriteCond %{REQUEST_FILENAME} !/status.php
  RewriteCond %{REQUEST_FILENAME} !/ocs/v1.php
  RewriteCond %{REQUEST_FILENAME} !/ocs/v2.php
  RewriteCond %{REQUEST_FILENAME} !/updater/
  RewriteCond %{REQUEST_FILENAME} !/ocs-provider/
  RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge/.*
  RewriteRule . index.php [PT,E=PATH_INFO:$1]
  RewriteBase /
  <IfModule mod_env.c>
    SetEnv front_controller_active true
    <IfModule mod_dir.c>
      DirectorySlash off
    </IfModule>
  </IfModule>
</IfModule>

第二种方案是过去使用的,由于上方有好的方式所以不建议使用。同时仅能修改一部分的 URL,不是所有的都可以用这个方式修改。

./lib/private/URLGenerator.php 第 246 行附近
  public function getAbsoluteURL(string $url): string {
    $separator = strpos($url, '/') === 0 ? '' : '/';
    /** index_edit **/
    $url=str_replace("/index.php/","/",$url);

./config/config.php 新增一行
  'htaccess.RewriteBase' => '/',

4.3 、安装 STUN/TURN 服务器

文章参考链接 [ 链接 ] 配置文件参数说明 [ 链接 ]

配置文件参考链接 [ 链接 ] 服务端测试方式 [ 链接 ]

apt install coturn

然后在 Ubuntu 中开启服务器

sed -i '/TURNSERVER_ENABLED/c\TURNSERVER_ENABLED=1' /etc/default/coturn
systemctl enable coturn.service
systemctl start coturn.service

接着修改配置文件 /etc/turnserver.conf

#syslog    默认自带的配置文件仅开启了 syslog 一个参数

# 配置 turn 监听接口(可选),turn 监听端口,备用 turn 监听端口(可选),turns 监听端口(可选),备用 (ALT)turns 监听端口(可选),以及内外网 NAT 地址(可选)如果备用端口未配置,默认为主端口号+1 的端口
# listening-device=eth0
# external-ip=101.202.102.201/192.168.1.100
listening-port=3478
alt-listening-port=3479
tls-listening-port=5349
alt-tls-listening-port=5350

fingerprint
lt-cred-mech

# 开启 NextCloud 所使用的 WebAuth 认证
use-auth-secret
# 配置 NextCloud 所使用的 WebAuth 认证密钥(时间密钥)(自行修改)
static-auth-secret=yoursecretpasswordqwertytuiop
# 配置本服务器所属域名(自行修改)
realm=your.domain.name

total-quota=100
bps-capacity=0
stale-nonce

# 配置 TLS 使用证书(自行修改)
cert=/etc/cert/fullchain.pem
pkey=/etc/cert/privkey.pem
# 配置自定义加密选择方式
# cipher-list="ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AES:RSA+3DES:!ADH:!AECDH:!MD5"

# 安全限制,禁用本地回复,禁用广播回复,禁止 DTLS(UDP 方式的 TLS 加密),禁止 TLSV1.0/1.1
no-loopback-peers
no-multicast-peers
no-dtls
no-tlsv1
no-tlsv1_1
no-cli
# 输出日志
no-stdout-log
syslog
simple-log
log-file=/var/log/coturn.log

配置完重启服务器,开放防火墙对应端口(以下仅开启了 turn 监听端口)(UDP 无认证可以选择不开)

firewall-cmd --zone=public --per --add-port=3478/tcp
firewall-cmd --zone=public --per --add-port=3478/udp
firewall-cmd --reload

在服务端配置页面中,进入 后台管理页面,通话(talk),在 STUN 服务器 和 TURN 服务器 中填写对应的信息,并等待测试通过后保存即可。(使用非加密的 turn 是因为 nextcloud 的 webrtc 已经加密,并且不能很好支持 turns [ 链接 ])

stun:your.domain.name:3478
turn:your.domain.name:3478 yoursecretpasswordqwertytuiop tcp/udp

在检测网站中,清空默认的 Google stun server,然后手动添加本次搭建的 STUN 服务器,URI 需要携带前缀 stun:yourdomainname:port
检测网站 [ 链接 ] WebRTC 测试网站 [ 链接 ]

点击 Gather candidates 按钮开始测试,等待下方出现 host 和 srflx 发送接收两条消息,即为正常使用。


4.4 、新用户云盘默认文件

Nextcloud 创建用户时,会自动复制 ./core/skeleton 中的文件到新用户目录下。

所以如果需要修改默认用户云盘的文件内容,可以直接清空这些文件自行创建。


4.5 、 Redis 配置

透明大页面(THP)是一种 Linux 内存管理系统,它通过使用较大的内存页面来减少具有大量内存的计算机上的转换后备缓冲区(TLB)查找的开销。

但是,启用 THP 时,数据库工作负载通常表现不佳,因为它们往往具有稀疏而不是连续的内存访问模式。 在 Linux 上运行 MongoDB 时,应禁用 THP 以获得最佳性能。

默认 Redis 在启动时会检查 THP 功能是否启用,并在发现该功能启用后提示如下警告内容

WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

针对该问题我们需要禁用该功能,但这个功能不能在 sysctl.conf 中禁用,所以我们需要手动创建一个文件,参考 [ 链接 ]

vim /etc/systemd/system/sysctl-thp-disable.service
[Unit]
Description=Disable Transparent Huge Pages (THP)
DefaultDependencies=no
After=sysinit.target local-fs.target
Before=redis.service

[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled > /dev/null'


[Install]
WantedBy=basic.target
systemctl enable sysctl-thp-disable.service
systemctl start sysctl-thp-disable.service

检查 THP 是否已成功设置为 [never] 通过运行以下命令:

cat /sys/kernel/mm/transparent_hugepage/enabled

4.6 、 Windows 10 使用 WebDAV 访问

既然都有了 NextCloud 为什么还要用 RaiDrive 挂 SFTP 来访问呢?

官方链接 [ 链接 ]

查阅手册,我们得到了一个 URL 用来填写

https://example.com:443/nextcloud/remote.php/dav/files/USERNAME/
Schme    Domain      Port Subdir     PHP_File    Path        UserName
https:// example.com :443 /nextcloud /remote.php /dav/files/ USERNAME

手动修改成上文配置所使用的 URL

https://cloud.domain.com:443/remote.php/dav/files/USERNAME/

Windows 会提示需要输入用户名和密码,对照尾部 UserName 部分填写对应的用户名和密码,并勾选记住凭据(凭据管理器中可以管理)

创建好后的名称如下(Subdir = / 时, nextcloud 会被替换为 DavWWWRoot )

user01 (\\cloud.domain.com@SSL@443\nextcloud\remote.php\dav\files)

4.7 、什么是 apt remove --purge

在开发者封装 Deb 包时,可以遵循 [ 链接 ] 制作完全删除的脚本

例如 Nginx 的包在删除时,脚本如下所示 [ 链接 ]

#!/bin/sh
set -e

case "$1" in
  purge)
    rm -rf /var/lib/nginx /var/log/nginx /etc/nginx
    ;;

  upgrade|remove|failed-upgrade|abort-install|abort-upgrade|disappear)
    ;;

  *)
    echo "postrm called with unknown argument \`$1'" >&2
    exit 1
    ;;
esac

#DEBHELPER#

exit 0

从中我们可以看出,脚本执行了 rm -rf 命令,清理了三个文件目录

而如果用户自行修改了目录位置,例如 ln -s /opt/nginx/file/ /etc/nginx ,此时会因为命令而跳转到非安装目录进行删除操作,有一定危险性

任何一个 Deb 包都可以通过 7z 打开,但 Windows 需要选择 打开压缩包,以 * 的方式打开 / Open as * 才能看到全部文件。

打开后可以看到 control.tar.xz 脚本文件 和 data.tar.xz 数据文件,在 control 中就有我们需要的文件了

StarryVoid

Have a good time