找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 50150|回复: 106

[k3] 云盘都快关完了,自己用K3打造一个私有云吧~

 火.. [复制链接]
发表于 2017-8-4 16:04 | 显示全部楼层 |阅读模式
本帖最后由 ouchyoung 于 2018-3-22 09:43 编辑

感谢~~咳咳~~的相关规定,我们的云盘一个接一个地倒下了,数据来回倒腾实在是太累,与其这样,不如自己建个私有云吧。其实建私有云的方法很多,也有很多简单的,比如QNAP、群晖神马的,可是价格不便宜。现在K3可以上车(祈祷不要翻车),赶上头班车的还有一个硬盘赠送,不赶上的,也只需要自己准备一个移动硬盘(想要高大上一些,准备一个NAS盘+USB to SATA的盒子就行),价格上可便宜多了。建议把硬盘格式化为ext3的格式(NTFS应该也可以,不过没试过)

好了,废话不多说,先说说用K3搭建私有云需要准备的东西:

K3的内存有512M,算是很大了,比好多NAS都大。所以,我们可以考虑服务比较全面的私有云。我选择了owncloud,除了有网页版的管理外,还有同步客户端,有webdav服务,还可以在软件里加装很多插件,实现word的多人在线编辑之类的工作。

owncloud基于PHP编写,所以需要搭建一个PHP的环境。512M的内存看似不小(其实我之前在我128M的NAS上面也跑了owncloud,能实现功能,不过性能就不行了,跟K3比,差远了),但考虑到如果有数目众多的文件,或者有多个客户端同时同步的话,还是省着用。本来想选择lighttpd,但是对owncloud支持不好。所以最终选择了LEMP环境。

要搭建LEMP,先得root,装上entware。具体方法我也是看的坛子里tianbaoha的教程:https://www.right.com.cn/forum/thread-212725-1-1.html,在此表示感谢。接下来的软件全在entware里有了。


先写到这里,回头再继续。

==================我是第二天编辑的分割线,要跟大家抱个歉,我第一次写的时候没写完,等到后面编辑时,发现让验证邮箱,死活收不到验证邮件,直到现在才成功=======================

转入正题,部署LEMP。因为不确定原来是否安装了entware中的东西,是否会有冲突,建议全新安装。如果已经安装了东西,又想试试owncloud,试过后发现失败,可以用opt install local还个原,再试。
先说一下我的路由器设置,wan口用过pppoe和自动获取、固定IP,都无所谓。LAN口我设置的是192.168.3.1,下面都是基于这个IP设置的。

一、建立一系列文件夹

      因为涉及到的软件多,同时闪存的擦写是有限的,建议把一些文件挪到USB的硬盘上,其实对速度影响并不大:
1. 切换到移动硬盘。一般位于/tmp/share/sda1。命令是:cd /tmp/share/sda1


2. 建立opt文件夹:mkdir opt

3. 把原来NAND中的opt目录下已有的、且需要的文件移过来(也可以把opt文件夹整个拷过来,但不建议。拷上面列出的子文件夹够用,且不会对速度产生太大影响):
cp -R /opt/bin /tmp/share/sda1/opt
cp -R /opt/etc /tmp/share/sda1/opt
cp -R /opt/lib /tmp/share/sda1/opt
cp -R /opt/sbin /tmp/share/sda1/opt
cp -R /opt/share /tmp/share/sda1/opt
cp -R /opt/tmp /tmp/share/sda1/opt
cp -R /opt/usr /tmp/share/sda1/opt
cp -R /opt/var /tmp/share/sda1/opt

4. 删除原位置的上述目录:
cd /opt
rm -R bin etc lib sbin share tmp usr var

5. 符号链接刚刚拷过去的目录回去(为避免出错,建议一行一行命令进行):
ln -s /tmp/share/sda1/opt/bin /opt/
ln -s /tmp/share/sda1/opt/sbin /opt/
ln -s /tmp/share/sda1/opt/etc /opt/
ln -s /tmp/share/sda1/opt/lib /opt/
ln -s /tmp/share/sda1/opt/share /opt/
ln -s /tmp/share/sda1/opt/tmp /opt/
ln -s /tmp/share/sda1/opt/usr /opt/
ln -s /tmp/share/sda1/opt/var /opt/


二、安装软件
owncloud涉及许多php扩展,考虑内存和CPU性能,还有运行的稳定性与兼容性,用的是nginx+php-fpm+mariadb,安装需要的软件:
opkg install bzip2 zlib icu mariadb-client-extra mariadb-server nginx-extras php7 php7-cgi php7-fpm php7-mod-ctype php7-mod-curl php7-mod-dom php7-mod-exif php7-mod-fileinfo php7-mod-gd php7-mod-hash php7-mod-iconv php7-mod-intl php7-mod-json php7-mod-mbstring php7-mod-mcrypt php7-mod-mysqli php7-mod-opcache php7-mod-openssl php7-mod-pdo php7-mod-pdo-mysql php7-mod-session php7-mod-simplexml php7-mod-xml php7-mod-xmlreader php7-mod-xmlwriter php7-mod-zip php7-pecl-dio openssl-util
是的,就是要装这么多……而且这里面还没有owncloud呢!!这上面的软件不保证全部拼写正确(拷贝的时候有删除,也许有错。所以安装的时候要看看提示有没有报错)。如果报错的话,看看是哪个软件,上官网去找类似名字的,再opkg install它。


三、配置环境

如果对命令行不熟悉,可以通过网上邻居进行。方法是打开我的电脑,在地址栏输入\\192.168.3.1,回车,然后会看到sda1,双击进去,就到了移动硬盘上了,如果前面做了符号链接,我们装的软件应该全在这里。

1. 配置mariadb
进入opt\etc\mysql,双击打开my.cnf,询问用什么程序时,找到写字板(也可以用记事本,建议写字板,格式更好)。修改以下参数:

[client-server]

socket = /opt/tmp/mysql.sock (其实这个路径可以自己设置,只要后面相应的位置统一就好)

[mysqld]
user = 0 (因为用admin ssh进去后进行操作的用户为0,很奇怪,如果你的不是,就改成你自己的用户名)
pid-file        = /opt/var/run/mariadb.pid
basedir                = /opt
lc_messages_dir        = /opt/share/mysql
lc_messages        = en_US
transaction-isolation = READ-COMMITTED

datadir                = /opt/var/mysql/

tmpdir                = /opt/tmp/


查找一下binlog_format,看看是不是mixed,不是的话要修改成mixed:binlog_format = mixed

回到ssh中,进行数据库安装,命令为:mysql_install_db --force,
启动mariadb服务:/opt/etc/init.d/S70mariadbd start
设置数据库root密码:/opt/bin/mysqladmin password 你想设置的密码
连接数据库服务:mysql -u root -p,按提示输入刚刚设置的密码,进入mysql管理界面
建立数据库:CREATE DATABASE `owncloud` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;(没看错,有分号,这是mysql的语法,是要输入的。owncloud是数据库名,可以自己改,不过要记下来,后面的相应位置用同样名称)
建立一个非root用户:CREATE USER `你想要的用户名`@'localhost' IDENTIFIED BY '你想要的密码';(引号、分号都是要有的)
确定用户对数据库的权限:GRANT ALL PRIVILEGES ON `owncloud`.* TO `你刚刚设置的用户名`@`localhost`;
退出mysql管理界面:\q

重新启动mariadb:/opt/etc/init.d/S70mariadbd restart

2. 设置php-fpm

在etc目录下,找到php7-fpm,复制一份,重命名为php-fpm,注意不要改扩展名。

在FPM Configuration中,更改include行:include=/opt/etc/php7-fpm.d/*.conf,保存


进入php7-fpm.d目录,里面有个www文件,编辑它:

在[www]中,把user改为www:user = www
listen = /opt/tmp/php7-fpm.sock(位置可以自己定,但后面需要统一)
listen.owner = www
listen.group = www
listen.mode = 0666
pm.max_children = 9 (这个值根据需要可以调整,原始的是5,有点小了,稍微多点文件上传,会变慢)

env[PATH] = /usr/local/bin:/usr/bin:/bin:/opt/www:/opt/share/nginx/html:/opt/usr/lib:/opt/usr/share:/opt/bin:/opt/sbin

php_value[session.save_path]  = /opt/var/php-fpm/session (并去掉前面的分号注释符,同时在ssh窗口中用mkdir命令建立相应的目录:mkdir /opt/var/php-fpm,然后再mkdir /opt/var/php-fpm/session。)

更改权限:chown -R www:www /opt/var/php-fpm/session
保存退出。

在etc目录下,打开php.ini,在[php]块中,设置open_basedir,先去掉前方的注释分号,将其改为:
open_basedir = /opt/www:/opt/share/nginx/html:/tmp:/opt/www/ (这里的目录是允许php程序访问的目录,是为了安全性着想设置的,但在稍后设置owncloud时会警告,按自己的需要看是否设置,以及要设置哪些目录)
设置网站根目录:doc_root = "/opt/www"
保存退出。

回到ssh窗口,运行一次php-fpm -t,看看有没有报错。如果正常,应该报告configuration file /opt/etc/php-fpm.conf test is successful,如果有报错,会说是第几行错了。

不报错的话,就可以启动php-fpm了:/opt/etc/init.d/S79php7-fpm start

3. 设置nginx

首先是生成证书(如果有域名,可以用let's encryption,没有这个条件的,就自己生成ssl需要的文件,只是在后期访问会有警告):
mkdir /opt/etc/ssl
mkdir /opt/etc/ssl/nginx
cd /opt/etc/ssl/nginx
/opt/bin/openssl genrsa -des3 -out server.key 1024 会提示输入密码,自己建立一个密码输入两遍
/opt/bin/openssl rsa -in server.key -out server.key 会提示输入密码,把刚刚的密码输进去
/opt/bin/openssl req -new -x509 -key server.key -out ca.crt 会提示输入国家、城市、组织等等很多信息,随便输入就可以了,反正不给别人用。

然后在网上邻居的窗口中,找到sda1\opt\etc下的nginx.conf文件,用写字版打开它,把里面的内容全删掉,替换成下面的(不确定恩山会不会乱格式,会直接传一份文件上来):

user  www;
worker_processes  1;

#error_log  /opt/var/log/nginx/error.log;
#error_log  /opt/var/log/nginx/error.log  notice;
#error_log  /opt/var/log/nginx/error.log  info;

#pid        /opt/var/run/nginx.pid;


events {
    worker_connections  64;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  /opt/var/log/nginx/access.log main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream php-handler {
    #server 127.0.0.1:9000;
    server unix:/opt/tmp/php7-fpm.sock;
}

server {
    listen 81;
    server_name 192.168.3.1;
    # enforce https
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    server_name 192.168.3.1;

    ssl_certificate /opt/etc/ssl/nginx/ca.crt;
    ssl_certificate_key /opt/etc/ssl/nginx/server.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 /opt/www/;

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

在这个文件中,server_name需要改成你的路由器在局网地址,我的是192.168.3.1,端口需要改成80以外的,因为80被路由器自己的web管理占用了。我用的81,可以根据自己喜欢选择,反正最后也没啥用……只要不是80、21之类的端口,能让nginx跑起来就行。

保存退出,然后在ssh窗口中,/opt/etc/init.d/S80nginx start

5. 安装owncloud:

用浏览器打开https://owncloud.org/install/,点击“Get ownCloud Server”下的download(目前版本的直接链接是:https://download.owncloud.org/community/owncloud-10.0.2.zip),按提示下载。
下载的文件不要解压,直接通过网上邻居拷贝到sda1\opt之中。
回到ssh窗口,进入刚刚拷好owncloud文件的地方:cd /tmp/share/sda1/opt
解压owncloud文件:unzip owncloud-10.0.2.zip(要根据你下载的文件的名字,或者你是否有重命名决定upzip命令后的文件名)
解压完成后,更改解压出来的目录名为www:mv owncloud www(也可以在网上邻居中操作)
更改所有权:chown -R www:www www

将目录映射到/opt/www:ln -S /tmp/share/sda1/opt/www /opt/

四、设置owncloud服务器
用浏览器访问192.168.3.1:81(也可直接访问https://192.168.3.1),进入owncloud设置向导
按自己喜欢设置管理员账户密码,下方的服务器要填刚才设置mariadb的那个用户名和密码,数据库的名字,在我的文件中是设置的owncloud。点击完成,如果不报错,就成功了,随后会自动跳转到owncloud登录界面,用管理员账户登录后,进入文件管理界面。右上角有管理,可以设置服务器的很多参数,安装程序等。在owncloud官网上,有客户端的下载(windows/macOS/iOS/Andriod都有),第一次登录也会弹出客户端的下载页面。

最后,回答一些问题:

1. 没有公网IP:有几个解决办法,可以用公云(pubyun)、花生壳等的内网套餐,实现内网穿透。也可以换运营商,或者威胁换运营商,要求给公网IP或者给一个端口映射(只需要映射一个端口到你的443上即可。一般端口映射,如果能找到管理你们片区的技术经理,是有可能解决的)。还有就是学习重庆网友,闹上法庭打官司:https://www.cqsq.com/read/7095405

2. 替代NAS的问题:不是一个功能的东西,得到的服务肯定不一样。NAS是专门用于文件的服务器,当然更好用。不过就我目前在K3上运行的效果来看,稳定性和性能还过得去。而且要root路由器来当NAS用,乐趣不就在折腾么。

3. 私有云被关的问题:基本不可能。私有云并不代表要发布在公网上,只不过发到公网上会更方便应用而已,那样到哪里都能够访问自己的文件。但私有云是关不掉的,大不了放在家里自己用,起码电脑、手机、平板、电视上的内容在家就全都可以串流了,不需要东找西找再拷贝,可以实现集中式贮存。在外面拍的照片,回到家连上wifi,手机也就自动传到私有云上备份了,然后电脑上也就可以直接看到了。还是有用途的。

4. 快餐包:港真,我也是newbie to linux,快餐包不会做,有大神会的话,就麻烦做一个了。




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

点评

大神,你这个是官方固件还是lede里面装?  发表于 2017-9-8 13:45

评分

参与人数 5恩山币 +6 收起 理由
kesunji + 2 这个是官改可以装的吧,速度如何?
cyqian86 + 1 我来恩山就是为了撩你!
小白新上车 + 1 看不懂,看起来很牛逼
jeffrey_520 + 1 真是很牛
thange + 1 没有公网很烦神,怎么破

查看全部评分

我的恩山、我的无线 The best wifi forum is right here.
发表于 2017-8-4 16:07 | 显示全部楼层
沙发。。。。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 16:09 来自手机 | 显示全部楼层
期待继续......
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 16:16 来自手机 | 显示全部楼层
太监贴?......
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 16:19 来自手机 | 显示全部楼层
期待继续........
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 16:25 来自手机 | 显示全部楼层
MK一下、期待更新
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 16:26 | 显示全部楼层
期待楼主后续力作~
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 16:34 | 显示全部楼层
接下来的软件全在entware里有了。<-----这样的结尾还真有趣。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 16:48 | 显示全部楼层
前提是:需要一个稳定的LNMP环境。反正H大的固件里面内置的这个PHP环境,在经常断电的路由器上面跑的不稳定:经常需要重装环境。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 16:49 | 显示全部楼层
越详细越好,我也有这想法,谢谢!
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 16:56 | 显示全部楼层
私有云,将来也必关。运营商先关端口,后关网。文以儒乱法,武以侠犯禁,佛道教人迷信。好像文武佛道都是垃圾一样,现在网络搞什么都是毒药。乱世学成文武艺货与帝王家,佛道精神教人向善,这好的方面,共抢党总看不到,水的两面性总是看覆舟,看不到载舟?这些狗眼总会看低人的智慧
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 17:03 | 显示全部楼层
很实用啊
支持大大
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 17:08 | 显示全部楼层
小米路由器r2d用着很爽
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 17:16 | 显示全部楼层
期待大神的下一步文章。。。加油!!!!
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-8-4 18:09 来自手机 | 显示全部楼层
弄个快餐包吧,楼主
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-3-29 09:52

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表