跳过正文

如何在 RHEL系统操作系统中安装 LAMP环境

·376 字·2 分钟
作者
王浩宇
记录服务器、虚拟化、网络和日常折腾。

环境初始化
#

配置镜像源
#

首先我们想要在 RHEL 环境下安装lamp环境会需要一些软件包,Apache(Nginx)Mysql(Mariadb)默认系统仓库是自带的,无需额外添加其他软件包仓库,但是php推荐大家使用 remi 源进行安装,这里我们就需要安装Remi源。
如果你像我一样使用 RedHat Linux 发行版操作系统,需要先向红帽认证服务器注册此操作系统,才能正常使用。
rhel-register

由于Remi源依赖于EPEL(Extra Packages for Enterprise Linux)源,因此需要先安装EPEL:

dnf install epel-release -y # 如果不可用就用下面的,latest-x是操作系统版本
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

然后在进行安装 Remi 源:

dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y

关闭 SELinux
#

SELinux可能会限制Apache和MySQL的运行,因此建议关闭SELinux
参考:如何在 RHEL系列操作系统中禁用 SELinux?

安装 Apache
#

如果需要配置 ssl证书,请将mod_ssl模块一并安装。

 dnf install httpd mod_ssl httpd-manual 
install-apache

安装 PHP
#

我们在此之前已经安装好了 Remi 源 这里可以使用 Remi源直接安装 PHP环境以及依赖。 在安装PHP之前,需要先重置PHP模块,因为其他仓库(AppStream/EPEL)都携带了旧版本php,以避免冲突,我们需要先重置一下。

dnf module reset php -y

启用 php 8.2模块

dnf module enable php:remi-8.2 -y

dnf module enable 命令用于启用指定的模块,这里启用了Remi源提供的PHP8.2模块,确保后续安装的PHP软件包来自Remi源中的82版本。

安装php以及扩展(扩展根据你的实际需要进行安装,我这里要跑typecho,所以就是下面这些)

 dnf install php php-fpm php-mysqlnd php-gd php-xml php-mbstring php-zip -y

### 安装 MySQL 并初始化
这里仓库自带 MySQL 8.x版本,如需其他5.7等老版本可以参考官方教程进行编译安装,目前近几年的rhel8.x 9.x都不提供5.x以及更早版本的rpm包了。


dnf install mysql mysql-server -y
systemctl enable --now mysqld

初始化:

[root@blogserver ~]# mysql_secure_installation 

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 

 ... skipping.


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? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 

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

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
All done!

验证阶段
#

验证apache是否正常运行,php是否成功加载 验证是否能正常访问 apache

/var/www/html/ 目录下新建一个php文件,测试php是否成功加载,内容如下

<?php
phpinfo();
?>