下面,我们在 Debian 12 下安装并配置 Nextcloud 服务端。
在Hestiacp中新建网站,配置ssl,新建数据库的操作就不再赘述了。如果你是第一次看到hestiacp系列教程,请到网站上方搜索关键词 HestiaCP,就能看到完整的一系列教程了。
Nginx模板
hestiacp要自定义nginx配置文件,模板是必不可少的。
下面是我修改了官方推荐的nginx配置的模板。
nextcloud.tpl
# Version 2024-07-17
# Set the `immutable` cache control options only for assets with a cache busting `v` argument
map $arg_v $asset_immutable {
"" "";
default ", immutable";
}
server {
listen %ip%:%proxy_port%;
listen [::]:%proxy_port%;
server_name %domain_idn% %alias_idn%;
# Path to the root of your installation
root %docroot%;
# Prevent nginx HTTP Server Detection
server_tokens off;
# Enforce HTTPS
return 301 https://$server_name$request_uri;
# 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 10240M;
client_body_timeout 300s;
fastcgi_buffers 128 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 text/javascript 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;
# The settings allows you to optimize the HTTP2 bandwidth.
# See https://blog.cloudflare.com/delivering-http-2-upload-speed-improvements/
# for tuning hints
client_body_buffer_size 512k;
# 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-Frame-Options "SAMEORIGIN" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
add_header X-Robots-Tag "noindex, nofollow" 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;
# Set .mjs and .wasm MIME types
# Either include it in the default mime.types list
# and include that list explicitly or add the file extension
# only for Nextcloud like below:
include mime.types;
types {
text/javascript mjs;
application/wasm wasm;
}
# 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`, `/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;
include %home%/%user%/conf/web/%domain%/nginx.forcessl.conf*;
# 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 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\/.+|ocs-provider\/.+|.+\/richdocumentscode(_arm64)?\/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;
include %home%/%user%/conf/web/%domain%/nginx.fastcgi_cache.conf*;
}
# Serve static files
location ~ \.(?:css|js|mjs|svg|gif|ico|jpg|png|webp|wasm|tflite|map|ogg|flac)$ {
try_files $uri /index.php$request_uri;
# HTTP response headers borrowed from Nextcloud `.htaccess`
add_header Cache-Control "public, max-age=15778463$asset_immutable";
add_header Referrer-Policy "no-referrer" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
add_header X-Robots-Tag "noindex, nofollow" always;
add_header X-XSS-Protection "1; mode=block" always;
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
}
# Rule borrowed from `.htaccess`
location /remote {
return 301 /remote.php$request_uri;
}
location / {
try_files $uri $uri/ /index.php$request_uri;
}
include %home%/%user%/conf/web/%domain%/nginx.conf_*;
}
nextcloud.stpl
# Version 2024-07-17
upstream php-handler {
#server 127.0.0.1:9000;
server %backend_lsnr%;
}
# Set the `immutable` cache control options only for assets with a cache busting `v` argument
map $arg_v $asset_immutable {
"" "";
default ", immutable";
}
server {
listen %ip%:%proxy_ssl_port% http2;
listen [::]:443 ssl http2;
# With NGinx >= 1.25.1 you should use this instead:
# listen 443 ssl;
# listen [::]:443 ssl;
# http2 on;
server_name %domain_idn% %alias_idn%;
# Path to the root of your installation
root %docroot%;
# Use Mozilla's guidelines for SSL/TLS settings
# https://mozilla.github.io/server-side-tls/ssl-config-generator/
ssl_certificate %ssl_pem%;
ssl_certificate_key %ssl_key%;
# Prevent nginx HTTP Server Detection
server_tokens off;
# 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 10240M;
client_body_timeout 300s;
fastcgi_buffers 128 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 text/javascript 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;
# The settings allows you to optimize the HTTP2 bandwidth.
# See https://blog.cloudflare.com/delivering-http-2-upload-speed-improvements/
# for tuning hints
client_body_buffer_size 512k;
# 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-Frame-Options "SAMEORIGIN" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
add_header X-Robots-Tag "noindex, nofollow" 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;
# Set .mjs and .wasm MIME types
# Either include it in the default mime.types list
# and include that list explicitly or add the file extension
# only for Nextcloud like below:
include mime.types;
types {
text/javascript mjs;
application/wasm wasm;
}
# 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`, `/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;
include %home%/%user%/conf/web/%domain%/nginx.hsts.conf*;
# 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 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\/.+|ocs-provider\/.+|.+\/richdocumentscode(_arm64)?\/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;
include %home%/%user%/conf/web/%domain%/nginx.fastcgi_cache.conf*;
}
# Serve static files
location ~ \.(?:css|js|mjs|svg|gif|ico|jpg|png|webp|wasm|tflite|map|ogg|flac)$ {
try_files $uri /index.php$request_uri;
# HTTP response headers borrowed from Nextcloud `.htaccess`
add_header Cache-Control "public, max-age=15778463$asset_immutable";
add_header Referrer-Policy "no-referrer" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
add_header X-Robots-Tag "noindex, nofollow" always;
add_header X-XSS-Protection "1; mode=block" always;
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
}
# Rule borrowed from `.htaccess`
location /remote {
return 301 /remote.php$request_uri;
}
location / {
try_files $uri $uri/ /index.php$request_uri;
}
include %home%/%user%/conf/web/%domain%/nginx.ssl.conf_*;
}
这里要特别注意, Nginx 的 listen
指令和虚拟主机的绑定逻辑。
1. IP 绑定和虚拟主机
当你使用 %ip%:%proxy_ssl_port%
形式的 listen
指令时,你明确指定了 Nginx 只在特定 IP 地址和端口上监听。这样,Nginx 会根据你配置的 server_name
指定的域名来提供正确的 SSL 证书。这种情况下,Nginx 知道应该为 nextcloud.xx.com
提供相应的证书。
但是,当你使用 listen %proxy_ssl_port%
(即没有指定 IP 地址),Nginx 会监听所有可用的 IP 地址。在这种情况下,如果你有多个虚拟主机(如 HestiaCP 的主控面板和 Nextcloud 站点),Nginx 可能默认使用第一个匹配的 server
块的 SSL 证书,这通常是 HestiaCP 主控面板的证书。
2. 默认虚拟主机问题
当没有明确指定 IP 地址时,Nginx 需要决定哪个 server
块会处理特定请求。如果 Nginx 找不到匹配的 server_name
,它会使用第一个加载的虚拟主机作为默认服务器,并加载相应的证书。如果你不指定 IP,HestiaCP 的管理面板可能会被 Nginx 识别为默认的虚拟主机,因此为 nextcloud.xx.com
加载了 HestiaCP 主控面板的 SSL 证书。
3. IPV6支持
HestiaCP自带的变量暂时不支持ipv6的配置,如果想启用ipv6,自行把主机的ipv6填写进去,比如
listen [xxxxxxxxx]:%proxy_ssl_port% ssl;
优化 PHP-FPM
登录管理员账户,在设置中可以找到修改php-fpm配置文件的地方。这里列举几个:
- 将
upload_max_filesize
从默认的2M
改为10240M
,即 10 GB。 post_max_size
设置 POST 请求体的最大大小。这里从8M
改为10240M
,即 10 GB。memory_limit
限制了 PHP 脚本执行过程中可以使用的内存量。将它从128M
改为512M
,允许 PHP 使用更多的内存,有助于处理更大的请求或更复杂的任务。- opcache.interned_strings_buffer=16
更多的具体配置,可以参考官方文档
下面这个小工具可以帮助你计算适合你的系统的值:
1. pm.max_children
- 解释: 这是 PHP-FPM 可以同时运行的最大子进程数,表示服务器在处理并发请求时最多可以启动的 PHP-FPM 工作进程数量。
- 作用: 当服务器负载高时,PHP-FPM 会根据需求启动子进程处理请求。如果达到
pm.max_children
的限制,新的请求将会被挂起,直到某些进程完成并释放资源。 - 如何设置: 这个值应基于服务器的内存大小和每个 PHP-FPM 进程的平均内存使用量来计算(上文已经讨论过如何计算)。
2. pm.start_servers
- 解释: 当 PHP-FPM 启动时,初始化时会生成的 PHP-FPM 工作进程的数量。
- 作用: 这个参数决定了 PHP-FPM 服务启动时,立刻准备好处理请求的进程数量。如果流量开始增加,PHP-FPM 将逐步根据需求启动更多进程。
- 如何设置: 通常
pm.start_servers
的值比pm.max_children
小,但也要足够高,以便在高流量开始时能够快速响应。根据服务器负载,可以设置为pm.max_spare_servers
和pm.min_spare_servers
之间的一个值。
3. pm.min_spare_servers
- 解释: PHP-FPM 进程池中保持的最少空闲子进程数量。
- 作用: 当空闲的 PHP-FPM 子进程数量少于
pm.min_spare_servers
,PHP-FPM 将创建新的子进程来满足需求,以确保总有足够的进程准备处理请求。 - 如何设置: 这个值应根据服务器的流量模式来设置。如果您有稳定的中等流量,可以设置为较低的值。如果流量波动较大,可以设置稍高一些,以确保高峰期有足够的空闲进程。
4. pm.max_spare_servers
- 解释: PHP-FPM 进程池中允许的最多空闲子进程数量。
- 作用: 当空闲的 PHP-FPM 子进程数量超过
pm.max_spare_servers
时,PHP-FPM 将自动终止多余的空闲进程,以避免浪费系统资源。 - 如何设置: 这个值应设置得稍高一些,以应对流量的突增,但不要过高,否则会浪费服务器资源。
下载NextCloud
进入 /home/user/web/domain/public_html/
目录中,下载最新版本
wget -O nextcloud.zip https://download.nextcloud.com/server/releases/latest-29.zip
解压
unzip nextcloud.zip
然后设置权限
sudo chown -R user:user /home/domain/web/domain/public_html/
如果你解压出来之后,程序在目录**/public_html/nextcloud/
里面,也记得在hestiacp面板中的高级选项,设置好自定义网站主目录。
安装NextCloud
安装完成后,直接访问 https://example.com
。
然后,根据提示,填入你之前新建的数据库信息以及管理员帐号密码即可。
后台作业
如下图所示
配置 Redis
打开此目录下 /home/user/web/domain/public_html/config/
config.php这个文件,加入如下配置:
'memcache.locking' => '\\OC\\Memcache\\Redis',
'memcache.distributed' => '\\OC\\Memcache\\Redis',
'memcache.local' => '\\OC\\Memcache\\Redis',
'redis' =>
array (
'host' => '127.0.0.1',
'port' => 6379,
'password' => 'xxx',
),
参考
重启 PHP-FPM。
配置国际区号
还是config.php,加入以下配置
'default_phone_region' => 'CN',
配置维护时段
还是config.php,加入以下配置
'maintenance_window_start' => '02:00',
'maintenance_window_end' => '04:00',
PHP OPcache 模块配置不正确
编辑 PHP 配置文件 php.ini
opcache.interned_strings_buffer = 16
将值设置为 16
或更高,这将增加驻留字符串缓冲区大小,建议从 16 开始。
缺少 PHP GMP 模块
sudo apt install php8.2-gmp
PHP Imagick 模块缺少 SVG 支持
解决方法:
- 安装必要的软件包来启用 SVG 支持,确保安装了 ImageMagick 和 libmagickcore:
sudo apt install imagemagick libmagickcore-6.q16-6-extra
- 安装或重新安装 PHP
imagick
模块:
sudo apt install php-imagick
- 检查
convert -list format | grep SVG