在 CentOS 系统下搭建 WordPress 可以通过 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)或 LAMP(Linux + Apache + MySQL/MariaDB + PHP)两种方式实现。以下是详细步骤,适用于 CentOS 7/8/Stream。
🌐 准备工作
-
更新系统:
sudo yum update -y(CentOS 8 或 Stream 使用
dnf) -
安装常用工具:
sudo yum install wget curl vim net-tools epel-release -y
✅ 方式一:LNMP 搭建 WordPress(推荐用于高性能场景)
1️⃣ 安装 Nginx
sudo yum install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
验证:访问服务器 IP,应看到 Nginx 欢迎页。
2️⃣ 安装 MariaDB(MySQL 替代品)
sudo yum install mariadb-server mariadb -y
sudo systemctl enable mariadb
sudo systemctl start mariadb
运行安全配置脚本:
sudo mysql_secure_installation
按提示设置 root 密码、移除匿名用户等。
3️⃣ 安装 PHP 与相关模块(WordPress 所需)
安装 PHP 7.4 或 8.0(建议使用 Remi 源):
# 安装 Remi 源
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-$(rpm -E %rhel).rpm -y
# 启用 PHP 7.4 或 8.0 模块(以 7.4 为例)
sudo yum module enable php:7.4 -y
# 安装 PHP 和扩展
sudo yum install php php-fpm php-mysqlnd php-gd php-xml php-mbstring php-zip php-curl -y
启动并启用 PHP-FPM:
sudo systemctl enable php-fpm
sudo systemctl start php-fpm
4️⃣ 配置 Nginx 支持 PHP
编辑站点配置文件:
sudo vim /etc/nginx/conf.d/wordpress.conf
写入以下内容(替换 your_domain_or_ip):
server {
listen 80;
server_name your_domain_or_ip;
root /var/www/html/wordpress;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
}
测试并重启 Nginx:
sudo nginx -t
sudo systemctl restart nginx
5️⃣ 创建数据库和用户
登录 MariaDB:
mysql -u root -p
执行 SQL:
CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
6️⃣ 下载并部署 WordPress
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo chown -R apache:apache wordpress
sudo chmod -R 755 wordpress
注意:Nginx 使用
apache用户运行 PHP-FPM 是默认行为。也可改为nginx用户。
7️⃣ 访问并完成安装
浏览器访问:
http://your_server_ip/wordpress
按照向导填写数据库信息:
- 数据库名:
wordpress - 用户名:
wpuser - 密码:你设置的密码
- 数据库主机:
localhost - 表前缀:可保持
wp_
继续完成安装即可。
✅ 方式二:LAMP 搭建 WordPress(传统稳定方案)
1️⃣ 安装 Apache
sudo yum install httpd -y
sudo systemctl enable httpd
sudo systemctl start httpd
防火墙放行:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
2️⃣ 安装 MariaDB(同上)
sudo yum install mariadb-server mariadb -y
sudo systemctl enable mariadb
sudo systemctl start mariadb
sudo mysql_secure_installation
3️⃣ 安装 PHP(同上,略作调整)
sudo yum module enable php:7.4 -y
sudo yum install php php-mysqlnd php-gd php-xml php-mbstring php-zip php-curl -y
无需安装 php-fpm,Apache 会直接处理 PHP。
重启 Apache:
sudo systemctl restart httpd
4️⃣ 创建数据库(同 LNMP 步骤)
CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
5️⃣ 部署 WordPress
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo chown -R apache:apache wordpress
sudo chmod -R 755 wordpress
6️⃣ 访问安装页面
http://your_server_ip/wordpress
填写数据库信息,完成安装。
🔐 安全建议
- 修改 WordPress 目录权限:
find /var/www/html/wordpress -type d -exec chmod 755 {} ; find /var/www/html/wordpress -type f -exec chmod 644 {} ; - 使用
.htaccess(仅 LAMP)增强安全。 - 定期更新系统、PHP、WordPress。
- 考虑使用 Let’s Encrypt 配置 HTTPS。
🧰 常见问题排查
| 问题 | 解决方法 |
|---|---|
| 白屏或 500 错误 | 查看 /var/log/php-fpm/www-error.log 或 /var/log/httpd/error_log |
| 无法连接数据库 | 检查用户名、密码、数据库名是否正确 |
| 文件上传失败 | 检查 PHP upload_max_filesize 和目录权限 |
| Nginx 显示 403 | 检查 index.php 是否存在,目录权限是否正确 |
✅ 总结
| 架构 | 特点 |
|---|---|
| LNMP | 更高效,适合高并发,使用 Nginx + PHP-FPM |
| LAMP | 更简单,兼容性好,Apache 内建支持 PHP |
根据需求选择即可。生产环境推荐 LNMP,开发或小站可用 LAMP。
如需自动化脚本,可使用 宝塔面板 或 lnmp.org 一键包。
如有具体版本或错误信息,欢迎提供进一步帮助!
云服务器