下面给你一套**在 CentOS 上安装 LAMP(Linux + Apache + MySQL/MariaDB + PHP)**的常见做法,适合 CentOS 7 / 8 / Stream,并分别说明关键差异。
yum update -y
# 或 CentOS 8 / Stream
dnf update -y
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
yum install httpd -y
dnf install httpd -y
systemctl start httpd
systemctl enable httpd
浏览器访问:
http://服务器IP
CentOS 默认用 MariaDB
yum install mariadb mariadb-server -y
# 或
dnf install mariadb mariadb-server -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
yum install epel-release -y
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
yum install yum-utils -y
yum-config-manager --enable remi-php74
yum install php php-mysql php-gd php-xml php-mbstring php-cli -y
dnf install php php-mysqlnd php-gd php-xml php-mbstring php-cli -y
systemctl restart httpd
vi /var/www/html/info.php
内容:
<?php
phpinfo();
浏览器访问:
http://服务器IP/info.php
✅ 看到 PHP 信息页面即成功
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
yum install php
systemctl restart httpd
mysqld 已启动127.0.0.1 而不是 localhost| 作用 | 路径 |
|---|---|
| 网站根目录 | /var/www/html |
| Apache 配置 | /etc/httpd/conf/httpd.conf |
| PHP 配置 | /etc/php.ini |
| MariaDB 配置 | /etc/my.cnf |
yum update -y
yum install httpd mariadb mariadb-server php php-mysql -y
systemctl start httpd mariadb
systemctl enable httpd mariadb
如果你需要:
告诉我你的 CentOS 版本和使用场景,我可以给你定制方案。