在 CentOS 8.2 环境下通过 yum 安装 WordPress,核心逻辑其实是“先搭建 LNMP(Linux + Nginx/Apache + MySQL/MariaDB + PHP)环境,再部署 WordPress 代码”。
需要特别说明的是:CentOS 8 系列已于 2021 年底停止维护(EOL)。虽然技术上可以操作,但在生产环境中强烈建议迁移至 Rocky Linux、AlmaLinux 或 CentOS Stream。以下步骤基于 CentOS 8.2 的包管理器行为编写,适用于学习或测试环境。
一、前置准备:更新系统与基础工具
sudo yum update -y
sudo yum install epel-release -y
sudo yum install wget curl vim net-tools -y
注意:CentOS 8 默认使用
dnf,但yum是dnf的软链接,命令通用。
二、安装 Web 服务器(推荐 Nginx)
Apache 也可以,但 Nginx 在高并发下表现更好,且与 WordPress 配合更灵活。
sudo yum install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
检查状态:
systemctl status nginx
确保防火墙放行 HTTP/HTTPS:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
三、安装数据库(MariaDB)
CentOS 8 默认仓库包含 MariaDB 10.3,适合 WordPress。
sudo yum install mariadb-server mariadb -y
sudo systemctl enable mariadb
sudo systemctl start mariadb
初始化数据库安全设置:
sudo mysql_secure_installation
按提示设置 root 密码、移除匿名账户、禁止 root 远程登录等。
创建 WordPress 专用数据库和用户:
mysql -u root -p
进入 MySQL 命令行后执行:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
四、安装 PHP 及必要扩展
WordPress 对 PHP 版本要求较高,建议安装 PHP 7.4 或更高(CentOS 8 默认可能是 PHP 7.2/7.4)。
sudo yum install php php-fpm php-mysqlnd php-json php-gd php-mbstring php-xml php-zip php-curl -y
启动并启用 PHP-FPM:
sudo systemctl enable php-fpm
sudo systemctl start php-fpm
关键点:Nginx 不直接解析 PHP,需通过 FastCGI 与 PHP-FPM 通信。
五、配置 Nginx 支持 WordPress
创建站点配置文件:
sudo vim /etc/nginx/conf.d/wordpress.conf
写入以下内容(替换 example.com 为你的域名或 IP):
server {
listen 80;
server_name example.com www.example.com;
root /usr/share/nginx/html/wordpress;
index index.php index.html index.htm;
client_max_body_size 64M;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
# 禁止访问隐藏文件如 .git, .env
location ~ /. {
deny all;
}
# 静态资源缓存
location ~* .(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
测试配置语法:
sudo nginx -t
如无错误,重载 Nginx:
sudo systemctl reload nginx
六、下载并部署 WordPress
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
sudo cp -r wordpress/* /usr/share/nginx/html/wordpress/
设置权限(重要!避免后续上传主题/插件报错):
sudo chown -R nginx:nginx /usr/share/nginx/html/wordpress
sudo chmod -R 755 /usr/share/nginx/html/wordpress
安全提示:不要将目录设为
777,这会带来严重安全风险。
七、浏览器完成安装
- 打开浏览器,访问
http://your_server_ip或域名。 - 选择语言,点击“开始”。
- 输入之前创建的数据库信息:
- 数据库名:
wordpress - 用户名:
wp_user - 密码:你设置的强密码
- 数据库主机:
localhost - 表前缀:
wp_(可保留默认)
- 数据库名:
- 点击“提交”,然后运行安装程序。
- 填写站点标题、管理员账号、密码、邮箱。
- 完成安装,登录后台。
八、可选优化:SSL 证书(Let’s Encrypt)
生产环境务必启用 HTTPS:
sudo yum install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
按提示操作,Certbot 会自动修改 Nginx 配置并重定向 HTTP 到 HTTPS。
常见问题排查
| 问题 | 可能原因 | 解决方案 |
|---|---|---|
| 502 Bad Gateway | PHP-FPM 未启动或端口/socket 不对 | 检查 systemctl status php-fpm;确认 Nginx 配置中 fastcgi_pass 路径正确 |
| 无法上传媒体文件 | 权限不足 | 检查 /usr/share/nginx/html/wordpress/wp-content/uploads 是否属主为 nginx |
| 页面样式错乱 | 静态资源被拦截 | 检查 Nginx location ~* .(js|css...)$ 是否正确配置 |
| WordPress 提示需要 FTP | 文件系统权限问题 | 确保所有文件属主为 nginx,且无写权限给其他用户 |
最后提醒
- 备份:安装完成后立即备份数据库和
wp-content目录。 - 安全加固:修改默认登录 URL、禁用 XML-RPC、安装防火墙插件(如 Wordfence)。
- 系统升级:由于 CentOS 8 EOL,建议尽快迁移至支持长期维护的发行版。
这套流程覆盖了从底层服务到应用部署的完整链路,适用于大多数标准 VPS 环境。如有特定需求(如 Redis 缓存、OPcache 调优),可在此基础上进一步扩展。
云技术博客