在 CentOS 系统下,可以通过 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)或 LAMP(Linux + Apache + MySQL/MariaDB + PHP)环境来搭建 WordPress。以下是两种方式的详细步骤。
一、准备工作
1. 更新系统
sudo yum update -y
2. 安装常用工具(可选)
sudo yum install wget curl vim net-tools epel-release -y
二、方法一:使用 LNMP 搭建 WordPress
步骤 1:安装 Nginx
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
验证:访问服务器 IP 地址,应看到 Nginx 欢迎页。
步骤 2:安装 MariaDB(MySQL 替代品)
sudo yum install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
运行安全配置脚本:
sudo mysql_secure_installation
按提示设置 root 密码、移除匿名用户等。
步骤 3:创建 WordPress 数据库和用户
登录 MariaDB:
mysql -u root -p
执行以下 SQL 命令:
CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
步骤 4:安装 PHP 和相关扩展
LNMP 推荐使用 PHP-FPM:
sudo yum install php php-fpm php-mysql php-gd php-xml php-mbstring php-json -y
启动并启用 PHP-FPM:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
步骤 5:配置 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;
}
}
重启 Nginx:
sudo systemctl restart nginx
步骤 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 默认运行用户是
nginx,但 PHP-FPM 配置可能用apache。建议统一用户权限。
修改 /etc/php-fpm.d/www.conf 中的用户组为 nginx:
user = nginx
group = nginx
然后:
sudo chown -R nginx:nginx /var/www/html/wordpress
sudo systemctl restart php-fpm
步骤 7:完成 WordPress 安装
访问 http://你的服务器IP/wordpress,按照向导填写数据库信息:
- 数据库名:
wordpress - 用户名:
wpuser - 密码:你设置的密码
- 数据库主机:
localhost - 表前缀:
wp_(可自定义)
完成安装即可。
三、方法二:使用 LAMP 搭建 WordPress
步骤 1:安装 Apache
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
步骤 2:安装 MariaDB(同上)
sudo yum install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
创建数据库(同 LNMP 步骤)。
步骤 3:安装 PHP
sudo yum install php php-mysql php-gd php-xml php-mbstring php-json -y
重启 Apache:
sudo systemctl restart httpd
步骤 4:部署 WordPress
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo mv wordpress/* ./
sudo rmdir wordpress
sudo chown -R apache:apache /var/www/html
sudo chmod -R 755 /var/www/html
步骤 5:配置防火墙(开放 HTTP)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
步骤 6:完成安装
访问 http://你的IP地址,进入 WordPress 安装向导,填入数据库信息即可。
四、常见问题解决
-
权限问题
确保 Web 服务器用户(Apache 或 Nginx)对 WordPress 目录有读写权限。 -
SELinux 限制
若遇到无法写入等问题,临时关闭 SELinux 测试:sudo setenforce 0永久关闭:编辑
/etc/selinux/config,设SELINUX=disabled。 -
PHP 不解析
- LNMP:检查 Nginx 是否正确X_X到 PHP-FPM。
- LAMP:确认
.htaccess支持和mod_rewrite已启用。
-
防火墙阻止访问
sudo firewall-cmd --permanent --add-port=80/tcp sudo firewall-cmd --reload
五、总结
| 方式 | 优点 | 适用场景 |
|---|---|---|
| LNMP | 高性能、低资源占用 | 高并发、静态内容多 |
| LAMP | 配置简单、兼容性好 | 初学者、传统应用 |
选择 LNMP 还是 LAMP 取决于你的需求和熟悉程度。两者均可稳定运行 WordPress。
✅ 完成以上步骤后,你的 WordPress 博客就已经成功部署在 CentOS 上了!
云计算导航