在CentOS8上安装Nextcloud 18.0.4+Apache+MariaDB+Redis

首先

Nextcloud是一种在线存储,可使用自己的服务器方便地实现安全的文件共享环境。
最近在《尝试在CentOS8上安装Nextcloud18.0.4以便体验简便分享的文章中》,我们介绍了最基本的Nextcloud操作环境建立步骤,但本次将介绍添加数据库和内存缓存服务器的安装步骤,使其能够进行正常运行。

这次要构建的环境

形成形象

image.png

使用操作系统和中间件

服务器操作系统(CentOS)

这次我们将使用CentOS8。如果您已经准备好了VirtualBox或Vagrant等工具也没有问题。
下面是我们使用的步骤的环境。

$ cat /etc/redhat-release 
CentOS Linux release 8.1.1911 (Core) 
$ uname -a
Linux localhost.localdomain 4.18.0-147.el8.x86_64 #1 SMP Wed Dec 4 21:51:45 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

下一个云本身

当然没有这个就不能使用Nextcloud。
Nextcloud有各种各样的版本,但我们将使用截至2020年4月27日的最新版本18.0.4。

网络服务器 (Apache)

由于Nextcloud是基于Web的,所以需要一个运行Web服务器的环境。这次我们将使用Apache。下面是所部署的Apache版本。

$ httpd -v
Server version: Apache/2.4.37 (centos)
Server built:   Dec 23 2019 20:45:34

PHP (Hypertext Preprocessor) 是一种流行的服务器端脚本语言。

Nextcloud在PHP下运行。
该部署步骤中使用的PHP版本如下所示。

$ php -v
PHP 7.4.5 (cli) (built: Apr 14 2020 12:54:33) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies

数据库(MariaDB)

在Nextcloud中,使用数据库。在正式运营中,建议使用MariaDB/MySQL等数据库。本次使用的是MariaDB。
在这里展示的步骤中,MariaDB的版本如下所示。

$ mysql --version
mysql  Ver 15.1 Distrib 10.4.12-MariaDB, for Linux (x86_64) using readline 5.1

Redis是一种内存缓存服务器。

在Nextcloud中,我们管理会话和文件锁定,并通过使用内存缓存服务器来提高性能。在这次操作中,我们将使用Redis。

以下是展示操作步骤中使用的Redis版本:

$ redis-server --version
Redis server v=5.0.3 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=8c0bf22bfba82c8f

建立程序

几乎是Nextcloud官方文档中的内容,我们将按照这个进行。

我们将安装所需的软件包以执行安装操作。

sudo dnf install dnf-utils unzip curl wget bash-completion -y

sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y

我会将系统更新到最新状态。

sudo dnf upgrade -y

安装Web服务器(Apache)。

sudo dnf install -y httpd

创建Apache的附加配置文件。

sudo vi /etc/httpd/conf.d/nextcloud.conf

请提供需要转述的具体内容。

<VirtualHost *:80>
  DocumentRoot /var/www/html/
  ServerName  (サーバのIPアドレス)

<Directory "/var/www/html/">
  Require all granted
  AllowOverride All
  Options FollowSymLinks MultiViews
</Directory>
</VirtualHost>

将Apache配置成自动启动,并启动Apache。

sudo systemctl enable httpd.service

sudo systemctl start httpd.service

安装需要的PHP模块才能运行Nextcloud。

sudo dnf install http://rpms.famillecollet.com/enterprise/remi-release-8.rpm -y

sudo dnf module install php:remi-7.4 -y

sudo dnf install php-gd php-mysqlnd php-process php-opcache php-pecl-apcu php-intl php-pecl-redis php-pecl-zip php-pear -y

⑦下载Nextcloud软件包和校验文件(用于检查Nextcloud软件包是否正确下载)。

wget https://download.nextcloud.com/server/releases/nextcloud-18.0.4.tar.bz2

wget https://download.nextcloud.com/server/releases/nextcloud-18.0.4.tar.bz2.md5

使用校验和文件来验证Nextcloud软件包是否正确下载。

md5sum -c nextcloud-18.0.4.tar.bz2.md5 < nextcloud-18.0.4.tar.bz2

如果显示nextcloud-18.0.4.tar.bz2:OK或nextcloud-18.0.4.tar.bz2:完成,则表示OK。

我会解压下载的Nextcloud软件包。

tar xf nextcloud-18.0.4.tar.bz2

将已展开的Nextcloud复制到Web服务器的文档根目录中。

sudo cp -R nextcloud/ /var/www/html/

创建Nextcloud的数据目录。

sudo mkdir /var/www/html/nextcloud/data

我将更改所有者信息,以便在整个Nextcloud目录中实现读写权限。

sudo chown -R apache:apache /var/www/html/nextcloud

重新启动Apache服务器。

sudo systemctl restart httpd.service

安装数据库(MariaDB)。

curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash

sudo dnf install MariaDB-server MariaDB-client -y

将MariaDB的自启动设置为启动MariaDB。

sudo systemctl enable mariadb.service

sudo systemctl start mariadb.service

进行MariaDB的基本设置。会有一些问题要问,但除了在New password:和Re-enter new password:处设置root用户密码外,其他都按下[Enter]键留空。

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!

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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] 
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] 
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
      5 # See the examples of server my.cnf files in /usr/share/mysql/


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] 
 ... 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] 
 ... Success!

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] 
 - 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] 
 ... 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!

为了设置在MariaDB中处理的字符编码,需要编辑MariaDB配置文件。

sudo cp -pi /etc/my.cnf.d/server.cnf{,.orig}

sudo vi /etc/my.cnf.d/server.cnf

※修改部分※ 在 [mariadb] 下方新增4行。

$ cat /etc/my.cnf.d/server.cnf
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]

#
# * Galera-related settings
#
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
character-set-server = utf8mb4

[client-mariadb]
default-character-set = utf8mb4

# This group is only read by MariaDB-10.4 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.4]

为了将设置反映到MariaDB中,需要重新启动MariaDB。

sudo systemctl restart mariadb

我将连接到MariaDB控制台,并确认字符编码已设置为utf8mb4。

sudo mysql -u root -p
show variables like "chara%";

请确认按以下所示显示结果。

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

使用MariaDB控制台,继续添加在Nextcloud上使用的用户、数据库和权限。在这里,我们将分别进行以下设置。

    • ユーザ: nextcloud

 

    データベース名: nextcloud
CREATE DATABASE nextcloud DEFAULT CHARACTER SET utf8mb4;

CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY '[nextcloudユーザに対して設定したいパスワード]';

GRANT ALL ON nextcloud.* TO "nextcloud"@"localhost";

SHOW GRANTS FOR 'nextcloud'@'localhost';

※运行 SHOW GRANTS 的结果

+------------------------------------------------------------------------------------------------------------------+
| Grants for nextcloud@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'nextcloud'@'localhost' IDENTIFIED BY PASSWORD 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' |
| GRANT ALL PRIVILEGES ON `nextcloud`.* TO 'nextcloud'@'localhost'                                                 |
+------------------------------------------------------------------------------------------------------------------+

退出MariaDB控制台。

quit;

我会进行一些设置,使得这个服务器可以从外部通过http进行访问,例如防火墙等。

sudo setenforce 0

sudo sed -i s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config

sudo firewall-cmd --permanent --zone public --add-service http

sudo firewall-cmd --reload
image.png
image.png

请设定并输入Nextcloud管理员的用户名和密码。然后,在「请设置数据库」中点击「MySQL/MariaDB」,会出现数据库相关的设置表单,按照以下配置值进行输入。

image.png
image.png

确认数据库中是否已经创建了表格。这样数据库的设置就完成了。

sudo mysql -u mextcloud -p nextcloud
SHOW TABLES;

执行 “SHOW TABLES;” 的结果

+-----------------------------+
| Tables_in_nextcloud         |
+-----------------------------+
| oc_accounts                 |
| oc_activity                 |
| oc_activity_mq              |

~~~ (中略) ~~~

| oc_vcategory                |
| oc_vcategory_to_object      |
| oc_whats_new                |
+-----------------------------+

退出MariaDB控制台。

quit;

安装具有内存缓存功能的Redis服务器。

sudo dnf install redis -y

将Redis设置为自动启动并启动Redis。

sudo systemctl enable redis.service

sudo systemctl start redis.service

我們將編輯Nextcloud設定文件,以便在Nextcloud中使用Redis。

sudo cp -pi /var/www/html/nextcloud/config/config.php{,.orig}

sudo vi /var/www/html/nextcloud/config/config.php

※需要修正的地方※ 添加了 memcache 行和 redis 行。

$ sudo cat /var/www/html/nextcloud/config/config.php  # 'memcache.〜'行と'redis'行を追加します。 
<?php
$CONFIG = array (
  'instanceid' => 'xxxxxxxxxxxx',
  'passwordsalt' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'trusted_domains' => 
  array (
    0 => '<サーバのIPアドレス>',
  ),
  'datadirectory' => '/var/www/html/nextcloud/data',
  'dbtype' => 'mysql',
  'version' => '18.0.4.0',
  'overwrite.cli.url' => 'http://<サーバのIPアドレス>/nextcloud',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost:3306',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'nextcloud',
  'dbpassword' => 'xxxxxxxx',
  'installed' => true,
  'memcache.distributed' => '\\OC\\Memcache\\Redis',
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'redis' => 
  array (
    'host' => 'localhost',
    'port' => 6379,
  ),
);

我要重新启动Apache。

sudo systemctl restart httpd.service

为了确认Nextcloud是否使用了Redis,将启动Redis的监控功能。

redis-cli monitor

我会在这种状态下登录Nextcloud并随便操作一下。如果此时能输出以下访问日志,则表示Redis已正确配置。

・・・
1587956310.804419 [0 127.0.0.1:47168] "GET" "8924842f75705fec205fcdd0f54c05dd/JS-4d3c1191d80cb633e342867f6cc7d180files-merged-index.js.deps"
1587956310.804658 [0 127.0.0.1:47168] "EXISTS" "8924842f75705fec205fcdd0f54c05dd/JS-4d3c1191d80cb633e342867f6cc7d180files-merged-index.js.deps"
1587956310.811691 [0 127.0.0.1:47168] "GET" "8924842f75705fec205fcdd0f54c05dd/JS-4d3c1191d80cb633e342867f6cc7d180activity-activity-sidebar.js.deps"
・・・

作者的话

只需要一种选项:由于Nextcloud只需要一个服务器就可以轻松尝试,因此请尽情试用并体验其易用性。虽然未来可能不定期,但我会尝试提供不同案例的Nextcloud构建步骤。

更新记录

    2019/4/28: 初版。
广告
将在 10 秒后关闭
bannerAds