centos7下部署Nextcloud私有云盘笔记
简介:
Nextcloud 是一款自由 (开源) 的类 Dropbox 软件,由 ownCloud 分支演化形成。它使用 PHP 和 JavaScript 编写,支持多种数据库系统,比如
MySQL/MariaDB、PostgreSQL、Oracle 数据库和 SQLite。它可以使你的桌面系统和云服务器中的文件保持同步,Nextcloud 为 Windows、Linux、Mac、
安卓以及苹果手机都提供了客户端支持。同时,Nextcloud 也并非只是 Dropbox 的克隆,它还提供了很多附加特性,如日历、联系人、计划任务以及流媒体 Ampache。
Nextcloud内置了Office文档、图片相册、日历联系人、两步验证、文件管理、RSS阅读等丰富的应用,我发现Nextcloud已经仅仅可以用作个人或者团队存储与共享,
还可以打造成为一个个人办公平台,几乎相当于一个个人的Dropbox了。
以下内容将介绍如何在 CentOS 7 服务器中安装和配置最新版本的 Nextcloud 16,并且会通过 Nginx 和 PHP7-FPM 来运行 Nextcloud,同时使用 MariaDB
做为数据库系统。Nextcloud云盘环境部署后,可以实现web网页端、手机移动端和桌面客户端三者数据同步,其中桌面客户端可以在本地设置一个文件夹,用于同步数据,
这样也就相当于在本地备份了数据。同时客户端只要设置开机启动,即只要是启动状态中,它和网页端的数据就是自动同步的。
部署机器的系统是Centos7.3版本
一、安装并配置Nginx和php-fpm将自带的epel、nginx、php全部卸载(rpm -e ... --nodeps)
安装Nextcloud服务组件如下: nginx/1.16.1 PHP 7.2.27 mysql 5.7.11 Nextcloud-16.0.1
[root@nextcloud-server ~]# rpm -qa|grep php [root@nextcloud-server ~]# rpm -qa|grep php-common [root@nextcloud-server ~]# rpm -qa|grep nginx 更新centos7内核以及包等 [root@nextcloud-server ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) [root@nextcloud-server ~]# yum -y update [root@nextcloud-server ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
CentOS默认的yum源中并不包含Nginx和php-fpm,首先要为CentOS添加epel源: [root@nextcloud-server ~]# yum -y install epel-release [root@nextcloud-server ~]# yum -y install nginx
需要再添加一个yum源来安装php-fpm,可以使用webtatic(这个yum源对国内网络来说恐怕有些慢,当然你也可以选择其它的yum源)安装php7.2
[root@nextcloud-server ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm [root@nextcloud-server ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm [root@nextcloud-server ~]# yum -y remove php* [root@nextcloud-server ~]# yum -y install php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml
完成后,检查一下php-fpm是否已正常安装
[root@nextcloud-server ~]# php -v PHP 7.2.19 (cli) (built: Jun 2 2019 09:49:05) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.19, Copyright (c) 1999-2018, by Zend Technologies
配置php-fpm
[root@nextcloud-server ~]# vim /etc/php-fpm.d/www.conf ..... user = nginx //将用户和组都改为nginx group = nginx ..... listen = 127.0.0.1:9000 //php-fpm所监听的端口为9000 ...... env[HOSTNAME] = $HOSTNAME //去掉下面几行注释 env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp
在
/var/lib
目录下为session路径创建一个新的文件夹,并将用户名和组设为nginx
[root@nextcloud-server ~]# mkdir -p /var/lib/php/session [root@nextcloud-server ~]# chown nginx:nginx -R /var/lib/php/session/ [root@nextcloud-server ~]# ll -d /var/lib/php/session/ drwxr-xr-x 2 nginx nginx 4096 Jul 24 17:40 /var/lib/php/session/ 启动Nginx和php-fpm服务,并添加开机启动 [root@nextcloud-server ~]# systemctl start php-fpm [root@nextcloud-server ~]# systemctl start nginx [root@nextcloud-server ~]# systemctl enable php-fpm [root@nextcloud-server ~]# systemctl enable nginx
二、安装并配置MariaDB
使用MaraiDB作为Nextcloud数据库。yum安装MaraiDB服务
[root@nextcloud-server ~]# yum -y install mariadb mariadb-server
启动MariaDB服务并添加开机启动
[root@nextcloud-server ~]# systemctl start mariadb [root@nextcloud-server ~]# systemctl enable mariadb
初始化设置MariaDB的root密码
[root@nextcloud-server ~]# mysql_secure_installation //按照提示设置密码,首先会询问当前密码,密码默认为空,直接回车即可 Enter current password for root (enter for none): //直接回车 Set root password? [Y/n] Y New password: //输入新密码 Re-enter new password: //再次输入新密码 Remove anonymous users? [Y/n] Y Disallow root login remotely? [Y/n] Y Remove test database and access to it? [Y/n] Y Reload privilege tables now? [Y/n] Y ============================================================================================== 或者采用另一种修改密码的方式:跳过授权表 1)在/etc/my.cnf文件里添加"skip-grant-tables" 2)重启mariadb服务 3)无密码登陆mariadb,然后重置mysql密码 MariaDB [(none)]> update mysql.user set password=password("kevin@123") where user="root"; 4)去掉/etc/my.cnf文件里的"skip-grant-tables"内容 5)重启mariadb服务 6)这样就可以使用上面重置的新密码kevin@123登陆mariadb了
设置完MariaDB的密码后,使用命令行登录MariaDB,并为Nextcloud创建相应的用户和数据库。
例如数据库为nextcloud_db,用户为nextclouduser,密码为nextcloudpasswd:
[root@nextcloud-server ~]# mysql -p ...... MariaDB [(none)]> create database nextcloud_db; MariaDB [(none)]> create user nextclouduser@localhost identified by 'nextcloudpasswd'; MariaDB [(none)]> grant all privileges on nextcloud_db.* to nextclouduser@localhost identified by 'nextcloudpasswd'; MariaDB [(none)]> flush privileges;
三、为Nextcloud添加ssl证书
[root@nextcloud-server ~]# cd /etc/nginx/cert/ #没有创建一个 把ssl证书存放到该目录下 [root@nextcloud-server cert]# openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key Generating a 2048 bit RSA private key ................................................+++ ..............+++ writing new private key to '/etc/nginx/cert/nextcloud.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN #国家 State or Province Name (full name) []:Beijing #省份 Locality Name (eg, city) [Default City]:Beijing #地区 Organization Name (eg, company) [Default Company Ltd]:***信息技术有限公司 #公司名 Organizational Unit Name (eg, section) []:IT #部门 Common Name (eg, your name or your server's hostname) []:test #CA主机名 Email Address []:123456@ceshi.com #邮箱
[root@nextcloud-server cert]# ll 总用量 8 -rw-r--r--. 1 root root 1395 4月 14 17:41 nextcloud.crt -rw-r--r--. 1 root root 1704 4月 14 17:41 nextcloud.key 然后将证书文件的权限设置为660 [root@nextcloud-server cert]# chmod 700 /etc/nginx/cert [root@nextcloud-server cert]# chmod 600 /etc/nginx/cert/*
[root@nextcloud-server ~]# yum -y install wget unzip bzip2 [root@nextcloud-server ~]# cd /usr/local/src/ [root@nextcloud-server src]# wget https://download.nextcloud.com/server/releases/nextcloud-16.0.1.tar.bz2 [root@nextcloud-server src]# tar jxvf nextcloud-16.0.1.tar.bz2 [root@nextcloud-server src]# mv nextcloud /apps/ [root@nextcloud-server src]# chown nginx:nginx -R /apps/nextcloud/
五、设置Nginx虚拟主机
进入Nginx的虚拟主机配置文件所在目录并创建一个新的虚拟主机配置(记得修改两个server_name为自己的域名):
[root@nextcloud-server ~]# cd /etc/nginx/conf.d/ [root@nextcloud-server conf.d]# vim nextcloud.conf
# 将nginx原配置中80端口的配置删除 $ vim /etc/nginx/nginx.conf -------------------------------------------------------- server { # 将80端口的server整个注释掉,我们在nextcloud.conf中已经配置了,这里不删除的话会导致冲突不生效。 listen 80; ... }
接下来测试以下配置文件是否有错误,确保没有问题后重启Nginx服务。
[root@nextcloud-server conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@nextcloud-server conf.d]# systemctl restart nginx
六、为Nextcloud设置Firewalld防火墙和SELinux
可以选择关闭Firewalld和SELinux [root@nextcloud-server ~]# systemctl stop firewalld [root@nextcloud-server ~]# systemctl disable firewalld [root@nextcloud-server ~]# setenforce 0 [root@nextcloud-server ~]# getenforce disable [root@nextcloud-server ~]# cat /etc/sysconfig/selinux ...... SELINUX=disabled
如果打开了防火墙,则需要设置Firewalld和SELinux
首先需要安装SElinux管理工具policycoreutils-python
[root@nextcloud-server ~]# yum -y install policycoreutils-python 接着设置SELinux [root@nextcloud-server ~]# semanage fcontext -a -t httpd_sys_rw_content_t '/apps/nextcloud/data(/.*)?' [root@nextcloud-server ~]# semanage fcontext -a -t httpd_sys_rw_content_t '/apps/nextcloud/config(/.*)?' [root@nextcloud-server ~]# semanage fcontext -a -t httpd_sys_rw_content_t '/apps/nextcloud/apps(/.*)?' [root@nextcloud-server ~]# semanage fcontext -a -t httpd_sys_rw_content_t '/apps/nextcloud/assets(/.*)?' [root@nextcloud-server ~]# semanage fcontext -a -t httpd_sys_rw_content_t '/apps/nextcloud/.htaccess' [root@nextcloud-server ~]# semanage fcontext -a -t httpd_sys_rw_content_t '/apps/nextcloud/.user.ini' [root@nextcloud-server ~]# restorecon -Rv '/apps/nextcloud/' 接下来设置Firewlld防火墙,为Nextcloud开放http和https两个端口 [root@nextcloud-server ~]# systemctl start firewalld [root@nextcloud-server ~]# systemctl enable firewalld [root@nextcloud-server ~]# firewall-cmd --permanent --add-service=http [root@nextcloud-server ~]# firewall-cmd --permanent --add-service=https [root@nextcloud-server ~]# firewall-cmd --reload
创建nextcloud用户数据目录 [root@nextcloud-server ~]# mkdir /data1/nextclo_db [root@nextcloud-server ~]# chown nginx:nginx /data1/nextclo_db/ -R
七、安装Nextcloud
解析上面nginx中配置的域名,访问访问http:
//172.16.5.12
进行Nextcloud界面安装(访问http域名会自动跳转到https,安装提示安装即可!)
性能优化,进入主界面后,右上角自己头像,点击设置,基本设置:
第一项就有 安全及设置警告,这里会有配置错误提示,优化提示等。根据提示进行优化:
① 修改php.ini 文件,添加如下配置:
$ vim /etc/php.ini [PHP] # 在[PHP] 以下添加如下配置 opcache.enable=1 opcache.enable_cli=1 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=10000 opcache.memory_consumption=128 opcache.save_comments=1 opcache.revalidate_freq=1 upload_max_filesize = 10240M # 上传文件最大大小,可以自定义修改,默认为512M
346 yum -y install memcached
347 cat /etc/sysconfig/memcached
348 systemctl start memcached
349 systemctl enable memcached
350 lsof -i:11211
351 yum install lsof
352 lsof -i:11211
353 cd /apps/nextcloud
355 cd config/
357 cp -a config.php config.php.bar
359 vim config.php
360 vim /etc/php.ini
361 cd /etc/
363 cp -a php.ini php.ini.bar
364 vim php.ini
365 vim /etc/nginx/conf.d/nextcloud.conf
366 systemctl restart php-fpm
367 systemctl restart nginx