如何在RHEL、CentOS、Rocky和AlmaLinux上安装LAMP服务器

A LAMP stack is a collection of four different software (Linux, Apache, MySQL, and PHP) that programmers or web developers use to create and deploy websites or applications.

这个教程将集中介绍如何在最新版本的RHEL和基于RHEL的发行版(如CentOS、Oracle Linux、Rocky和AlmaLinux)上安装和配置著名的LAMP堆栈与PhpMyAdmin。

要求

根据所使用的发行版,使用以下链接执行最小系统安装,使用静态IP地址进行网络配置。

步骤1:安装Apache Web服务器

1. 完成最小系统安装并使用静态IP地址配置服务器网络接口后,请使用以下yum命令从官方仓库安装Apache(httpd)服务二进制包。

# yum install httpd
Install Apache on Linux

2. 完成Apache安装后,使用以下命令管理Apache守护进程,因为RHEL和CentOS都将它们的init脚本从SysV迁移到了Systemd为什么init在Linux中被Systemd取代?

# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd
Manage Apache in Linux

3. 接下来,确保通过使用firewall-cmd在防火墙上打开端口80443来允许访问Apache,这是通过守护进程管理Firewalld的默认命令。

# firewall-cmd --add-service=http
# firewall-cmd --add-service=https
# systemctl restart firewalld
NOTE: Make notice that using this rule will lose its effect after a system reboot or firewalld service restart because it opens on-fly rules, which are not applied permanently.

为了在防火墙上应用一致的iptables规则,使用--permanent选项并重启firewalld服务以使其生效。

# firewall-cmd --permanent --add-service=http
# firewall-cmd --permanent --add-service=https
# systemctl restart firewalld
Open Apache Ports in Firewalld

以下是管理防火墙的一些重要Firewalld命令:

# firewall-cmd --state
# firewall-cmd --list-all
# firewall-cmd --list-interfaces
# firewall-cmd --get-service
# firewall-cmd --query-service service_name
# firewall-cmd --add-port=80/tcp

4. 要验证Apache功能,请在远程浏览器中输入服务器IP地址,使用HTTP协议在URL中,并应出现默认页面,如下面的截图所示。

http://server_IP
Verify Apache in Linux

5. 目前,Apache的DocumentRoot路径设置为/var/www/html系统路径,默认情况下不提供任何索引文件。如果您想查看您的DocumentRoot路径的目录列表。

打开Apache的welcome配置文件,并将Indexes语句从更改为+LocationMatch指令上,以下面的截图为例。

# vi /etc/httpd/conf.d/welcome.conf
Configure Apache Welcome Page

6. 更改后,关闭文件,重新启动Apache服务以反映更改,并重新加载浏览器页面以查看最终结果。

# systemctl restart httpd
Apache Directory Listing

第二步:为Apache安装PHP支持

7. 在为Apache安装PHP动态语言支持之前,使用以下命令获取可用PHP模块和扩展的完整列表。

# yum search php
List PHP Modules

8. 根据您想要使用的应用程序类型,从上述列表中安装所需的PHP模块,但对于在PHP中支持MySQL/MariaDBPhpMyAdmin,您需要安装以下模块。

# yum install php php-mysql php-pdo php-gd php-mbstring
Install PHP Modules

9.要从浏览器获取PHP的完整信息列表,使用以下命令在Apache文档根目录创建一个info.php文件,重新启动httpd服务,并将浏览器定向到http://server_IP/info.php地址。

# echo "<?php phpinfo(); ?>" > /var/www/html/info.php
# systemctl restart httpd
List PHP Information

10.如果在PHP日期和时区上出现错误,请打开php.ini配置文件,搜索并取消注释date.timezone语句,附加您的物理位置,并重新启动Apache守护程序。

# vi /etc/php.ini

找到并更改date.timezone行,使其如下所示,使用PHP支持的时区列表。

date.timezone = Continent/City
Set PHP Timezone

第三步:安装和配置MariaDB数据库

11.RHEL发行版已将默认数据库管理系统从MySQL更改为MariaDB。 使用以下命令安装MariaDB数据库。

# yum install mariadb-server mariadb
Install MariaDB on Linux

12.安装MariaDB软件包后,启动数据库守护程序并使用mysql_secure_installation脚本保护数据库(设置根密码,禁用根用户的远程登录,删除测试数据库并删除匿名用户)。

# systemctl start mariadb
# systemctl enable mariadb
# systemctl status mariadb
# mysql_secure_installation
Manage MariaDB Service
Secure MariaDB in Linux

13.为了测试数据库功能,使用其根帐户登录到MariaDB,然后使用quit语句退出。

mysql -u root -p
MariaDB > show databases;
MariaDB > quit
Connect MariaDB to Linux

步骤 4:安装 PhpMyAdmin 来管理 MySQL

14. 默认情况下,官方仓库不提供任何 PhpMyAdmin Web 界面的二进制软件包。如果您不习惯使用 MySQL 命令行来管理数据库,您可以通过启用以下命令来安装 PhpMyAdmin 软件包。

# yum install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm  [On CentOS/RHEL 8]
# yum install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm  [On CentOS/RHEL 8]
# yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm  [On CentOS/RHEL 7]

启用 remi 仓库后,接下来安装 PhpMyAdmin

# yum install phpmyadmin
Install PhpMyAdmin on Linux

15. 接下来,通过编辑位于 Apache conf.d 目录下的 phpmyadmin.conf 文件来配置 PhpMyAdmin,并注释以下行。

# vi /etc/httpd/conf.d/phpMyAdmin.conf

在 “Require local” 行下添加 “Require all granted” 行,如下所示。

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8
   Require local
   Require all granted
</Directory>
Allow Remote PhpMyAdmin Access

16. 要能够登录到 PhpMyAdmin Web 界面,请重新启动 Apache Web 服务,并将浏览器指向 URL 地址。

# systemctl restart httpd

http://server_IP/phpmyadmin/
PhpMyAdmin Dashboard

步骤 5:在 Linux 系统中系统范围启用 LAMP

17. 如果您需要在重新启动后自动启动 MariaDBApache 服务,请发出以下命令以在系统范围内启用它们。

# systemctl enable mariadb
# systemctl enable httpd

这就是在基于RHEL的发行版上进行基本 LAMP 安装所需的全部步骤。接下来的一系列文章将讨论如何创建虚拟主机、生成SSL证书和密钥,并为Apache HTTP Server添加SSL事务支持。

Source:
https://www.tecmint.com/install-lamp-server-linux/