搭建 NextCloud 服务器

之前折腾那些个 Low End VPS 的时候,弄到过一款 CentexHosting 的大硬盘的VPS,然后正好最近折腾有需求,就准备搭一个私有云在上面。一般来说,碰到私有云第一反应可能都会是 OwnCloud / SeaFile,不过我为什么要用 NextCloud 呢,还是因为我比较懒,NextCloud 有在线一键安装包,就不用麻烦去折腾配置了。

整体安装还是比较简单的,首先使用 LinuxEye 家的 LNMP 安装包,添加一个 nginx 虚拟主机。在这里不得不佩服一下这个一键包的作者,几乎所有的事情都可以通过他提供的脚本完成,比如我需要使用 Let’s Encrypt 签发 SSL 证书,只要用他的 addon.sh 安装 certbot 以后,在 vhost.sh 添加虚拟主机时就可以自动签发对应的证书,同时也配置好了自动续期的功能,如果有读者有类似需求,我在这里强烈推荐使用这个一键包,可以解决很多问题。

接下来就在 NextCloud 官网上下载 Web-Installer,放在虚拟主机对应的数据文件夹中,使用 phpMyAdmin 创建供私有云使用的用户和数据库,授予权限之后,填写在安装页面就可以了。

#安装过程不算特别顺利,NextCloud 似乎无法和 LNMP 一键包默认安装的 Zend OPcache 共存(报错“PHP 被设置为移除内联块, 这将导致多个核心应用无法访问”),需要使用 memcache 进行缓存。如何关掉 Zend OPcache也费了我一番周折,最后在 /usr/local/php/etc/php.d/ 目录下找到了它的配置文件,opcache.enable=0 关掉了缓存。不过这个仍然存在一定的问题,因为 NextCloud 还是需要缓存的,这点先按下不表。

  • Update:关于这个问题,其实还是可以使用 Zend OPcache 的,需要将 /usr/local/php/etc/php.d/ext-opcache.ini (oneinstack专用)中改为 opcache.save_comments=1 ,因为默认是0,改完重启 php-fpm 即可。

安装完成之后,却出现了 index.php 被跳转到 index.php/app/files 并且找不到文件的问题,Google 一下之后,官方论坛有人提到可能是 nginx 配置的问题,于是我就去查看了官方建议配置,但是却出现了 502 Bad Gateway 的问题。经查询,这种一般出现于 nginx 和 php-fpm 进程通信之间,观察了官方的配置文件之后,发现了这样一段:

upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php5-fpm.sock;
}

我突然意识到,官方的配置文档是针对 php5 制作的,而我安装的是 php7,可能问题就出现在这里。于是试着换成了 /usr/local/nginx/conf/nginx.conf 中的 php 用法:

upstream php-handler {
     #server 127.0.0.1:9000;
     #server unix:/var/run/php5-fpm.sock;
     server unix:/dev/shm/php-cgi.sock;
}

#在执行了 service nginx reload 之后,神奇地可以用了,我也暂时也搞不清这是为什么,能正常运行了也是一件好事。

  • Update:关于这个跳转的问题,我认为是 nginx 配置 rewrite 规则的问题,如果需要解决这样的问题,应该严格按照官方建议 nginx 配置中的内容调整 location 开头的那些配置行。如果需要解决安装后 Nextcloud 有关 http header 问题的报错,则需要添加官方建议配置中的 add-header。这些在 Linuxeye 的一键包生成的配置中都没有解决,需要手动添加。

以下列出官方建议配置:

upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php5-fpm.sock;
}

server {
    listen 80;
    server_name cloud.example.com;
    # enforce https
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    server_name cloud.example.com;

    ssl_certificate /etc/ssl/nginx/cloud.example.com.crt;
    ssl_certificate_key /etc/ssl/nginx/cloud.example.com.key;

    # Add headers to serve security related headers
    # Before enabling Strict-Transport-Security headers please read into this
    # topic first.
    # add_header Strict-Transport-Security "max-age=15768000;
    # includeSubDomains; preload;";
    #
    # 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 X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header X-Download-Options noopen;
    add_header X-Permitted-Cross-Domain-Policies none;

    # Path to the root of your installation
    root /var/www/nextcloud/;

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # The following 2 rules are only needed for the user_webfinger app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json
    # last;

    location = /.well-known/carddav {
      return 301 $scheme://$host/remote.php/dav;
    }
    location = /.well-known/caldav {
      return 301 $scheme://$host/remote.php/dav;
    }

    # 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;

    # Uncomment if your server is build with the ngx_pagespeed module
    # This module is currently not supported.
    #pagespeed off;

    location / {
        rewrite ^ /index.php$uri;
    }

    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/.+|ocs-provider/.+)\.php(?:$|/) {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param HTTPS on;
        #Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        fastcgi_param front_controller_active true;
        fastcgi_pass php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ ^/(?:updater|ocs-provider)(?:$|/) {
        try_files $uri/ =404;
        index index.php;
    }

    # Adding the cache control header for js and css files
    # Make sure it is BELOW the PHP block
    location ~ \.(?:css|js|woff|svg|gif)$ {
        try_files $uri /index.php$uri$is_args$args;
        add_header Cache-Control "public, max-age=15778463";
        # Add headers to serve security related headers (It is intended to
        # have those duplicated to the ones above)
        # Before enabling Strict-Transport-Security headers please read into
        # this topic first.
        # add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
        #
        # 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 X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Robots-Tag none;
        add_header X-Download-Options noopen;
        add_header X-Permitted-Cross-Domain-Policies none;
        # Optional: Don't log access to assets
        access_log off;
    }

    location ~ \.(?:png|html|ttf|ico|jpg|jpeg)$ {
        try_files $uri /index.php$uri$is_args$args;
        # Optional: Don't log access to other assets
        access_log off;
    }
}

不过,NextCloud 服务端报告目前仍然存在一定的问题:

  • PHP模块’文件信息’丢失. 我们强烈建议启用此模块以便mime类型检测取得最佳结果.
  • “X-Frame-Options” HTTP 头部没有配置和 “SAMEORIGIN” 的一样。这是一个潜在的安全或者隐私风险,我们调整
  • 内存缓存未配置。如果可用,请配置 memcache 来增强性能。更多信息请查看我们的文档 。

之后我再考虑如何解决,解决了之后也会在这里更新。

Update: Memcached 配置

根据官方文档,我选择 redis 作为 cache 模块,在 <site root>/config/config.php 中添加如下字段即可:

'memcache.local' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => array(
   'host' => 'localhost',
   'port' => 6379,
),

Update2:优化 php-fpm 内存占用

pm = ondemand
pm.max_children = 16
pm.start_servers = 11
pm.min_spare_servers = 8
pm.max_spare_servers = 16
pm.max_requests = 2048
pm.process_idle_timeout = 10s
request_terminate_timeout = 120
request_slowlog_timeout = 0

dd关于这个问题,如果 pm 配置为 dynamic 或者 static,php-fpm 会维护一个进程池,导致消耗大量内存,在小内存的 VPS 上,我们可以配置为 ondemand,就不需要那些额外开销了。

搭建 NextCloud 服务器》有一个想法

回复 Eve Hunt 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注