在 ArchLinux 上安装 Nextcloud

之前因为某些原因,弄了一台 Vultr 上的主机,有 1000GB 的带宽,基本上每月都用不完。因此就想着用来搭建一些服务自己来用。

之前尝试安装过 rsshub,不过后来转移到 Vercel 了。这次就想尝试安装一下 Nextcloud。

虽然我知道,直接用 docker 会更简单,不过既然 Nextcloud 以及在官方的源(我的系统是 Arch Linux)里了,就忍不住折腾一番。

需要用到的安装包:

1
pacman -S mariadb php nextcloud php-intl php-fpm redis php-redis

由于之前已经安装并配置了 nginx,这个就不用重新在安装了。

1. 配置数据库

由于我用的文件系统是 btrfs,所以需要先禁用写时复制(CoW):

1
2
btrfs subvolume create /var/lib/mysql
chattr +C /var/lib/mysql

然后运行数据库:

1
2
3
mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
systemctl enable mariadb
systemctl start mariadb

增强安全性:

1
mysql_secure_installation

限制只能本地访问:

/etc/my.cnf.d/server.cnf
1
2
3
[mysqld]
bind-address=127.0.0.1
skip-networking

自动补全功能:

/etc/my.cnf.d/mysql-clients.cnf
1
2
[mysql]
auto-rehash

配置 Nextcloud 的数据库:

1
2
3
4
CREATE DATABASE nextcloud DEFAULT CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost' IDENTIFIED BY '<password>';
FLUSH PRIVILEGES;
\q

2. 配置 php

启用所需要的模块:

/etc/php/php.ini
1
2
3
4
5
6
7
8
9
10
[php]
extension=gd
extension=iconv
extension=intl
extension=mysqli
zend_extension=opcache
extension=pdo_mysql

memory_limit = 512M
upload_max_filesize = 1024M

3. 配置 Redis 缓存

启用 redis:

1
2
systemctl enable redis
systemctl start redis

配置 php 扩展:

/etc/php/conf.d/redis.ini
1
extension=redis
/etc/php/conf.d/igbinary.ini
1
2
[igbinary]
extension=igbinary.so

在 nextcloud 的配置文件中启用 redis:

/etc/webapps/nextcloud/config.php
1
2
3
4
5
6
7
'memcache.distributed' => 'OCMemcacheRedis',
'memcache.local' => 'OCMemcacheRedis',
'memcache.locking' => 'OCMemcacheRedis',
'redis' => array(
'host' => 'localhost',
'port' => 6379,
),

4. 配置 php-fpm

在 php-fpm 添加 nextcloud 配置:

/etc/php/php-fpm.d/nextcloud.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[nextcloud]
user = nextcloud
group = nextcloud
listen = /run/nextcloud/nextcloud.sock
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp

; should be accessible by your web server
listen.owner = http
listen.group = http

pm = dynamic
pm.max_children = 15
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

运行

1
systemctl edit php-fpm.service

编辑 php-fpm 服务:

/etc/systemd/system/php-fpm.service.d/override.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Service]
# Your data directory
ReadWritePaths=/var/lib/nextcloud/data

# Optional: add if you've set the default apps directory to be writable in config.php
ReadWritePaths=/usr/share/webapps/nextcloud/apps

# Optional: unnecessary if you've set 'config_is_read_only' => true in your config.php
ReadWritePaths=/usr/share/webapps/nextcloud/config

ReadWritePaths=/etc/webapps/nextcloud/config

# Optional: add if you want to use Nextcloud's internal update process
# ReadWritePaths=/usr/share/webapps/nextcloud

启用 php-fpm 服务:

1
2
systemctl enable php-fpm.service
systemctl start php-fpm.service

5. 配置域名

我的域名是托管在阿里云,在 dns 解析中添加三级域名 nextcloud 解析,并申请 ssl 证书。

证书下载之后,上传到 /etc/nginx/ssl/

6. 配置 nginx

在 nginx 添加配置,基本是按照官方参考文件 来的,只修改了 php-handlerserver_namessl_certificateroot

/etc/nginx/site-available/nextcloud.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
upstream php-handler {
server unix:/run/nextcloud/nextcloud.sock;
}

server {
listen 80;
listen [::]:80;
server_name <nextcloud.example.com>;

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

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name <nextcloud.example.com>;

# Use Mozilla's guidelines for SSL/TLS settings
# https://mozilla.github.io/server-side-tls/ssl-config-generator/
ssl_certificate ssl/<nextcloud.example.com>.pem;
ssl_certificate_key ssl/<nextcloud.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 1024M;
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 /usr/share/webapps/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 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(?:$|/) {
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)$ {
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
}

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

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

然后启用该配置:

1
ln -s /etc/nginx/site-available/nextcloud.conf /etc/nginx/site-enabled/nextcloud.conf

原来的 nginx 中已有配置:

/etc/nginx/nginx.conf
1
2
3
4
5
http {
# ...
include /etc/nginx/sites-enabled/*;
# ...
}

之后重启 nginx 服务:

1
systemctl restart nginx

7. 配置 nextcloud

在 nextcloud 的配置文件中添加:

/etc/webapps/nextcloud/config.php
1
2
3
'trusted_domains' => [
'<nextcloud.example.com>'
]

然后安装 nextcloud:

1
occ maintenance:install --database mysql --database-name nextcloud --database-host localhost --database-user nextcloud --database-pass='<password>' --data-dir /var/lib/nextcloud/data

然后就可以登录了!

8. 配置 pacman hook

在 nextcloud 软件包更新后,自动更新 nextcloud 数据库:

1
2
mkdir -vp /etc/pacman.d/hooks
ln -sv /usr/share/doc/nextcloud/nextcloud.hook /etc/pacman.d/hooks/

参考:

  • ArchWiki: Nextcloud
  • ArckWiki: MariaDB
  • Documentation: Nextcloud