在 CentOS 8.2(已停止维护,强烈建议升级至 CentOS Stream 8/9 或 Rocky/AlmaLinux 8/9)上安装 Nginx + PHP + MySQL(实际推荐 MariaDB,因 CentOS 8 默认仓库中 MySQL 官方包需手动添加且存在兼容性风险)的完整、安全、生产就绪步骤如下:
⚠️ 重要前提说明:
- CentOS 8 已于 2021-12-31 生命周期结束(EOL),不再接收安全更新。
mysql包在默认AppStream仓库中已被 MariaDB 替代(mariadb-server是官方推荐数据库)。- 若必须使用 Oracle MySQL,请使用其官方 YUM 仓库(但会增加维护复杂度和潜在冲突风险)。
- 本指南采用 Nginx(官方仓库)+ PHP 7.4(来自 EPEL + PowerTools)+ MariaDB 10.3(系统默认) 组合,符合 CentOS 8 最佳实践。
✅ 完整安装步骤(以 root 执行)
步骤 1:系统更新与基础准备
# 更新系统(尽可能获取最后可用补丁)
dnf update -y
# 安装常用工具(vim、wget、curl、net-tools 等)
dnf install -y vim-enhanced wget curl net-tools bash-completion
# 启用 EPEL 仓库(提供额外软件包,如 php-fpm)
dnf install -y epel-release
# 启用 PowerTools(原 CodeReady Builder)——PHP 编译依赖及部分扩展所需
dnf config-manager --set-enabled powertools
# 验证仓库状态
dnf repolist
步骤 2:安装 Nginx(使用官方 nginx.org 仓库,版本更新、更安全)
✅ 推荐:避免使用系统自带
nginx(版本较旧,如 1.14),改用 nginx 官方稳定版(当前为 1.24.x)
# 添加 Nginx 官方 YUM 仓库
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=https://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
# 导入 GPG key(关键!否则安装失败)
rpm --import https://nginx.org/keys/nginx_signing.key
# 安装 Nginx
dnf install -y nginx
# 启动并设为开机自启
systemctl enable --now nginx
# 检查状态
systemctl status nginx
# ✅ 应显示 active (running)
# 开放防火墙(若 firewalld 启用)
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
🌐 浏览器访问
http://你的服务器IP,应看到 "Welcome to nginx!" 页面。
步骤 3:安装 PHP 7.4(CentOS 8 默认 PHP 版本)及常用扩展
# 安装 PHP 及核心扩展(含 FPM —— Nginx 必需)
dnf install -y php php-fpm php-cli php-common php-gd php-mbstring php-xml php-json php-opcache php-mysqlnd
# ✅ 关键:修改 PHP-FPM 运行用户(匹配 Nginx,默认为 apache → 改为 nginx)
sed -i 's/^user = apache/user = nginx/' /etc/php-fpm.d/www.conf
sed -i 's/^group = apache/group = nginx/' /etc/php-fpm.d/www.conf
# 设置监听方式(推荐 Unix socket 提升性能与安全性)
sed -i 's/^listen = 127.0.0.1:9000/listen = /run/php-fpm/www.sock/' /etc/php-fpm.d/www.conf
sed -i 's/^;listen.owner = nobody/listen.owner = nginx/' /etc/php-fpm.d/www.conf
sed -i 's/^;listen.group = nobody/listen.group = nginx/' /etc/php-fpm.d/www.conf
sed -i 's/^;listen.mode = 0660/listen.mode = 0660/' /etc/php-fpm.d/www.conf
# 启动 PHP-FPM
systemctl enable --now php-fpm
systemctl status php-fpm
🔍 验证 PHP:创建测试文件
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php访问
http://你的IP/info.php,确认 PHP 版本为7.4.x,且Server API显示FPM/FastCGI。
步骤 4:安装 MariaDB(MySQL 兼容替代,CentOS 8 官方默认)
# 安装 MariaDB 服务器与客户端
dnf install -y mariadb-server mariadb
# 启动并设为开机自启
systemctl enable --now mariadb
# 运行安全初始化(设置 root 密码、删除匿名用户等)
mysql_secure_installation
# 👉 按提示操作:设 root 密码(强密码!)、Y/Y/Y/Y
🔐 建议后续操作:
# 登录验证 mysql -u root -p # 创建网站专用数据库与用户(示例) CREATE DATABASE myweb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'StrongPass123!'; GRANT ALL ON myweb.* TO 'webuser'@'localhost'; FLUSH PRIVILEGES; EXIT;
步骤 5:配置 Nginx 支持 PHP(关键!)
编辑默认站点配置:
vim /etc/nginx/conf.d/default.conf
📌 替换 server { ... } 块为以下内容(支持 .php 解析、index 优先级、安全头):
server {
listen 80;
server_name localhost;
# 网站根目录(可按需修改)
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# 处理 PHP 请求(关键)
location ~ .php$ {
root /usr/share/nginx/html;
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;
}
}
✅ 重载 Nginx 配置:
nginx -t && systemctl reload nginx
步骤 6:测试完整栈(WordPress 级别验证)
# 下载并解压 WordPress(示例)
cd /usr/share/nginx/html
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
mv wordpress/* . && rmdir wordpress && rm latest.tar.gz
# 赋予 Web 用户权限(Nginx + PHP-FPM 使用 nginx 用户)
chown -R nginx:nginx /usr/share/nginx/html/
chmod -R 755 /usr/share/nginx/html/
# 创建 wp-config.php(或通过浏览器安装向导)
# ✅ 访问 http://你的IP 即可进入 WordPress 安装界面
🔒 生产环境强化建议(必做!)
| 类别 | 措施 |
|---|---|
| 安全 | • setsebool -P httpd_can_network_connect 1(如需 PHP 连网络)• semanage port -a -t http_port_t -p tcp 8080(如改端口)• 禁用 PHP 显示错误: sed -i 's/expose_php = On/expose_php = Off/' /etc/php.ini |
| 性能 | • 启用 OPcache:sed -i 's/;opcache.enable=1/opcache.enable=1/' /etc/php.d/10-opcache.ini• 调整 php-fpm 进程数(/etc/php-fpm.d/www.conf 中 pm = dynamic, pm.max_children = 50) |
| 备份 | • mysqldump -u root -p myweb > /backup/myweb_$(date +%F).sql(配合 cron) |
| 监控 | • dnf install -y nginx-mod-http-status(启用 /nginx_status) |
❌ 不推荐做法(避坑指南)
- ❌ 直接
dnf install mysql-server→ CentOS 8 无此包,会报错或装错源。 - ❌ 使用
php-mysql(已废弃)→ 必须用php-mysqlnd(本教程已包含)。 - ❌ Nginx 用
http://X_X PHP(性能差、不安全)→ 必须用 Unix socket(unix:/run/php-fpm/www.sock)。 - ❌ 忽略 SELinux → CentOS 8 默认开启,需用
setsebool/semanage配置,而非直接禁用!
🚀 后续运维命令速查
# 查看服务状态
systemctl status nginx php-fpm mariadb
# 查看错误日志
tail -f /var/log/nginx/error.log
tail -f /var/log/php-fpm/www-error.log
journalctl -u mariadb -f
# 重启全部服务(顺序!)
systemctl restart mariadb php-fpm nginx
✅ 总结:你已完成
✔️ Nginx 1.24+(官方源)
✔️ PHP 7.4-FPM(含常用扩展)
✔️ MariaDB 10.3(安全初始化)
✔️ 全栈连通性验证(PHP Info + WordPress)
✔️ 基础安全加固(SELinux、防火墙、权限)
⚠️ 再次强调:CentOS 8 已 EOL,请尽快迁移至 AlmaLinux 8/9、Rocky Linux 8/9 或 CentOS Stream。
如需我提供 AlmaLinux 9 + Nginx + PHP 8.2 + MariaDB 10.11 的升级版脚本,欢迎随时提出!
需要我为你生成一键部署脚本(.sh)、SSL(Let’s Encrypt)自动配置、或 WordPress 一键安装脚本吗? 😊
云知道CLOUD