NextCloud私有网盘部署

1、NextCloud简介

  • 关于NextCloud

    NextCloud是一个开源的私有云盘存储项目,可以快速的搭建一套属于自己的网盘平台。也可以给自己的团队使用。除了网盘以外,还提供各种插件的扩展功能,上传下载速度取决于你服务器的带宽。比某盘高到不知道哪里去了

  • 关于环境

    需要服务器提供LNMP或者LAMP环境。安装方法的话,可以把NextCloud简单理解为一个别人写好的网站。我们只需要放在服务器上即可。

  • 关于离线下载

    网盘基本上都有离线下载功能,NextCloud的离线下载用的是Aria2。

2、NextCloud部署配置

2.1 LAMP的搭建

这个地方可以自己手动搭建也可以使用一些一键脚本或者面板来搭建。通过手动搭建可以更好地了解整个过程。所以以下过程全都是手动搭建。

  • 首先配置源(根据系统配置,这里是centos7)
[root@Cloud ~]# mount /dev/sr0 /mnt
[root@Cloud ~]# mv /etc/yum.repos.d/* /tmp/
[root@Cloud ~]# vi /etc/yum.repos.d/local.repo
[local]
name=centos
baseurl=file:///mnt
gpgcheck=0
enabled=1

#安装基础工具
[root@Cloud ~]# yum install -y wget zip unzip

Apache安装配置

  • 通过yum安装apache
[root@Cloud ~]# yum install -y httpd

#启动服务设置开机自启
[root@Cloud ~]# systemctl start httpd && systemctl enable httpd 

#查看端口
[root@Cloud ~]# ss -tnl | grep 80
LISTEN     0      128         :::80                      :::* 

#关闭防火墙
[root@Cloud ~]# systemctl stop firewalld
[root@Cloud ~]# systemctl disable firewalld

#SELinux关掉(也可以配置一下安全上下文)
[root@Cloud ~]# setenforce 0
[root@Cloud ~]# vi /etc/sysconfig/selinux 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled    #这里设置成disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are pro
tected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

之后通过自己的ip访问应该会有个默认的测试页面。这里就不展示了 到此为止LAMP中的L和A我们就搞定了。

Mariadb安装配置

  • 通过yum安装Mariadb
[root@Cloud ~]#yum install mariadb mariadb-server mariadb-devel -y

#启动服务设置开机自启
[root@Cloud ~]# systemctl start mariadb && systemctl enable mariadb

#查看端口
[root@Cloud ~]# ss -tnl | grep 3306
LISTEN     0      50           *:3306                     *:*  

#初始化一下数据库
[root@Cloud ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):  #回车初始数据库没有密码
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y #这里问你要不要设置一个root密码
New password:     #输入新密码
Re-enter new password:     #重复输入
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y  #去掉匿名用户
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n  #是否禁止root远程登录
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y #删除测试用的数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

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! 

[root@Cloud ~]# mysql -uroot -p刚才设置的密码 
#查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

#创建一个数据库叫nextcloud_db 默认字符集使用utf8
MariaDB [(none)]> create database nextcloud_db default charset=utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nextcloud_db       |
| performance_schema |
+--------------------+

#授权root用户远程登录访问nextcloud_db所有表,密码是:123456
MariaDB [(none)]> grant all privileges on nextcloud_db.* to root@'%' identified by '123456';

MariaDB [(none)]> flush privileges;
#刷一下授权

这样数据库的搭建就完成了。最后就剩下php 我们的环境就搭好了

PHP安装配置

nextcloud搭建需要php7.2以上的版本。而如果用本地的 yum安装版本过低不符合搭建的要求。所以这个地方得用一下网络源。或者你选择使用源码安装。这里直接找一个fedora的源去安装7.2的php

  • 首先增加php网络源
[root@Cloud ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

[root@Cloud ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

  • 安装php7.2
[root@Cloud ~]# yum install -y php72w php72w-devel php72w-pear php72w-pecl php72w-gd php72w-opcache php72w-cli php72w-pdo php72w-process php72w-pecl-apcu php72w-mcrypt php72w-mysql php72w-fpm php72w-pecl-redis php72w-common php72w-xml php72w-mbstring php72w-pecl-igbinary php72w-intl php72w-pecl-imagick

#httpd添加PHP服务模块
[root@Cloud ~]# vi /etc/httpd/conf/httpd.conf 
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php #加上这一行

#写一个php测试页面
[root@Cloud ~]# vi /var/www/html/test.php
<?php
phpinfo();
?>

#将这个默认目录的权限给apache
[root@Cloud ~]# chown -R apache.apache /var/www/html

#启动服务,注意还需重启httpd服务加载配置
[root@Cloud ~]# systemctl start php-fpm && systemctl enable php-fpm
[root@Cloud ~]# systemctl restart httpd

之后通过http://服务器的ip/test.php可以看到一个php的测试页面说明配置php环境成功

2.2 nextcloud安装

从官网下载nextcloud。

[root@Cloud ~]# wget https://download.nextcloud.com/server/releases/nextcloud-18.0.0.zip

#unzip解压 
[root@Cloud ~]# unzip nextcloud-18.0.0.zip

#复制网盘项目文件到/var/www/html中
[root@Cloud ~]# mv nextcloud /var/www/html

#配置权限
[root@Cloud html]# chown -R apache.apache /var/www/html/nextcloud 

然后就可以通过http://服务器ip/nextcloud/index.php 来访问,并根据页面提示配置相应的数据库地址,最后可以通过端口映射来将服务映射出去。

公网映射从外网访问可能会弹出从不受信任的域名访问。这个时候需要更改一下配置文件

 [root@Cloud nextcloud]#vi /var/www/html/nextcloud/config/config.php 
 array (
    0 => '192.168.2.9',
    1 => '你的公网ip'