在 CentOS 下安装 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 是非常常见的操作。下面我分别给出 CentOS 7 和 CentOS 8/Stream 的安装方式(因为两者包管理略有差异)。
yum update -y
yum install -y httpd
启动并设置开机自启:
systemctl start httpd
systemctl enable httpd
验证:
systemctl status httpd
浏览器访问服务器 IP,看到 Apache 测试页即成功。
yum install -y mariadb mariadb-server
启动并设置开机自启:
systemctl start mariadb
systemctl enable mariadb
初始化数据库安全设置:
mysql_secure_installation
yum install -y php php-mysql php-gd php-xml php-mbstring
重启 Apache:
systemctl restart httpd
创建测试文件:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
浏览器访问:
http://服务器IP/info.php
✅ 看到 PHP 信息页面说明成功。
CentOS 8 默认使用
dnf,但yum仍然可用。
dnf update -y
dnf install -y httpd
systemctl start httpd
systemctl enable httpd
dnf install -y mariadb mariadb-server
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
dnf install -y php php-mysqlnd php-gd php-xml php-mbstring
systemctl restart httpd
如果无法访问网页,通常是防火墙问题。
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
/var/www/html
vim /etc/php.ini
tail -f /var/log/httpd/error_log
CentOS 7:
yum install -y httpd mariadb mariadb-server php php-mysql
systemctl start httpd mariadb
systemctl enable httpd mariadb
CentOS 8:
dnf install -y httpd mariadb mariadb-server php php-mysqlnd
systemctl start httpd mariadb
systemctl enable httpd mariadb
如果你需要:
可以直接告诉我你的 CentOS 版本和使用场景,我可以给你更精确的方案。