手动安装nextcloud

新入手了VPS后,一直琢磨着倒腾个网盘,工作用。 从seafile、owncloud到其它一系列的尝试了不少,最后决定使用nextcloud。被这个nextcloud折磨了三四天,我都有点佩服自己的毅力了。

趟过的坑

安装LNMP环境

参考ubuntu手动安装LNMP环境完成安装。

配置nginx并安装SSL证书

新建文件夹,用来存放网站文件:

mkdir /var/www/file.zimohan.com

在nginx配置文件所在位置(/etc/nginx/sites-enabled/)下新建网站的配置文件(file.zimohan.com.conf):

vi file.zimohan.com.conf

粘贴如下内容:

server {
        root /var/www/file.zimohan.com;
        index index.html index.htm index.nginx-debian.html;
        server_name zimohan.com;
        location / {
                try_files $uri $uri/ =404;
        }
}

这是一个基本的nginx网站配置文件,完成后使用nginx -s reload使刚才新建的配置文件生效,然后输入我们的域名,应该就能正常访问了。
接着添加SSL证书,这一步很重要,我就是因为忽略了这一步,结果折腾了两三天。参考免费的HTTPS证书-Let's Encrypt添加免费的SSL证书。

修改nginx配置文件

要成功运行nextcloud,需要对nginx网站的配置文件做些改动,官方也有推荐的配置,我这里对官方的配置文件做了些微改动:

server {
    listen 80;
    listen [::]:80;
    server_name file.zimohan.com; # 域名
    return 301 https://$server_name:443$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name file.zimohan.com;

    ssl_certificate /etc/letsencrypt/live/file.zimohan.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/file.zimohan.com/privkey.pem;
    # ssl证书的位置

    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;

    root /var/www/file.zimohan.com;

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

    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    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;

    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;
        fastcgi_param modHeadersAvailable true;
        fastcgi_param front_controller_active true;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; #这里对官方的配置做了改动,将upstream php-handler移动到了这里
        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;
    }
}

下载并安装nextcloud

剩下的就是下载安装报并安装了,其实到这里已经很简单了,因为前面我们做的铺垫工作已经到位。
由于我这里是国内的vps,服务器端下载安装包很慢,所以到官网下载安装包再上传到我们的服务器。解压后得到nextcloud文件夹,将文件夹内的文件复制到网站所在目录(file.zimohan.com),或者干脆删除该目录,将nextcloud重命名为file.zimohan.com

mv nextcloud file.zimohan.com

同时,还需要注意权限问题。赋予网站文件夹相应的权限,具体权限要给到啥程度才行,我也不知道,先给个777权限跑起来再说:

chmod -R 777 /var/www/file.zimohan.com

接下来就是见证奇迹的时刻了,访问我们的域名,开始安装。过程很简单,输入账号密码,配置好数据库的用户名和密码,稍等片刻就转到登录地址,登录使用吧。