在 CentOS 8.2 上搭建 WordPress 需要配置 PHP 环境,以下是详细步骤:
1. 更新系统并安装必要工具
sudo dnf update -y
sudo dnf install -y epel-release
2. 启用 PHP 模块仓库
CentOS 8 默认使用 AppStream 仓库管理 PHP:
sudo dnf module list php
sudo dnf module enable php:7.4 # WordPress推荐PHP 7.4或更高版本
3. 安装 PHP 及相关扩展
sudo dnf install -y php php-cli php-fpm php-mysqlnd
php-zip php-devel php-gd php-mbstring php-curl
php-xml php-pear php-bcmath php-json php-opcache
4. 验证 PHP 安装
php -v
php -m | grep -E "mysql|gd|mbstring|xml|curl"
5. 配置 PHP-FPM(如果使用Nginx)
编辑 PHP-FPM 配置文件:
sudo vi /etc/php-fpm.d/www.conf
修改以下参数:
user = nginx
group = nginx
listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
启动并启用 PHP-FPM:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
sudo systemctl status php-fpm
6. 配置 PHP.ini(可选优化)
sudo vi /etc/php.ini
建议调整的参数:
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
date.timezone = Asia/Shanghai # 根据时区修改
7. 安装并配置 Web 服务器
如果使用 Apache:
sudo dnf install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
如果使用 Nginx:
sudo dnf install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
配置 Nginx 虚拟主机(示例):
server {
listen 80;
server_name 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 unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
8. 安装并配置数据库(MariaDB)
sudo dnf install -y mariadb-server mariadb
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
创建 WordPress 数据库:
CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
9. 下载并配置 WordPress
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo chown -R nginx:nginx wordpress/ # Nginx用户
# 或 sudo chown -R apache:apache wordpress/ # Apache用户
10. 配置防火墙
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
11. 重启服务
# 如果使用Nginx+PHP-FPM
sudo systemctl restart nginx php-fpm
# 如果使用Apache
sudo systemctl restart httpd
12. 完成安装
通过浏览器访问服务器IP或域名,按照 WordPress 安装向导完成配置。
常见问题解决:
-
PHP扩展缺失错误:
sudo dnf install php-{gd,mbstring,xml,curl,zip} -
权限问题:
sudo chown -R web_user:web_user /var/www/html/wordpress sudo find /var/www/html/wordpress -type d -exec chmod 755 {} ; sudo find /var/www/html/wordpress -type f -exec chmod 644 {} ; -
检查PHP配置:
sudo php-fpm -t # 测试PHP-FPM配置 sudo nginx -t # 测试Nginx配置
这样就完成了 CentOS 8.2 上 WordPress 的 PHP 环境配置。
CLOUD技术笔记