使用NextCloud 12 搭建私人云服务和问题解决

简介

NextCloud 是一款开源网络硬盘系统,最新版本是12,NextCloud 源代码完全开放,你可以在开源许可协议的约束下免费使用,对于需要专业支持的用户可以购买 NextCloud 官方的专业版订阅服务。同时它还支持Android和IOS客户端,方便同步。
详情参见:NextCloud官网

前提

要搭建云服务,请确保你已经有一台VPS。因为要配置服务器,你还需要有一个域名,这些你都可在阿里云腾讯云进行购买。

VPS系统

Ubuntu 16.0.4

搭建方式

搭建NextCloud有两种方式,一是以Docker方式安装,另一种以php环境下安装,本文以php方式安装,以Docker方式安装参见Docker安装私有云盘NextCloud过程记录,下面开始安装。

安装依赖

服务器

这里我们使用nginx作为服务器,通过以下命令安装nginx

apt-get install nginx

数据库

NextCloud可用的数据库有MySQL/MariaDB,PostgreSQL,Oracle。官方推荐MySQL/MariaDB,这里以mysql为例,执行以下指令安装mysql:

apt-get install mysql-server
mysql_secure_installation

期间会让你设置root密码和密码强度,请自行判断。

运行环境要求

执行下面的命令安装php

apt-get install php

官方要求php5.0+,通过以下指令查看php版本

php -v

这里推荐使用php7.0,有更高的性能表现。

然后安装NextCloud所需的其它php依赖

apt-get install php-zip
apt-get install php-dompdf
apt-get install php-xml
apt-get install php-mbstring
apt-get install php-curl
apt-get install php-mysql

这样环境就安装完毕了。

安装NextCloud

进入安装目录/var/www下,通过下面的命令下载NextCloud 12并解压:

wget https://download.nextcloud.com/server/releases/nextcloud-12.0.3.zip
unzip nextcloud-*.zip

配置数据库

NextCloud需要一个数据库保存数据,这里以Mysql为例创建数据库,其它数据库相似,

进入mysql命令界面:

mysql -u root -p

创建数据库

create database your_db;

再为NextCloud创建一个数据库用户:

create user 'your_username'@'localhost' identified by 'your_passwd'

其中your_username是用户名,localhost指明只能通过本地访问。要想通过远程访问可改为remote同时配置你的mysql访问策略。your_passwd即所对应的密码。
如果遇到Your password does not satisfy the current policy requirements 问题,这是因为你的密码强度级别设置太高,通过set global validate_password_policy=0可以设置为最低级别,关于密码强度的说明请参考百度。

为所创建的用户授予权限:

grant all privileges on your_db.* to 'your_username'@'localhost' identified by 'your_passwd';
flush privileges;
quit

到此数据库的部分已经完成了。

配置Nginx服务器

进入/etc/nginx/sites-available/目录下

cd /etc/nginx/sites-available/

创建一个文件cloud(文件名任意,可读性好,最好为你的域名就行),

touch cloud

进入/etc/nginx/sites-enabled/目录

cd /etc/nginx/sites-enabled/

执行以下命令创建链接

ln -s ../sites-available/cloud cloud

编辑cloud

vi cloud

在NextCoud的配置官方文档中可以找到nginx服务器的配置。复制到cloud中,需要修改的部分已用中文注释

upstream php-handler {
    #server 127.0.0.1:9000;
    server  unix:/var/run/php/php7.0-fpm.sock; #使用sock加速磁盘访问
}

#若使用https,取消下面这段注释
#server {
#    listen 80;
#    server_name cloud.example.com; #将cloud.example.com替换为你的域名
#    # enforce https
#    return 301 https://$server_name$request_uri;
#}

server {
    #listen 443 ssl http2; #若使用https,取消本行注释,同时注释下面这行
    listen 80; 
    server_name cloud.example.com; #将cloud.example.com替换为你的域名

    #若使用https,取消注释下面两行
    #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; # 若使用https取消这行注释
        #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;
    }
}

至此服务器已经配置完成。
重启服务器:

systemctl reload nginx.service

打开浏览器,输入你的域名,若一切正常,就可以看到安装界面了,若仍有依赖未安装,按照提示进行安装即可。

可能遇到的问题

  • 你没有安装php-*依赖

    解:请确保你已经安装完上面的所有php依赖

  • 安装后打开浏览器出现502异常

    解:这是由于你解压nextcloud压缩文件时是作为root(或其它)用户进行操作的,而php使用的用户默认为www-data,你需要更改文件所有者:

    cd /var/www
    chown -R www-data:www-data nextcloud/
    

管理界面出现安全警告:

  • PHP 的设置似乎有问题, 无法获取系统环境变量. 使用 getenv("PATH") 测试时仅返回空结果

    解:取消/etc/php/7.0/fpm/pool.d/www.conf中这几行的注释

    ;env[HOSTNAME] = $HOSTNAME
    ;env[PATH] = /usr/local/bin:/usr/bin:/bin
    ;env[TMP] = /tmp
    ;env[TMPDIR] = /tmp
    ;env[TEMP] = /tmp
    
  • 内存缓存未配置

    解:请查看性能优化一节。

性能优化

nextcloud支持内存加速,它可支持3种方式APCu,Memcached,Redis。这里只展示APCu的配置,其它方式可参考官方配置

配置APCu步骤

安装apcu

sudo apt-get update
sudo apt-get install php-apcu -y

打开/var/www/nextcloud/config/config.php文件,添加下面这行

'memcache.local' => '\OC\Memcache\APCu',

最终的config.php类似这样

<?php
$CONFIG = array (
  'instanceid' => '',
  'passwordsalt' => '',
  'secret' => '',
  'trusted_domains' =>
  array (
    0 => 'cloud.host.com',
  ),
  'datadirectory' => '/var/www/nextcloud/data',
  'overwrite.cli.url' => 'https://cloud.host.com',
  'dbtype' => 'mysql',
  'version' => '12.0.3.3',
  'dbname' => 'nextcloud_db',
  'dbhost' => 'localhost:3306',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'nextcloud',
  'dbpassword' => '*_mycloud',
  'installed' => true,
  'memcache.local' => '\OC\Memcache\APCu', #添加到这里
);

重启php7.0-fpm服务

sudo systemctl reload php7.0-fpm.service

重启服务器

sudo systemctl reload nginx.service

若此时进入管理界面,没有任何安全警告,恭喜你,你以完成nextcloud的搭建工作,nextcloud还拥有大量的插件协助你的工作,敬请发现吧!

配置HTTPS

在前面的配置中,并没有启用https,通过https的方式访问网站,别人窃取你密码的机会将会大大减少。实现HTTPS访问需要SSL证书,但是SSL证书一般都需要购买,好在有免费开源的let's encrypt证书可用,let's encrypt的配置一般相当麻烦,正因为此certbot诞生了,通过这款开源工具,你可以很快安装好SSL证书。下面开始SSL的安装:

安装cerbot

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

自动安装SSL

certbot

cerbot会自动识别你的服务器和域名,请根据提示进行操作,安装期间它会告诉你安装好的证书所在位置。
这里需要注意的是let's encrypt为了让所有人都能享受到证书服务,你的子域名每周只能重复申请3-5次证书,主域名一旦申请超过3次就会被限制申请,只有等5天后才能再次申请。所以申请好的证书请保管好,没有其它问题不要经常申请。详情参见官方说明频率限制

现在解除掉/etc/nginx/sites-enabled/cloud文件中关于https的限制,最终的cloud文件如下所示:

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

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

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot

}

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;
    }
ssl_certificate /etc/letsencrypt/live/cloud.manlier.top/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/cloud.manlier.top/privkey.pem; # managed by Certbot
}

你也可以看到certbot帮你修改的部分

最后重启你的服务器

systemctl reload nginx.service

如果你使用类谷歌浏览器,网址左侧应该会显示一把小绿锁,表示你成功配置好了https服务

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,099评论 18 139
  • 一、MemCache简介 session MemCache是一个自由、源码开放、高性能、分布式的分布式内存对象缓存...
    李伟铭MIng阅读 3,649评论 2 13
  • 系统环境 所需软件官方下载地址: 一、 安装开发包环境: 二、 关闭iptables和Selinux(生产...
    莫名其妙的一生阅读 1,208评论 0 4
  • 远程控制你的服务器 远程控制 Linux 类型的系统的服务器,比如 CentOS 系统的服务器,一般不像 Wind...
    Mamba_Liao阅读 713评论 0 0
  • Composer Repositories Composer源 Firegento - Magento模块Comp...
    零一间阅读 3,940评论 1 66