Nextcloud是一套用于创建网络硬盘的客户端-服务器软件,是一款开源网盘的绝佳解决方案,Nextcloud上的文件存储在一般的目录结构中,并可透过WebDAV访问。用户的文件会在传输时加密。
Nextcloud支持Windows(Windows XP、Vista、7、8、10)、macOS(10.6或更新版本)、iOS、Android、FreeBSD或Linux等客户端。
本文以CentOS7系统为例,基于wlnmp一键安装包,使用php7.4、mysql5.7版本来安装。请严格按照文中步骤进行(注:擅自更换系统及文中所提版本,有安装失败风险)
我这里不得不强调一下防火墙和selinux,请自行关闭或放行防火墙和selinux规则
1、添加wlnmp一键安装包源
1 |
rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm |
2、安装epel源
1 |
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo |
3、安装lnmp环境
1 2 3 |
yum clean all yum makecache fast yum install wphp74 wnginx wmysql57 |
4、安装php依赖
1 |
yum install wphp74-fileinfo wphp74-imagick wphp74-apcu |
5、Nginx配置
Nextcloud建议使用https,我这里为了方便就直接使用http方式了(请在以下两个配置文件中任选其一),下面我给出了两个nginx的配置,如果你想使用https,请自行申请证书和生成赫尔曼密钥。
http配置文件,请删除原有的/usr/local/nginx/conf/vhost/demo.conf内容,将以下内容复制进来,并替换server_name的地址!
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 |
server { listen 80; server_name 192.168.50.194; index index.html index.htm index.php; root html/nextcloud; 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; fastcgi_hide_header X-Powered-By; location = /robots.txt { allow all; log_not_found off; access_log off; } location = /.well-known/carddav { return 301 $scheme://$host:$server_port/remote.php/dav; } location = /.well-known/caldav { return 301 $scheme://$host:$server_port/remote.php/dav; } location /.well-known/acme-challenge { } location / { rewrite ^ /index.php; } location ~ ^\/(?:build|tests|config|lib|3rdparty|templates|data)\/ { deny all; } location ~ ^\/(?:\.|autotest|occ|issue|indie|db_|console) { deny all; } location ~ ^\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+)\.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; # Avoid sending the security headers twice fastcgi_param modHeadersAvailable true; # Enable pretty urls fastcgi_param front_controller_active true; fastcgi_pass unix:/tmp/php-fpm74.sock; fastcgi_intercept_errors on; fastcgi_request_buffering off; } location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) { try_files $uri/ =404; index index.php; } location ~ \.(?:css|js|woff2?|svg|gif|map)$ { try_files $uri /index.php$request_uri; add_header Cache-Control "public, max-age=15778463"; 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; access_log off; } location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ { try_files $uri /index.php$request_uri; access_log off; } access_log /data/logs/nginx/nextcloud_access.log; error_log /data/logs/nginx/nextcloud_error.log; } |
https配置文件,请删除原有的/usr/local/nginx/conf/vhost/demo.conf内容,将以下内容复制进来,并替换server_name的地址!
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 |
server { listen 80; server_name 192.168.50.194; return 301 https://$server_name:443$request_uri; } server { listen 443 ssl http2; ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem; ssl_certificate /usr/local/nginx/conf/ssl/xxx.pem; ssl_certificate_key /usr/local/nginx/conf/ssl/xxx.key; ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload"; add_header X-Content-Type-Options nosniff; add_header Referrer-Policy "no-referrer" 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; fastcgi_hide_header X-Powered-By; ssl_session_cache shared:SSL:10m; ssl_stapling on; ssl_stapling_verify on; resolver 223.5.5.5 223.6.6.6 valid=300s; resolver_timeout 5s; server_name 192.168.50.194; index index.html index.htm index.php; root html/nextcloud; location = /robots.txt { allow all; log_not_found off; access_log off; } location = /.well-known/carddav { return 301 $scheme://$host:$server_port/remote.php/dav; } location = /.well-known/caldav { return 301 $scheme://$host:$server_port/remote.php/dav; } location /.well-known/acme-challenge { } location / { rewrite ^ /index.php; } location ~ ^\/(?:build|tests|config|lib|3rdparty|templates|data)\/ { deny all; } location ~ ^\/(?:\.|autotest|occ|issue|indie|db_|console) { deny all; } location ~ ^\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+)\.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; # Avoid sending the security headers twice fastcgi_param modHeadersAvailable true; # Enable pretty urls fastcgi_param front_controller_active true; fastcgi_pass unix:/tmp/php-fpm74.sock; fastcgi_intercept_errors on; fastcgi_request_buffering off; } location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) { try_files $uri/ =404; index index.php; } location ~ \.(?:css|js|woff2?|svg|gif|map)$ { try_files $uri /index.php$request_uri; add_header Cache-Control "public, max-age=15778463"; 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; access_log off; } location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ { try_files $uri /index.php$request_uri; access_log off; } access_log /data/logs/nginx/nextcloud_access.log; error_log /data/logs/nginx/nextcloud_error.log; } |
6、重启nginx服务
1 2 |
nginx -t /etc/init.d/nginx restart |
7、配置php-fpm.conf
如果不配置此项,可能会导致Nextcloud安装警告:PHP的安装似乎不正确,无法访问系统环境变量。getenv("PATH") 函数测试返回了一个空值。
1 |
echo 'env[PATH] = /usr/local/bin:/usr/bin:/bin:/usr/local/php/bin' >> /usr/local/php/etc/php-fpm.conf |
8、启用php-opcache
1 2 3 4 5 6 7 8 |
echo 'zend_extension=opcache.so' >> /usr/local/php/etc/php.ini sed -i 's/;opcache.enable=1/opcache.enable=1/' /usr/local/php/etc/php.ini sed -i 's/;opcache.interned_strings_buffer=8/opcache.interned_strings_buffer=8/' /usr/local/php/etc/php.ini sed -i 's/;opcache.max_accelerated_files=10000/opcache.max_accelerated_files=10000/' /usr/local/php/etc/php.ini sed -i 's/;opcache.memory_consumption=128/opcache.memory_consumption=128/' /usr/local/php/etc/php.ini sed -i 's/;opcache.save_comments=1/opcache.save_comments=1/' /usr/local/php/etc/php.ini sed -i 's/;opcache.revalidate_freq=2/opcache.revalidate_freq=1/' /usr/local/php/etc/php.ini sed -i 's/memory_limit = 128M/memory_limit = 512M/' /usr/local/php/etc/php.ini |
9、重启php服务
1 2 |
/etc/init.d/php-fpm74 configtest /etc/init.d/php-fpm74 restart |
10、配置mysql
1 2 3 4 |
mysql -uroot -p mysql> set password = password('wlnmp'); mysql> create database nextcloud; mysql> quit |
11、下载nextcloud
官方下载的速度很慢,可以使用下面我本地提供的地址,点此捐赠服务器流量支出
1 2 3 4 5 |
cd /usr/local/nginx/html/ 官方下载:wget https://download.nextcloud.com/server/releases/nextcloud-18.0.4.zip 本地下载:wget http://down.whsir.com/downloads/nextcloud-18.0.4.zip yum install unzip unzip nextcloud-18.0.4.zip |
12、设置权限
1 |
chown -R www.www /usr/local/nginx/html/nextcloud |
13、安装Nextcloud
访问http://SERVER_NAME,请选择MySQL/MariaDB数据库,默认会在网站根目录下创建一个data目录用来存放数据,该目录可根据情况进行更改,我这里保持默认状态进行安装!
14、修复Nextcloud数据库
在服务器上执行以下命令
1 2 3 4 5 6 7 8 9 10 11 |
sudo -u www php /usr/local/nginx/html/nextcloud/occ db:add-missing-indices sudo -u www php /usr/local/nginx/html/nextcloud/occ db:convert-filecache-bigint Following columns will be updated: * mounts.storage_id * mounts.root_id * mounts.mount_id This can take up to hours, depending on the number of files in your instance! Continue with the conversion (y/n)? [n] y |
如果不执行以上命令,可能会在Nextcloud中出现安全警告,看到以下警告内容!
数据库丢失了一些索引。由于给大的数据表添加索引会耗费一些时间,因此程序没有自动对其进行修复。您可以在 Nextcloud 运行时通过命令行手动执行 "occ db:add-missing-indices" 命令修复丢失的索引。索引修复后会大大提高相应表的查询速度。
在数据表 "oc_calendarobjects_props" 中无法找到索引 "calendarobject_calid_index"。
在数据表 "oc_schedulingobjects" 中无法找到索引 "schedulobj_principuri_index"。
数据库中的一些列由于进行长整型转换而缺失。由于在较大的数据表重改变列类型会耗费一些时间,因此程序没有自动对其更改。您可以通过命令行手动执行 "occ db:convert-filecache-bigint" 命令以应用挂起的更改。该操作需要当整个实例变为离线状态后执行。查阅相关文档以获得更多详情。
mounts.storage_id
mounts.root_id
mounts.mount_id
15、缓存配置
如果不配置缓存,可能会看到以下警告:内存缓存未配置,为了提升使用体验,请尽量配置内存缓存。
Nextcloud官方提供了几种缓存配置方案,APCu、Memcached及Redis
- 如果你是小型/私人家庭用途,可以仅使用APCu进行缓存。
- 如果你是用于公司集群用途,可以使用APCu+redis方式进行缓存。
- memcached缓存方案,它不适合与事务性文件锁定一起使用(关于事务性文件锁定可以查看官方文档),因为它不存储锁,而且数据可以随时从缓存中消失。
因此官方建议的最佳缓存方案是redis
对于Redis和APCu内存缓存的说明:APCu在本地缓存中比Redis更快。如果您有足够的内存,请使用APCu进行内存缓存,并使用Redis进行文件锁定。如果内存不足,请同时使用Redis。
最佳缓存配置APCu+Redis(推荐)
安装redis及redis模块扩展,默认安装的redis密码为空,建议添加redis密码
1 |
yum install wphp74-redis wredis5 |
编辑config.php配置文件,增加以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 |
vi /usr/local/nginx/html/nextcloud/config/config.php ...... 'dbpassword' => '59iSA7XDJciDTXsmR4BU174k95Wijg', 'installed' => true, 'memcache.local' => '\OC\Memcache\APCu', 'memcache.locking' => '\OC\Memcache\Redis', 'memcache.distributed' => '\OC\Memcache\Redis', 'redis' => [ 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', ], ); |
纯Redis作为缓存
安装redis及redis模块扩展,默认安装的redis密码为空,建议添加密码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
yum install wphp74-redis wredis5 vi /usr/local/nginx/html/nextcloud/config/config.php ...... 'dbpassword' => '59iSA7XDJciDTXsmR4BU174k95Wijg', 'installed' => true, 'memcache.local' => '\OC\Memcache\Redis', 'memcache.locking' => '\OC\Memcache\Redis', 'memcache.distributed' => '\OC\Memcache\Redis', 'redis' => [ 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', ], ); |
仅使用APCu作为缓存
1 2 3 4 5 6 7 8 |
echo 'apc.enable_cli=1' >> /usr/local/php/etc/php.ini /etc/init.d/php-fpm74 restart vi /usr/local/nginx/html/nextcloud/config/config.php ...... 'dbpassword' => '59iSA7XDJciDTXsmR4BU174k95Wijg', 'installed' => true, 'memcache.local' => '\OC\Memcache\APCu', ); |
使用memcached作为缓存
APC用于本地缓存,将memcached作为分布式缓存
1 2 3 4 5 6 7 8 9 10 11 |
yum install wphp74-memcached wmemcached vi /usr/local/nginx/html/nextcloud/config/config.php ...... 'dbpassword' => '59iSA7XDJciDTXsmR4BU174k95Wijg', 'installed' => true, 'memcache.local' => '\OC\Memcache\APCu', 'memcache.distributed' => '\OC\Memcache\Memcached', 'memcached_servers' => [ [ '127.0.0.1', 11211 ], ], ); |
至此基于wlnmp一键包安装Nextcloud网盘完成
PS:大文件上传失败,可以更改nginx和php参数限制,参考官方文档