在CentOS上搭建LAMP(Linux, Apache, MySQL, PHP)环境是一个常见的任务,以下是详细的步骤:
1. 系统准备
更新系统:
sudo yum update -y
关闭防火墙:
sudo systemctl stop firewalldsudo systemctl disable firewalld
关闭SELinux:
sudo setenforce 0sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
配置IP地址:编辑
/etc/sysconfig/network-scripts/ifcfg-ens32
文件,设置静态IP:BOOTPROTO="static"ONBOOT="yes"IPADDR="192.168.92.110"NETMASK="255.255.255.0"GATEWAY="192.168.92.2"DNS1="8.8.8.8"DNS2="8.8.4.4"
然后重启网络服务:
sudo systemctl restart network
2. 安装Apache
安装Apache及其扩展:
sudo yum install httpd httpd-manual mod_ssl mod_perl -y
启动Apache服务:
sudo systemctl start httpdsudo systemctl enable httpd
访问验证:在浏览器中访问
http://192.168.92.110:80
,如果看到Apache的默认页面,说明安装成功。
3. 安装MySQL
安装MySQL:
sudo yum install mysql-community-server -y
启动MySQL服务:
sudo systemctl start mysqldsudo systemctl enable mysqld
配置MySQL安全性:
sudo mysql_secure_installation
4. 安装PHP
安装PHP及其扩展:
sudo yum install php php-mysql php-fpm -y
配置PHP支持MySQL:编辑
/etc/php.ini
文件,确保以下行未被注释:extension=mysql.so
重启Apache服务:
sudo systemctl restart httpd
测试PHP:创建一个
info.php
文件: