下面给你一份**在 CentOS(以 CentOS 7 为例,CentOS 8 类似但仓库不同)搭建 LAMP(Linux + Apache + MySQL/MariaDB + PHP)**的实用教程。
# 更新系统
yum update -y
# 关闭 SELinux(生产环境可按需配置)
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
yum install httpd -y
systemctl start httpd
systemctl enable httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
浏览器访问服务器 IP,看到 Apache 测试页即成功。
yum install mariadb-server mariadb -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
按提示设置 root 密码、删除匿名用户等。
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-mbstring php-xml php-fpm -y
重启 Apache:
systemctl restart httpd
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
浏览器访问:
http://服务器IP/info.php
mkdir -p /var/www/example.com/public_html
chown -R apache:apache /var/www/example.com
vi /etc/httpd/conf.d/example.com.conf
示例配置:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
</Directory>
</VirtualHost>
systemctl restart httpd
php 模块已安装并重启 Apachednf,MySQL 可用官方源或 MariaDB如果你告诉我:
我可以给你更精准的方案。