在Redhat 7 Linux上安装LAMP(Linux,Apache,Mariadb,PHP)堆栈

安装Apache WebServer

首先,我们需要安装Apache WebServer。
包和服务被称为“httpd`:

[root@rhel7 ~]# yum install httpd

开始httpd服务:

[root@rhel7 ~]# service httpd start
Redirecting to /bin/systemctl start  httpd.service

启用防火墙以允许HTTP端口80访问:

[root@rhel7 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@rhel7 ~]# firewall-cmd --reload

确保我们的WebServer,即“httpd守护程序”在重新启动后启动:

[root@rhel7 ~]# systemctl enable httpd

创建一个简单HTML页面以测试所有上述步骤是否正常:

[root@rhel7 ~]# echo "APACHE HTML TEST" > /var/www/html/index.html

使用浏览器打开Web服务器的主机名或者IP地址。
http://服务器ip

安装MariaDB数据库

安装MariaDB数据库,同时还需要安装“PHP-MySQL"模块,用于PHP与数据库通信:

[root@rhel7 ~]# yum install php-mysql mariadb-server
[root@rhel7 ~]# service httpd restart

启动MariaDb数据库并设置重新启动后启动:

[root@rhel7 ~]# service mariadb start
[root@rhel7 ~]# systemctl enable mariadb

测试:

创建文件/var/www/html/connect.php并添加下面内容

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lamp";
//创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
//判断连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接数据库成功";
?>

使用浏览器打开
http://服务器ip/connect.php

与数据库的连接失败的原因是因为我们尚未创建任何数据库。

让我们创建名为“lamp”的数据库并重试我们的测试:

[root@rhel7 ~]# mysql -u root -e "create database lamp"

故障排除

如果我们收到以下错误:

Database connection failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

确保已启动MariaDB服务器:

[root@rhel7 ~]# service mariadb start

如果收到以下错误消息:

Sep 04 17:20:12 rhel7 httpd[2234]: AH00557: httpd: apr_sockaddr_info_get() failed for rhel7
Sep 04 17:20:12 rhel7 httpd[2234]: AH00558: httpd: Could not reliably determine 
the server's fully qualified domain name, using 127.0.0.1. Set the 'Server...his message

修复方法是:

[root@rhel7 ~]# echo 'ServerName 127.0.0.1' >> /etc/httpd/conf/httpd.conf
[root@rhel7 ~]# service httpd restart

PHP支持的安装

添加PHP支持。
要执行此操作,只需安装php包并重新启动我们的httpd守护程序:

[root@rhel7 ~]# yum install php
[root@rhel7 ~]# service httpd restart

要测试PHP安装,创建文件/var/www/html/index.php并添加下面的内容:

<?php
    phpinfo();
?>

浏览器打开http://服务器ip/index.php 将可以看到php有关的信息。

日期:2020-07-07 20:55:00 来源:oir作者:oir