在CentOS7上设置Nextcloud18.0.4+Apache+MariaDB+Redis
首先
Nextcloud是一种在线存储服务,可以通过使用自己的服务器轻松实现安全的文件共享环境。
最近,在CentOS7上安装了Nextcloud18.0.4,并在文章《在CentOS7上安装Nextcloud18.0.4并进行简单体验》中介绍了最低限度的Nextcloud运行环境设置步骤,而这次,我们将介绍添加数据库和内存缓存服务器的安装步骤,以便进行正常运行。
構建此次的環境
形象构成
使用的操作系统和中间件
服务器操作系统(CentOS)
我們這次使用CentOS7作業系統。如果您使用VirtualBox或Vagrant進行準備也沒有問題。
下面是我們正在展示的步驟所使用的環境。
$ cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
$ uname -a
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
下一个云(Nextcloud)的主体。
当然了,没有这个的话就不能使用Nextcloud。
Nextcloud有很多版本,但在2020年4月27日的最新版本是18.0.4,我们将使用这个版本。
网络服务器 (Apache)
由于Nextcloud是基于Web的,所以需要一个运行Web服务器的环境。在这次使用Apache。
下面是使用Apache的版本的步骤概述。
$ httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Aug 8 2019 11:41:18
PHP可以被编写成中文。
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.9 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=b8fc6671bdb40404
构建步骤
几乎所有的内容都取自Nextcloud官方文档。我们将依据此进行进行下一步操作。
安装所需的软件包进行安装操作。
sudo yum install -y epel-release yum-utils unzip curl wget bash-completion bzip2
我将系统更新到最新版本。
sudo yum update -y
安装Web服务器(Apache)。
sudo yum 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 yum install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm -y
sudo yum install --enablerepo=remi,remi-php74 php php-fpm php-cli php-common php-curl php-gd php-mbstring php-mysqlnd php-process php-xml 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。(Wǒ huì Apache.)
sudo systemctl restart httpd.service
安装数据库(MariaDB)。
curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
sudo yum install MariaDB-server MariaDB-client -y
将MariaDB配置为自动启动,并启动MariaDB。
sudo systemctl enable mariadb.service
sudo systemctl start mariadb.service
我将进行MariaDB的基本设置。会询问一些问题,除了设置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。
sudo systemctl restart mariadb
我們要先連接到MariaDB的控制台,然後確認字符編碼設定是否為UTF-8mb4。
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';
※展示授权~ 的执行结果。
+------------------------------------------------------------------------------------------------------------------+
| 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
请确定并输入适用于Nextcloud管理员的用户名和密码。然后,在”请设置数据库”中,点击”MySQL/MariaDB”,会出现与数据库相关的设置输入表单,按照以下方式输入设置值。
确认数据库中已经创建了表。现在数据库周边的设置已经完成了。
sudo mysql -u nextcloud -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 yum install --enablerepo=remi 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/2/10: 初版。
- 2020/4/27: CentOS7 + Apache2.4 + PHP7.4 + MariaDB10.4 + Redis5 + Nextcloud 18 の環境に即した修正、他こまごま修正。