在 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 net-tools vim -y
二、LNMP 搭建 WordPress
1. 安装 Nginx
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
2. 安装 MariaDB(MySQL 替代品)
sudo yum install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
# 运行安全设置向导
sudo mysql_secure_installation
3. 创建 WordPress 数据库和用户
mysql -u root -p
进入 MariaDB 后执行:
CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
4. 安装 PHP 及扩展(推荐 PHP 7.4 或 8.x)
# 安装 EPEL 和 Remi 仓库
sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
# 使用 yum-config-manager 启用 PHP 7.4
sudo yum-config-manager --enable remi-php74
sudo yum install php php-fpm php-mysql php-gd php-xml php-mbstring php-json -y
5. 配置 PHP-FPM
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
编辑配置文件:
sudo vim /etc/php-fpm.d/www.conf
确保以下设置正确:
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
重启服务:
sudo systemctl restart php-fpm
6. 配置 Nginx 虚拟主机
创建配置文件:
sudo vim /etc/nginx/conf.d/wordpress.conf
内容如下(替换 your_domain.com):
server {
listen 80;
server_name your_domain.com www.your_domain.com;
root /var/www/html/wordpress;
index index.php index.html index.htm;
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 reload nginx
7. 下载并安装 WordPress
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -zxvf latest.tar.gz
sudo chown -R nginx:nginx wordpress
sudo chmod -R 755 wordpress
8. 访问网站进行安装
打开浏览器访问:http://your_server_ip_or_domain
按照提示填写数据库信息:
- 数据库名:
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 步骤):
CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
3. 安装 PHP(同上,使用 Remi 仓库)
sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
sudo yum-config-manager --enable remi-php74
sudo yum install php php-mysql php-gd php-xml php-mbstring php-json -y
4. 配置 Apache 权限
sudo chown -R apache:apache /var/www/html/
sudo chmod -R 755 /var/www/html/
5. 下载 WordPress
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -zxvf latest.tar.gz
sudo mv wordpress/* ./
sudo rmdir wordpress
sudo chown -R apache:apache /var/www/html
6. 配置防火墙(开放 HTTP)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
7. 访问安装页面
浏览器访问:http://your_server_ip_or_domain
填写数据库信息完成安装。
四、常见问题处理
1. 文件权限问题
确保 Web 服务器用户(nginx 或 apache)有读写权限:
sudo chown -R nginx:nginx /var/www/html/wordpress # LNMP
sudo chown -R apache:apache /var/www/html # LAMP
2. SELinux 问题(CentOS 默认开启)
临时禁用(不推荐生产环境):
sudo setenforce 0
或设置正确上下文:
sudo setsebool -P httpd_can_network_connect_db 1
sudo setsebool -P httpd_read_user_content 1
3. 开放 80 端口
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload
五、总结
| 架构 | 特点 |
|---|---|
| LNMP | 高性能,适合高并发,使用 Nginx + PHP-FPM |
| LAMP | 传统稳定,Apache 配置简单,适合初学者 |
推荐新手使用 LAMP,进阶用户或高负载场景使用 LNMP。
✅ 完成以上步骤后,你的 WordPress 网站就成功运行在 CentOS 上了!后续可通过 WordPress 后台管理内容、主题和插件。
如有需要,可以继续配置 HTTPS(Let's Encrypt)、缓存插件或 CDN 提速。
CLOUD云计算