下面以 CentOS 7/8(含 Stream) 为例,说明如何搭建 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 并部署一个网站。
# 更新系统
yum update -y
# 关闭防火墙(测试环境,生产不建议)
systemctl stop firewalld
systemctl disable firewalld
# 或开放端口(推荐)
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
yum install httpd -y
systemctl start httpd
systemctl enable httpd
测试:浏览器访问 http://服务器IP
yum install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb
# 初始化
mysql_secure_installation
创建数据库示例:
mysql -u root -p
CREATE DATABASE testdb;
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON testdb.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
yum install php php-mysqlnd php-fpm -y
systemctl restart httpd
测试 PHP:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
访问 http://IP/info.php
# 网站文件放到
/var/www/html/
访问:
http://服务器IP/
mkdir -p /var/www/example.com/public_html
vi /etc/httpd/conf.d/example.conf
内容:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
systemctl restart httpd
# 可用 scp / ftp / git
scp -r ./web/* root@IP:/var/www/example.com/public_html
直接放 HTML 文件即可
# 下载
cd /var/www/example.com/public_html
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
配置数据库后访问安装向导
确保:
AllowOverride All
并启用:
httpd -M | grep rewrite
chown -R apache:apache /var/www/example.com
chmod -R 755 /var/www/example.com
✅ 403 错误
setenforce 0
# 或修改
chcon -R -t httpd_sys_content_t /var/www
✅ 无法连接数据库
如果你告诉我:
我可以给你 更精确的部署步骤 或 一键脚本。