0%

使用nextcloud搭建个人网盘

安装

安装nextcloud可以采用docker、zip和web安装的方法,本文采用的是web安装。

个人环境:宝塔 + nginx + mysql + php7.4

先去官网,选择web installer下载setup-nextcloud.php。再将该文件传到服务器中希望创建nextcloud网盘的目录下,推荐新建一个nextcloud的文件夹,然后放在那下面。

打开nginx的配置文件(宝塔中的操作:网站->设置->配置文件,如果专门配置了域名可以选择添加站点,本文直接在已有域名上通过不同端口访问nextcloud),直接复制已有的配置文件。在最后粘贴,修改listen的端口(记得打开对应端口的权限)和root(就修改为存setup-nextcloud.php的目录)。

在浏览器中通过域名端口访问setup-nextcloud.php文件,如https://itlay.top:1234/setup-nextcloud.php

然后进入了安装界面,如果没进入安装界面可以查看下nginx和网站的错误日志,很多时候是因为用户权限的问题。

然后按提示安装即可。注意不要急,点击一下就行,因为会在后台下载安装,多次点击会导致进程异常,可以看浏览器左上角是刷新还是X判断是否在后台运行。

nginx配置

以上安装完成后应该就能直接用了,好像不改nginx了也能行。不过我还是按官方的配置修改了,配置文件如下,自行修改域名端口rootssl证书位置

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
upstream php-handler {
#server 127.0.0.1:9000;
server unix:/var/run/php/php7.4-fpm.sock; #注意地址,可以去php的fpm配置中查看listen=
}

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

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

在最开始设置nginx的时候是复制的之前有的一份nginx配置,再粘贴到后面,能不能直接将上面的配置复制过去,一步到位呢?可以试试。PS:以上配置主要是使用zip安装的时候用的。

优化

安装完nextcloud后打开网页很慢,而且占满了内存,可以尝试以下方法进行调优。

参考:https://docs.nextcloud.com/server/21/admin_manual/installation/server_tuning.html

使用cron执行后台任务

在管理页面的基本设置中,修改后台任务为cron

运行

1
sudo crontab -u www-data -e

www-data修改为对应用户,可参考php的user配置。添加以下配置

1
*/5  *  *  *  * php -f /var/www/nextcloud/cron.php

/var/www/nextcloud/修改为nextcloud的地址,参考nginx中的root

添加缓存

在宝塔中打开php管理,选择安装扩展,安装opcacheredisapcu

redis在添加完后会直接帮你运行,默认运行端口是127.0.0.1:6379。修改config/config.php,在配置末尾(非最后一行)加上以下配置。

1
2
3
4
5
6
7
'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Redis',
'redis' => [
'host' => '127.0.0.1',
'port' => 6379,
],
'memcache.locking' => '\OC\Memcache\Redis',