在 CentOS 7.9 上安装 WordPress 需要搭建 LAMP(Linux + Apache + MySQL/MariaDB + PHP)环境。以下是详细的步骤:
✅ 一、准备工作
1. 更新系统
sudo yum update -y
2. 安装常用工具(可选)
sudo yum install wget vim net-tools epel-release -y
✅ 二、安装 Apache(httpd)
sudo yum install httpd -y
启动并设置开机自启:
sudo systemctl start httpd
sudo systemctl enable httpd
检查状态:
sudo systemctl status httpd
开放防火墙端口(80 HTTP):
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
测试:浏览器访问服务器 IP,应看到 Apache 默认页面。
✅ 三、安装 MariaDB(MySQL 替代品)
CentOS 7.9 默认使用 MariaDB。
sudo yum install mariadb-server mariadb -y
启动并设置开机自启:
sudo systemctl start mariadb
sudo systemctl enable mariadb
运行安全初始化脚本:
sudo mysql_secure_installATION
按提示设置 root 密码、删除匿名用户、禁止远程 root 登录等。
✅ 四、创建 WordPress 数据库和用户
登录 MariaDB:
mysql -u root -p
执行以下 SQL 命令(请修改密码):
CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
✅ 五、安装 PHP(WordPress 要求 7.4+ 推荐)
CentOS 7 默认源中的 PHP 版本较低,需添加 Remi 源。
1. 安装 Remi 和 yum-utils
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
sudo yum install yum-utils -y
2. 启用 PHP 7.4(推荐)或更高版本的 Remi 模块
sudo yum-config-manager --enable remi-php74
可替换为
remi-php80或remi-php81如果需要更高版本。
3. 安装 PHP 及扩展
sudo yum install php php-mysql php-gd php-xml php-mbstring php-json php-fpm php-curl -y
4. 检查 PHP 版本
php -v
5. 重启 Apache 使 PHP 生效
sudo systemctl restart httpd
✅ 六、下载并安装 WordPress
1. 进入 Web 目录
cd /var/www/html
2. 下载 WordPress 最新中文版(或英文版)
sudo wget https://cn.wordpress.org/latest-zh_CN.tar.gz
解压:
sudo tar -zxvf latest-zh_CN.tar.gz
移动文件到根目录:
sudo mv wordpress/* .
sudo rmdir wordpress
sudo rm latest-zh_CN.tar.gz
3. 设置权限
sudo chown -R apache:apache /var/www/html
sudo chmod -R 755 /var/www/html
注意:生产环境建议更精细的权限控制。
✅ 七、配置 WordPress
1. 复制配置文件
sudo cp wp-config-sample.php wp-config.php
2. 编辑配置文件
sudo vim wp-config.php
修改数据库相关配置:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'your_strong_password');
define('DB_HOST', 'localhost');
保存退出。
✅ 八、完成安装
在浏览器中访问你的服务器 IP 或域名:
http://your_server_ip
你应该进入 WordPress 安装向导。
填写站点信息:
- 站点标题
- 用户名(管理员账号)
- 密码(强烈建议使用强密码)
- 邮箱
点击“安装 WordPress”。
✅ 九、(可选)配置 SELinux 和防火墙增强
如果遇到权限问题(如无法上传媒体),可能需要调整 SELinux:
# 允许 Apache 写入
sudo setsebool -P httpd_can_network_connect_db 1
sudo setsebool -P httpd_unified 1
# 或者临时禁用 SELinux 测试(不推荐生产环境)
# sudo setenforce 0
✅ 十、常见问题排查
| 问题 | 解决方法 |
|---|---|
| 白屏 | 检查 PHP 错误日志 /var/log/httpd/error_log |
| 无法连接数据库 | 检查 wp-config.php 中的用户名、密码、数据库名 |
| 无法上传图片 | 检查目录权限、SELinux 设置 |
| 安装后跳转错误 | 检查 .htaccess 权限或启用 Apache rewrite 模块 |
✅ 总结
你已成功在 CentOS 7.9 上部署 WordPress!
🔧 核心组件:
- Web Server: Apache (httpd)
- Database: MariaDB
- Language: PHP 7.4+
- CMS: WordPress
📌 建议后续操作:
- 配置域名和 SSL(Let’s Encrypt)
- 定期备份数据库和文件
- 使用安全插件加强 WordPress 安全
如果你希望使用 Nginx + PHP-FPM(LNMP 架构),也可以告诉我,我可以提供相应教程。
需要我帮你写一个自动化安装脚本吗?
CLOUD云知道