在 CentOS 8.2 上部署 WordPress 需要安装 Web 服务器(Apache/Nginx)、数据库(MySQL/MariaDB)和 PHP。以下是基于 LAMP 架构(Linux + Apache + MariaDB + PHP)的完整配置步骤,适用于生产环境或开发测试环境。
一、系统更新与基础准备
sudo dnf update -y
sudo dnf install epel-release -y
sudo dnf install wget git vim -y
二、安装并配置 Apache 服务
1. 安装 Apache 和相关模块
sudo dnf install httpd mod_ssl -y
2. 启动并设置开机自启
sudo systemctl start httpd
sudo systemctl enable httpd
3. 开放防火墙端口(HTTP/HTTPS)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
✅ 若使用 SELinux,还需允许 Apache 访问自定义目录(见后文)。
三、安装并配置 MariaDB(MySQL 兼容)
1. 安装 MariaDB 服务器
sudo dnf install mariadb-server mariadb -y
2. 启动并设置开机自启
sudo systemctl start mariadb
sudo systemctl enable mariadb
3. 安全初始化(交互式设置)
sudo mysql_secure_installation
按提示操作:
- 设置 root 密码
- 删除匿名用户
- 禁止 root 远程登录
- 删除测试数据库
- 重新加载权限表
4. 创建 WordPress 专用数据库和用户
sudo mysql -u root -p
进入 MySQL 后执行:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost';
FLUSH PRIVILEGES;
EXIT;
🔐 请将
StrongPassword123!替换为强密码。
四、安装 PHP 及所需扩展
1. 安装 PHP 7.4(CentOS 8.2 默认仓库可能较旧,建议启用 Remi 源)
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
sudo dnf module reset php -y
sudo dnf module enable php:remi-7.4 -y
2. 安装 PHP 核心及 WordPress 所需扩展
sudo dnf install php php-fpm php-mysqlnd php-gd php-json php-mbstring php-xml php-xmlrpc php-zip php-curl php-intl php-opcache -y
3. 配置 PHP 时区(可选但推荐)
编辑 /etc/php.ini:
date.timezone = Asia/Shanghai
修改后重启 PHP-FPM 或 Apache(若用 mod_php)。
4. 启动 PHP-FPM(若使用 FastCGI)
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
⚠️ 注意:CentOS 8.2 中 Apache 通常搭配
mod_php(即直接集成),无需单独配 FPM;但若追求性能可改用 Nginx + PHP-FPM。本指南以 Apache + mod_php 为主。
五、下载并部署 WordPress
1. 创建网站目录
sudo mkdir -p /var/www/html/wordpress
sudo chown -R apache:apache /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
2. 下载 WordPress 最新版
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
sudo mv wordpress/* /var/www/html/wordpress/
sudo rm -rf wordpress latest.tar.gz
3. 设置 SELinux 上下文(如启用)
sudo setsebool -P httpd_can_network_connect on
sudo restorecon -Rv /var/www/html/wordpress
4. 生成 wp-config.php(自动配置更安全)
sudo cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php
sudo nano /var/www/html/wordpress/wp-config.php
修改以下行:
define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wordpress' );
define( 'DB_PASSWORD', 'StrongPassword123!' );
define( 'DB_HOST', 'localhost' );
保存退出。
六、配置 Apache 虚拟主机(推荐做法)
1. 创建站点配置文件
sudo nano /etc/httpd/conf.d/wordpress.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html/wordpress
<Directory /var/www/html/wordpress>
AllowOverride All
Require all granted
</Directory>
ErrorLog logs/example-error.log
CustomLog logs/example-access.log combined
</VirtualHost>
🔁 将
example.com替换为你的域名。
2. 重启 Apache 使配置生效
sudo systemctl restart httpd
七、完成 WordPress 安装向导
- 浏览器访问:
http://your_server_ip或http://example.com - 选择语言 → 点击“继续”
- 填写数据库信息(通常已自动识别)
- 设置站点标题、用户名、密码、邮箱
- 完成安装!
八、安全加固建议(生产环境必做)
1. 禁用文件编辑功能(防止恶意插件篡改)
在 wp-config.php 末尾添加:
define( 'DISALLOW_FILE_EDIT', true );
2. 限制 wp-admin 访问(可选)
通过 .htaccess 限制 IP:
<Files "wp-login.php">
Order Deny,Allow
Deny from all
Allow from YOUR_IP_ADDRESS
</Files>
3. 安装 SSL(Let’s Encrypt)
sudo dnf install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com
按提示输入邮箱并接受条款,自动配置 HTTPS。
4. 定期备份
# 示例脚本:备份数据库 + 文件
#!/bin/bash
DATE=$(date +%Y%m%d)
BACKUP_DIR="/backup"
mkdir -p $BACKUP_DIR
mysqldump -u wordpress -p'StrongPassword123!' wordpress > $BACKUP_DIR/db_$DATE.sql
tar -czf $BACKUP_DIR/files_$DATE.tar.gz /var/www/html/wordpress
find $BACKUP_DIR -name "*.sql" -mtime +7 -delete
find $BACKUP_DIR -name "*.tar.gz" -mtime +7 -delete
九、验证与故障排查
- 检查 Apache 状态:
systemctl status httpd - 查看错误日志:
tail -f /var/log/httpd/error_log - 测试 PHP:创建
info.php含<?php phpinfo(); ?>,访问http://your_ip/info.php(用完删除) - 确保数据库连接正常:WordPress 后台应能正常显示
✅ 至此,WordPress 已在 CentOS 8.2 上成功部署!
如需进一步优化(如启用缓存、CDN、WAF 等),可结合 Cloudflare、Redis、WP Super Cache 等工具。
需要 Nginx + PHP-FPM 版本或 Docker 部署方案,也可告诉我,我可提供对应步骤。
CLOUD技术笔记