Steven Li's Blog

李高健的博客


  • Home

  • Categories

  • Archives

  • Tags

基于CentOS 6 搭建 NextCloud 网盘

Posted on 2017-08-22

NextCoud 是一款开源(免费)软件,属于 OwnCloud的一个分支。NextCloud是基于PHP及Javascript开发,支持 MySQL/MariaDB, PostgreSQL, Oracle, SQLite等主流数据库。

与此同时,NextCloud提供了桌面版(Windows,Mac, Linux),手机版(IOS,Android)应用,极大了方便了用户的使用。

本文将主要介绍在 CentOS 6.* 环境下,安装 NextCloud 12。

近期,在捣鼓一台CentOS服务器,在加上领导想在部门内部搭建一个类似网盘的工具软件,就自己做了一些调研,对比之后选择了NextCloud。网上好多教程都是基于CentOS 7的,相对来说坑会少一些,本文也参考了部分内容。

搭建环境

  1. CentOS 6.* 64-bit
  2. Root权限
  3. PHP > 7.0
  4. 服务器:Nginx(👍),Apache(😒,试验过,太慢,放弃了)

安装Nginx 及 PHP7-FPM

网上大部分教程使用的都是Apache,我自己也试过(真的慢),但在用Nginx后,使用体验提升了一个档次。

在安装之前,须添加EPEL的Repo

1
yum -y install epel-release

安装 Nginx

1
yum -y install nginx

安装PHP7-FPM

如果之前安装过PHP,请检查PHP的版本:php -v ,也可以卸载之前的版本:yum remove php* php-common

添加PHP7的yum源:

1
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

修正yum源:

1
vi /etc/yum.repos.d/remi.repo

将remi段中的enabled=0改为enabled=1。

1
vim /etc/yum.repos.d/remi-php70.repo

与remi.repo类似,将remi-php70段中的enabled=0改为enabled=1。

扫行命令查看版本如果显示的是7.x的话 那就没问题

1
yum list php

接下来,安装PHP7-FPM以及PHP附加组件:

1
yum -y install php-fpm php-cli php-gd php-mcrypt php-mysql php-pear php-xml php-mbstring php-pdo php-json

最后,检查PHP的版本:

1
php -v

Markdown

配置 PHP7-FPM

接下来配置PHP7-FPM,使其以nginx用户运行,并监听 9000端口。

  • 修改配置文件

    1
    vim /etc/php-fpm.d/www.conf
    1. 修改用户及组,改为 nginx

      Markdown

    2. 确认PHP-FPM监听9000端口

      Markdown

    3. 去除以下注释,激活PHP-FPM的系统环境变量

      Markdown

    4. 保存,退出

    5. 在/var/lib目录下为php的session创建文件夹,并将所有者设置为nginx。(目前,我也不知道这一步是做啥的)

    6. 启动nginx与php-fpm

      1
      2
      service nginx start
      service php-fpm start

安装 MariaDB

  1. 添加MariaDB 源,在创建MariaDB.repo,文件内容可以参考:link,但是对于400多MB的文件来说,7,8K的速度也是怪可怜的。以下是一个国内镜像服务器的配置:

    1
    2
    3
    4
    5
    [mariadb]
    name = MariaDB
    baseurl = http://mirrors.ctyun.cn/MariaDB/yum/10.1/centos6-amd64
    gpgkey=http://mirrors.ctyun.cn/MariaDB/yum/RPM-GPG-KEY-MariaDB
    gpgcheck=1
  2. 安装:

    1
    yum install MariaDB-server MariaDB-client
  3. 启动

    1
    sudo /etc/init.d/mysql start
  4. 配置MariaDB

    • 数据库设置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    [MariaDB Secure installation]
    $ sudo mysql_secure_installation
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
    SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
    Enter current password for root (enter for none): Press Enter
    OK, successfully used password, moving on...
    Set root password? [Y/n] y
    New password:
    Re-enter new password:
    Password updated successfully!
    Reloading privilege tables..
    ... Success!
    Remove anonymous users? [Y/n] y
    ... Success!
    Disallow root login remotely? [Y/n] y
    ... Success!
    Remove test database and access to it? [Y/n] y
    - Dropping test database...
    ... Success!
    - Removing privileges on test database...
    ... Success!
    Reload privilege tables now? [Y/n] y
    ... Success!
    Cleaning up...
    All done! If you've completed all of the above steps, your MariaDB
    installation should now be secure.
    Thanks for using MariaDB!
    • 数据初始化
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ sudo mysql -u root -p
    Enter password: ******
    MariaDB [(none)]> create database nextcloud_db;
    Query OK, 1 row affected (0.01 sec)
    MariaDB [(none)]> GRANT ALL PRIVILEGES ON nextcloud_db.* TO 'nextcloud_user'@'localhost' IDENTIFIED BY '12345';
    Query OK, 0 rows affected (0.01 sec)
    MariaDB [(none)]> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)
    MariaDB [(none)]> exit
    Bye

安装 SSL 证书文件

(个人认为,此步可选)可以参考:这篇文章中的Step4

下载和安装 NextCloud

  1. 访问以下网址:Nextcloud,点击Download下载代码文件。

  2. 解压文件,并移动到 /usr/share/nginx/html/

    1
    2
    unzip nextcloud-10.0.2.zip
    mv nextcloud/ /usr/share/nginx/html/
  3. 移动到 nginx web应用的根目录://usr/share/nginx/html/,创建 /data文件夹,并将文件夹权限设置为nginx:nginx

    1
    2
    3
    cd /usr/share/nginx/html/
    mkdir -p nextcloud/data/
    chown nginx:nginx -R nextcloud/

配置Nginx & SELinux

  1. 进入 /etc/nginx/conf.d/,创建文件nextcloud.conf,并写入以下内容。

    (注:这里nextCloud 官网有关于 nginx的配置文档,在试验过程中,加入了SSL证书,发现一些小问题,特别是所有请求强制转发到 Https,一直有问题。在一顿Google之后,终于可以跳转了。)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    upstream php-handler {
    server 127.0.0.1:9000;
    }
    server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    # enforce https
    return 301 https://$host$request_uri;
    }
    server {
    listen 443 ssl;
    server_name cloud.example.com;
    ssl_certificate /etc/nginx/cert/nextcloud.crt;
    ssl_certificate_key /etc/nginx/cert/nextcloud.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;";
    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 /usr/share/nginx/html/;
    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 /nextcloud/public.php?service=host-meta
    # last;
    #rewrite ^/.well-known/host-meta.json
    # /nextcloud/public.php?service=host-meta-json last;
    location = /.well-known/carddav {
    return 301 $scheme://$host/nextcloud/remote.php/dav;
    }
    location = /.well-known/caldav {
    return 301 $scheme://$host/nextcloud/remote.php/dav;
    }
    location /.well-known/acme-challenge { }
    location ^~ /nextcloud {
    # 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 /nextcloud {
    rewrite ^ /nextcloud/index.php$uri;
    }
    location ~ ^/nextcloud/(?:build|tests|config|lib|3rdparty|templates|data)/ {
    deny all;
    }
    location ~ ^/nextcloud/(?:\.|autotest|occ|issue|indie|db_|console) {
    deny all;
    }
    location ~ ^/nextcloud/(?: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 ~ ^/nextcloud/(?: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 /nextcloud/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;";
    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 /nextcloud/index.php$uri$is_args$args;
    # Optional: Don't log access to other assets
    access_log off;
    }
    }
    }
  2. 保存并退出,重启 nginx。

    1
    service nginx restart
  3. 关于SELinux配置

    查看当前SELinux设置:vim /etc/selinux/conf

    • 如果 SELINUX=enforcing,须运行以下命令:

      1
      2
      3
      4
      5
      6
      7
      8
      semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/data(/.*)?'
      semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/config(/.*)?'
      semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/apps(/.*)?'
      semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/assets(/.*)?'
      semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/.htaccess'
      semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/.user.ini'
      restorecon -Rv '/usr/share/nginx/html/nextcloud/'

      这里需要注意的是:如果以后要修改云盘存储路径,同样需要修改SELinux配置。

    • 如果 SELINUX=disabled,则无须设置。

防火墙配置

如需通过IP或域名访问,须设置防火墙。

1
2
3
sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT
sudo service iptables save

NextCloud 安装配置

用浏览器打开https://[your_server_name]/nextcloud,出现如下页面,恭喜你,说明以上配置成功。

Main Page

Data folder:默认为 nextcloud/data,亦可进行修改,但必须保证所设置的文件夹的所有者为nginx:nginx。

MySQL/MariaDB:请参考第3步中的设置参数。

点击「Finish setup」,等待几十秒钟,如跳转到如下页面,则说明配置成功。

dashboard

写在后面的话

无论是NextCloud,OwnCloud还是Seafile,这些软件都为我们搭建网盘提供了比较不错的解决方案,方法大同小异。

近期,真的是掉入了Linux的海洋中,好多东西都是边实验边Google,文章中有不恰当的地方还请大家多多批评指正。

参考文档就不一一列举了,大家都可以在Google中找到相关文章。

  1. How to install Nextcloud 12 server on CentOS 7
  2. How to Install Nextcloud with Nginx and PHP7-FPM on CentOS 7
  3. NextCloud Install Guide

【Java 8 学习笔记 (2)】 Functional Interface

Posted on 2016-08-16 | In Technology

在上一篇Java学习笔记中给大家介绍了Stream,在Stream的操作方法中的参数都是Functional Interface(本文接下来简称FI)的实例, 所以要想用好Stream,就要弄懂FI。

What

关于FI,官方只给出了描述性的定义,即只包含一个抽象方法的接口即为函数式接口。这里呢,官方文档上,有一条额外的说明,原话如下:

If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

翻译过来大致的意思就是,FI中可以声明额外的抽象方法,但是其方法签名必须和java.lang.Object的public方法一致,因为这类任何接口都会存在一个来自java.lang.Object或者其他地方的实现(并不能称得上真正的抽象),所以这一类方法并不计入FI接口的抽象方法的个数。

当然大家可以将FI理解为接口中的一种特例。Java 8 提供了@FunctionalInterface 这个注解来方便我们定义函数式接口。比如我们常见的Runnable接口,就是一个函数式接口。

1
2
3
4
5
@FunctionalInterface
public interface Runnable {
//...
public abstract void run();
}

How

In Java 8, FI的实例可以通过lambda表达式,方法引用(Method Reference), 构造方法引用(Constructor Reference)三种方式来创建。本文将重点介绍一下lambda表达式,至于Method References,大家可以参考这篇文章.

就拿上面提到的Runnable接口为例。在Java 7 or lower,我们可能要这样写:

1
2
3
4
5
6
7
8
9
10
11
//Java 7 or lower
Runnable task1 = new Runnable()
{
@Override
public void run()
{
System.out.println("Task 1 is running...");
}
};
Thread thread1 = new Thread(task1);
thread1.start();
1
2
3
4
//Java 8
Runnable task1 = () -> System.out.println("Task 1 is running...");
Thread thread1 = new Thread(task1);
thread1.start();

从上面的两段代码,我们不难发现在Java8中,可以使用lambda表达式(或者方法引用,及构造方法引用)来替代之前Java版本中的匿名内部类。正是由于FI只有一个抽象方法,所以() -> System.out.println("Task 1 is running...");理所当然就指向了该抽象方法的实现。

lambda 表达式

lambda表达式的语法:

(args1, args2, args3, ……) -> { body }

表达式可分为参数集合与函数体2部分。参数集合可接受0个或多个参数,例如,() -> { some codes },(int a) -> { some codes }, (int a, int b, int c) -> { some codes }, 如果参数集合只有一个参数,其()可以省略;此外,由于FI接口在定义时,已经对其参数类型做过限制,因此参数类型亦可省略。
对于,函数体部分如果只有一行代码,最外层的{ }可以省略。

Java 8 内置常见的FI

Java 8 也提供了几种常见的FI,接下来结合Stream中的一些方法,给大家做个简单的介绍:

Function

Function接口表示:接受一个参数T,经过操作之后返回一个结果R。例如Stream中的map()方法:

1
2
3
4
5
6
/*
Returns a stream consisting of the results of applying the given function to the elements of this stream.
map(Function<? super T,? extends R> mapper)
*/
//Example: 从Stream<Person>流中找出每个girl的微信号,并返回Stream<String>
Stream<String> weChatStream = girls.map( girl -> girl.getWechat() );

Predicate

Predicate用来判断一个对象T是否满足某种条件,返回boolean值。

1
2
3
4
5
6
/*
Returns a stream consisting of the elements of this stream that match the given predicate.
filter(Predicate<? super T> predicate)
*/
//Example: 从Stream<Person>流中找出所有女生
Stream<Person> grils = persons.filter( p -> p.getGender() == Gender.FEMALE )

Consumer

Consumer表示对一个传入对象T进行操作,但并无返回值。

1
2
3
4
5
6
/*
Performs an action for each element of this stream.
forEach(Consumer<? super T> action)
*/
//Example: 打印每个女生的微信号
grils.forEach( (girl) -> System.out.println( girl.getWeChat()) );

Supplier

Supplier表示返回一个T类型的数据对象.

1
2
3
4
5
6
/*
Returns an infinite sequential unordered stream where each element is generated by the provided Supplier.
generate(Supplier<T> s)
*/
//Example: 生成一个无限的随机数流
Stream.generate( () -> Math.random() );

小结

通过本文,大家可以发现,为了支持函数式编程,Java“不计代价”的引入了Functional Interface,Default Method,Method Reference等一些全新的概念,可谓用心良苦。这两篇文章的目的也就在于给大家做一个概述,要想深入了解还需要在实际应用中慢慢摸索。本文如有不当之处,欢迎大家批评指正。

路漫漫其修远兮,吾将上下而求索。。。

参考文献

  1. The Java™ Tutorials - Lambda Expressions
  2. Java8学习笔记(1) – 从函数式接口说起
  3. JDK 8 Doc

【Java 8 学习笔记 (1)】 Stream

Posted on 2016-08-15 | In Technology

这段时间正好接触到Java8中的Stream,就想写点东西来简单记录下。

Read more »
Steven Li

Steven Li

a not bad person

3 posts
1 categories
3 tags
© 2017 Steven Li
Powered by Hexo
Theme - NexT.Pisces