MENU

【随便折腾】Nextcloud私有云搭建

August 28, 2022 • 代码

忙完了一段时间,看着自己几乎闲置服务器,遂决定继续利用起来,搭建一个私有云。搭建网盘的主要原因是本人并不喜欢目前国内主流网盘的审查制度,另外由于本人丢三落四的习惯,急需一个完整的文件管理平台来帮助我进行文件的保存和管理以及云同步的功能。

目前我使用的服务器是腾讯云 2 核 4G 的 centos7 版本,并使用了 nginx 作为反代,服务器的二级域名及其证书也已经申请好,如果你对申请 ssl 证书还抱有疑问,这篇文章也许可以帮到你[【歪门邪道】重新签发网站 SSL 证书]()。值得一提的是,我的服务器上已经有了 mysql,所以我并没有写安装数据库的步骤。并且我也建议:如果你的电脑已经有了 mysql,尽量不要按照网上的教程安装别的数据库,直接进行安装即可,如果没有也尽量优先安 mysql,你会发现 mysql 在以后的服务器任务中也可以经常用到而不会出现其他问题。

添加 epel 仓库

有很多软件位于 EPEL 仓库中, 而默认情况下安装的 CentOS 中没有该仓库, 因此需要自己手动添加.

$ sudo yum -y install epel-release

添加 Webtatic 仓库

php7-fpm 依赖需要这个仓库

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装配置 PHP7-FPM

执行以下命令:

$ sudo yum -y install php70w-fpm php70w-cli php70w-gd php70w-mcrypt php70w-mysql php70w-pear php7

安装完以后可以使用 php -v 来检查下是否正常安装

接下来开始配置 PHP7-FPM

$ sudo vi /etc/php-fpm.d/www.conf

确保以下内容的存在:

; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

; Note: This value is mandatory.
listen = 127.0.0.1:9000

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

上面的语句简单来说就是把 PHP7-FPM 和 nginx 相关联

/var/lib/ 目录下新建文件夹 session, 拥有者改为 ngnix

$ mkdir -p /var/lib/php/session
$ chown nginx:nginx -R /var/lib/php/session/

启动 PHP-FPM 和 Nginx,并设置为随开机启动服务

$ sudo systemctl start php-fpm
$ sudo systemctl start nginx
$ sudo systemctl enable php-fpm
$ sudo systemctl enable nginx

NextCloud 下载

  1. 安装 wgetunzip

    $ yum -y install wget unzip
  2. 下载与验证 NextCloud

    $ cd ~/
    $ wget https://download.nextcloud.com/server/releases/nextcloud-14.0.4.zip
    $ wget https://download.nextcloud.com/server/releases/nextcloud-14.0.4.zip.sha256
    $ sha256sum -c nextcloud-14.0.4.zip.sha256 < nextcloud-14.0.4.zip
  3. 解压并将 NextCloud 剪切到 /usr/share/nginx/html/ 目录下

    $ unzip nextcloud-14.0.4.zip
    $ sudo cp -R nextcloud/ /usr/share/nginx/html/
  4. 新建 data 文件夹, 并变更 nextcloud 所有者为 nginx

    $ cd /usr/share/nginx/html/
    $ sudo mkdir -p nextcloud/data/
    $ chown nginx:nginx -R nextcloud/

配置 NextCloud

配置主要是配置 nginx 的反向代理了,这里我申请了一个二级域名,conf 文件示例如下:

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

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name cloud.slagworld.com;

    # Use Mozilla's guidelines for SSL/TLS settings
    # https://mozilla.github.io/server-side-tls/ssl-config-generator/
    # NOTE: some settings below might be redundant
    ssl_certificate     你的pem证书
        ssl_certificate_key 你的key证书
        ssl_session_timeout 5m;
        ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
        ssl_prefer_server_ciphers on;  
        # 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;
    add_header Referrer-Policy no-referrer;

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

    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$request_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 /usr/share/nginx/html$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|woff2?|svg|gif)$ {
        try_files $uri /index.php$request_uri;
        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;
        add_header Referrer-Policy no-referrer;

        # Optional: Don't log access to assets
        access_log off;
    }

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

至此配置完成,剩下的网上的教程应该很全面了