在 CentOS 8.2 64位系统中搭建 Nginx + PHP + MySQL (通常指 MariaDB,因为 CentOS 8 默认使用 MariaDB) 的完整步骤如下。我们将使用 Nginx、PHP-FPM 和 MariaDB 组合。
⚠️ 注意:CentOS 8 已于 2021 年底停止维护(EOL)。生产环境建议使用 Rocky Linux、AlmaLinux 或 Ubuntu LTS。以下教程仅适用于学习或临时测试环境。
📌 整体架构说明
- Web Server: Nginx
- Language Processor: PHP 7.4 (通过 PHP-FPM 运行)
- Database: MariaDB (MySQL 兼容)
✅ 第一步:更新系统并安装基础工具
sudo dnf update -y
sudo dnf install -y epel-release
sudo dnf install -y vim wget curl git net-tools firewalld
✅ 第二步:安装 Nginx
sudo dnf install -y nginx
启动 Nginx 并设置为开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
检查 Nginx 状态:
sudo systemctl status nginx
开放 HTTP/HTTPS 端口:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
验证 Nginx 是否正常运行:浏览器访问 http://<你的服务器IP>,应看到 Nginx 欢迎页面。
✅ 第三步:安装 MariaDB(MySQL 兼容)
sudo dnf install -y mariadb-server mariadb
启动 MariaDB 并设置开机自启:
sudo systemctl start mariadb
sudo systemctl enable mariadb
运行安全脚本初始化数据库:
sudo mysql_secure_installation
按提示操作:
- 输入当前 root 密码(首次为空,直接回车)
- 设置 root 密码 → Y
- 删除匿名用户 → Y
- 禁止 root 远程登录 → Y
- 删除 test 数据库 → Y
- 重新加载权限表 → Y
验证 MariaDB 是否正常运行:
sudo systemctl status mariadb
✅ 第四步:安装 PHP 7.4 及常用扩展
CentOS 8 默认仓库包含 PHP 7.2,我们推荐安装更稳定的 PHP 7.4。
方法一:使用 Remi 仓库(推荐)
# 安装 EPEL 和 Remi 仓库
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm
sudo dnf module list php # 查看可用模块
# 启用 PHP 7.4 模块
sudo dnf module enable php:remi-7.4
sudo dnf install -y php php-fpm php-mysqlnd php-pdo php-gd php-mbstring php-zip php-json php-opcache php-xml php-intl php-curl
方法二:直接使用默认仓库(PHP 7.2,不推荐用于新项目)
sudo dnf install -y php php-fpm php-mysqlnd php-pdo php-gd php-mbstring php-zip php-json php-opcache php-xml php-intl php-curl
💡 建议根据项目需求安装额外扩展,如
php-redis、php-memcached等。
✅ 第五步:配置 PHP-FPM
编辑 PHP-FPM 配置文件:
sudo vi /etc/php-fpm.d/www.conf
找到以下行并确保值如下:
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
保存退出。
启动 PHP-FPM 并设置开机自启:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
验证 PHP-FPM 是否运行:
sudo systemctl status php-fpm
✅ 第六步:配置 Nginx 与 PHP-FPM 集成
创建网站目录:
sudo mkdir -p /var/www/html/mywebsite
sudo chown -R nginx:nginx /var/www/html/mywebsite
sudo chmod -R 755 /var/www/html/mywebsite
创建测试 PHP 文件:
echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/mywebsite/index.php
创建 Nginx 站点配置文件:
sudo vi /etc/nginx/conf.d/mywebsite.conf
写入以下内容:
server {
listen 80;
server_name your_domain.com www.your_domain.com; # 替换为你的域名或 IP
root /var/www/html/mywebsite;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ .php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
}
🔁 如果只使用 IP 访问,将
server_name改为_或你的 IP 地址。
测试 Nginx 配置语法:
sudo nginx -t
如无错误,重载 Nginx:
sudo systemctl reload nginx
✅ 第七步:验证整个栈是否工作
浏览器访问:
http://<你的服务器IP>/index.php
你应该看到 PHP 信息页面,证明 Nginx + PHP-FPM 正常工作。
再测试数据库连接:
创建测试脚本 /var/www/html/mywebsite/test_db.php:
<?php
$link = mysqli_connect('localhost', 'root', 'your_root_password', 'mysql');
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
echo "Successfully connected to MySQL!";
mysqli_close($link);
?>
将 your_root_password 替换为你设置的 MariaDB root 密码。
访问 http://<你的服务器IP>/test_db.php,如果显示 “Successfully connected to MySQL!”,则说明数据库连接正常。
✅ 第八步(可选):创建数据库和用户(用于实际建站)
登录 MariaDB:
mysql -u root -p
执行 SQL:
CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
✅ 第九步:防火墙与 SELinux 配置(如需)
防火墙已配置(见第二步),确保 http/https 开放。
SELinux 配置(如果启用):
# 允许 Nginx 连接网络
sudo setsebool -P httpd_can_network_connect 1
# 如果需要 Nginx 读取其他目录,调整上下文
sudo chcon -Rv -t httpd_sys_content_t /var/www/html/mywebsite
✅ 第十步:优化与安全建议
- 禁用目录浏览:在 Nginx 配置中添加
autoindex off; - 限制 PHP 上传大小:编辑
/etc/php.ini:upload_max_filesize = 20M post_max_size = 20M max_execution_time = 300 - 重启 PHP-FPM 使配置生效:
sudo systemctl restart php-fpm - 定期更新系统:
sudo dnf update -y
🧪 最终验证清单
| 组件 | 命令 | 预期结果 |
|---|---|---|
| Nginx | systemctl status nginx |
active (running) |
| PHP-FPM | systemctl status php-fpm |
active (running) |
| MariaDB | systemctl status mariadb |
active (running) |
| Web 访问 | 浏览器访问 IP | Nginx 欢迎页或 PHP 信息 |
| DB 连接 | 访问 test_db.php | 成功连接 |
📦 后续建站建议
- 使用 WordPress、Laravel、Django 等框架时,只需将代码放入
/var/www/html/mywebsite,并根据框架要求配置数据库。 - 推荐使用 Let’s Encrypt 免费 SSL 证书实现 HTTPS:
sudo dnf install -y certbot python3-certbot-nginx sudo certbot --nginx -d your_domain.com
如有任何问题,请提供具体错误信息,我可以进一步帮你排查!
云知识CLOUD