在 CentOS/RHEL 8 上安装 PHP

最后,我们必须在 Linux 服务器上安装 PHP,并将其与 Nginx Web 服务器集成以形成 LEMP 堆栈。

我们已经列出了所有必需的 PHP 扩展,并且我们正在使用单个 dnf 命令安装所有这些扩展。

# yum install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring php-common php-json php-curl php-zip php-bz2 php-intl -y

配置 PHP 设置以与 Nginx Web 服务器集成。

# vi /etc/php-fpm.d/www.conf

找到以下参数。

user = apache
group = apache

并替换为以下参数。

user = nginx
group = nginx

设置以下 SELinux 布尔值以允许 Nginx Web 服务器执行外部程序。

# setsebool -P httpd_execmem 1

使用 systemctl 命令启用并启动 php-fpm.service。

# systemctl enable --now php-fpm.service
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service -> /usr/lib/systemd/system/php-fpm.service.

要将 PHP 配置应用到 Nginx Web 服务器,我们需要重新启动它。

# systemctl restart nginx.service

使用著名的 phpinfo() 函数创建一个示例 PHP 页面,以验证 PHP 支持并检查已安装的模块。

# vi /usr/share/nginx/html/info.php

其中添加以下 PHP 代码。

<?php phpinfo(); ?>

在 Web 浏览器中打开 URL https://nextcloud-01.onitroad.com/info.php。

LEMP 服务器已成功配置。

在 CentOS/RHEL 8 上使用 Nginx 安装 Nextcloud 服务器

Nextcloud 是一款私有云存储软件。
在本文中,我们将学习如何在 CentOS/RHEL 8 上使用 Nginx 安装 Nextcloud 服务器。

为 Nextcloud 服务器配置 Nginx 服务器

编辑 /etc/nginx/nginx.conf 配置文件。

# vi /etc/nginx/nginx.conf

在此文件中搜索并设置以下指令。

server_name  nextcloud-01.onitroad.com;
root         /usr/share/nginx/nextcloud/;

重新启动 Nginx 服务使更改生效。

# systemctl restart nginx.service

为 Nextcloud 服务器创建数据目录。
该目录非常重要,因为它将保存使用 Web 界面上传的所有文件。

# mkdir /usr/share/nginx/nextcloud/data

将 nginx 用户设置为该目录的所有者。

# chown -R nginx:nginx /usr/share/nginx/nextcloud/data

为此目录创建一个 SELinux 文件上下文。
所以,nginx 用户可以在这个目录上写。

# semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/nextcloud/data(/.*)?'

在数据目录上应用新添加的文件上下文。

# restorecon -R /usr/share/nginx/nextcloud/data/

通过在 Web 浏览器中打开 URL http://nextcloud-01.onitroad.com 访问 Nextcloud Web 应用程序。

你会被要求

  • 为 Nextcloud 创建一个管理员用户
  • 数据目录的位置
  • 访问 Nextcloud 数据库的凭据

我们可以按如下方式输入所需的信息。

单击“完成设置”,我们将被路由到“Nextcloud Hub”。

默认情况下,我们以管理员用户身份登录。
我们现在可以使用 Nextcloud 网络界面上传文件。

为 Nextcloud 配置 SELinux 和文件权限

调整 Nextcloud 服务器软件上的文件权限。

# chown -R nginx:nginx /usr/share/nginx/nextcloud

我们还需要调整以下 PHP 目录的所有者组。

# chgrp -R nginx /var/lib/php/{opcache,session,wsdlcache}

因为,我们正在以强制模式安装带有 SELinux 的 Nextcloud。
因此,我们需要在 SELinux 策略中添加 Nextcloud 软件目录的自定义“文件上下文”。

为此,我们可以使用 semanage 命令。
如果 CentOS/RHEL 8 机器上没有 semanage 命令,那么我们必须使用 Linux 包管理器安装 policycoreutils-python-utils 包。

# semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/nextcloud(/.*)?'

使用 restorecon 命令在 Nexcloud 目录上应用新添加的文件上下文。

# restorecon -R /usr/share/nginx/nextcloud

为 Nextcloud 服务器创建 MariaDB 数据库

以 root 用户身份连接到 MariaDB 数据库服务器。

# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

为 Nextcloud 服务器创建一个 MySQL 数据库,如下所示。

MariaDB [(none)]> CREATE DATABASE nextcloud DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
Query OK, 1 row affected (0.143 sec)

创建一个用户来拥有 nextcloud 数据库。

MariaDB [(none)]> CREATE USER ncuser@localhost IDENTIFIED BY 'onitroad@123';
Query OK, 0 rows affected (0.209 sec)

将 nextcloud 数据库的所有权限授予 ncuser 。

MariaDB [(none)]> GRANT ALL PRIVILEGES ON nextcloud.* TO ncuser@localhost;
Query OK, 0 rows affected (0.032 sec)

重新加载权限表。

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.069 sec)

从 MariaDB shell退出。

MariaDB [(none)]> EXIT;
Bye

在 CentOS/RHEL 8 上安装 Nginx Web 服务器

Nextcloud 是一个基于 PHP 的 Web 应用程序,因此,它需要在 LEMP 服务器上安装 LAMP 才能进行部署。

根据文档,建议使用 Apache Web 服务器。
但是,Nextcloud 也支持 Nginx Web 服务器,我们在本安装教程中使用相同的。

# dnf install nginx -y

通过执行 systemctl 命令启用和启动 Nginx 服务。

# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service -> /usr/lib/systemd/system/nginx.service.

通过允许 Linux 防火墙中的相关服务端口,允许通过网络访问 Nginx 服务。

# firewall-cmd --permanent --add-service=http
success
# firewall-cmd --reload
success

要验证 Nginx Web 服务器是否可访问,请在 Web 浏览器中打开 URL http://nextcloud-01.onitroad.com。

在 CentOS/RHEL 8 上安装 MariaDB 数据库服务器

Nextcloud 软件需要后端数据库服务器来创建其数据存储库。

SQLite 是 Nextcloud 服务器的默认数据库,适用于测试环境。

Nextcloud 还支持 MySQL、MariaDB、Oracle 11g 和 PostgreSQL 数据库。
但是 MySQL/MariaDB 是 Next Cloud 在生产环境中的推荐数据库。

因此,我们应该在 Linux 操作系统上安装 MariaDB 服务器。

# dnf install -y mariadb-server

通过执行 systemctl 命令启用和启动 MariaDB 服务。

# systemctl enable --now mariadb.service
Created symlink /etc/systemd/system/mysql.service -> /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service -> /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service -> /usr/lib/systemd/system/mariadb.service.

配置 MySQL 服务器并为 root 用户设置一个强密码。

# 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
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] Y
 ... 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] 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!
on  It Road.com

在 CentOS/RHEL 8 上安装 Nextcloud 服务器

Nextcloud 是免费和开源的,因此我们可以从他们的官方网站轻松下载 Nextcloud。

目前,Nextcloud 19 可以在他们的网站上下载。

复制下载 Nextcloud 按钮的链接位置。
我们将使用此 URL 从 Linux CLI 下载 Nextcloud 软件。

将目录更改为 /tmp 并使用复制的 URL 和 wget 下载 Nextcloud 19 安装档案。

# cd /tmp
# wget https://Nextclouddownload.nextcloud.com/server/releases/nextcloud-19.0.0.zip

下载的 Nextcloud 软件是 zip 格式,因此,我们需要解压缩实用程序来解压缩下载文件的内容。

如果 Linux 服务器上尚未安装 unzip,那么我们可以通过执行 dnf 命令轻松安装它。

# dnf install unzip -y

使用 unzip 命令解压缩下载文件的内容。

# unzip nextcloud-19.0.0.zip -d /usr/share/nginx/

我们使用 -d 开关将 zip 文件直接解压缩到 Nginx Web 服务器的默认文档根目录中。

日期:2020-09-17 00:16:38 来源:oir作者:oir