如何通过Nginx或Apache在CentOS 8.2上运行多个WordPress?

在 CentOS 8.2 上通过 Nginx 或 Apache 运行多个 WordPress 实例,核心思路是:每个 WordPress 站点使用独立的目录、数据库、域名(或子域名/路径),并配置对应的 Web 服务器虚拟主机(VirtualHost / Server Block)。下面分别给出 Nginx 和 Apache 的完整步骤示例(以两个站点为例:site1.example.comsite2.example.com)。


一、前置准备(通用)

1. 安装基础依赖

sudo dnf install -y epel-release
sudo dnf install -y nginx php-fpm php-mysqlnd php-gd php-mbstring php-xml php-curl php-json php-fileinfo php-zip 
    mariadb-server mariadb python3 python3-pip wget curl git

若用 Apache,将 nginx 替换为 httpd,并安装 mod_phpphp-fpm + mod_proxy_fcgi(推荐后者):

sudo dnf install -y httpd mod_ssl php-fpm php-mysqlnd php-gd php-mbstring php-xml php-curl php-json php-fileinfo php-zip

2. 启动服务并设置开机自启

Nginx:

sudo systemctl enable --now nginx
sudo systemctl enable --now php-fpm

Apache:

sudo systemctl enable --now httpd
sudo systemctl enable --now php-fpm   # 如果使用 PHP-FPM

3. 创建 MySQL/MariaDB 用户与数据库(每个站点独立)

sudo mysql -u root -p <<EOF
CREATE DATABASE site1_db;
CREATE USER 'site1_user'@'localhost' IDENTIFIED BY 'StrongPassword1';
GRANT ALL PRIVILEGES ON site1_db.* TO 'site1_user'@'localhost';

CREATE DATABASE site2_db;
CREATE USER 'site2_user'@'localhost' IDENTIFIED BY 'StrongPassword2';
GRANT ALL PRIVILEGES ON site2_db.* TO 'site2_user'@'localhost';

FLUSH PRIVILEGES;
EXIT;
EOF

二、方式一:使用 Nginx 运行多 WordPress(推荐)

1. 安装 WordPress(每个站点独立目录)

# 创建站点根目录
sudo mkdir -p /var/www/site1.example.com/html
sudo mkdir -p /var/www/site2.example.com/html

# 下载最新 WordPress(示例用 site1)
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo cp -r wordpress/* /var/www/site1.example.com/html/

# 同样处理 site2(可复用 tarball)
sudo cp -r wordpress/* /var/www/site2.example.com/html/

# 设置权限(关键!)
sudo chown -R apache:apache /var/www/site1.example.com/html
sudo chown -R apache:apache /var/www/site2.example.com/html
sudo chmod -R 755 /var/www/site1.example.com/html
sudo chmod -R 755 /var/www/site2.example.com/html

⚠️ 注意:CentOS 8 默认用户是 apache(不是 www-data),请根据实际调整。

2. 配置 Nginx 虚拟主机

站点 1 配置:/etc/nginx/conf.d/site1.conf

server {
    listen 80;
    server_name site1.example.com www.site1.example.com;
    root /var/www/site1.example.com/html;
    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;  # 或 127.0.0.1:9000
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_read_timeout 300s;
    }

    location ~ /.ht {
        deny all;
    }

    # 可选:禁止访问 wp-config.php 等敏感文件
    location ~* wp-config.php {
        deny all;
    }
}

站点 2 配置:/etc/nginx/conf.d/site2.conf(类似,改域名和 root)

server {
    listen 80;
    server_name site2.example.com www.site2.example.com;
    root /var/www/site2.example.com/html;
    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_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.ht {
        deny all;
    }
}

3. 修改 wp-config.php 中的数据库连接

对每个站点执行:

sudo nano /var/www/site1.example.com/html/wp-config.php

修改以下部分:

define( 'DB_NAME', 'site1_db' );
define( 'DB_USER', 'site1_user' );
define( 'DB_PASSWORD', 'StrongPassword1' );
define( 'DB_HOST', 'localhost' );

同理修改 site2wp-config.phpsite2_db 等。

4. 测试并重载 Nginx

sudo nginx -t
sudo systemctl reload nginx

5. 本地 hosts 测试(临时)

编辑 /etc/hosts

127.0.0.1 site1.example.com
127.0.0.1 site2.example.com

然后浏览器访问 http://site1.example.comhttp://site2.example.com,按提示完成 WordPress 安装。


三、方式二:使用 Apache 运行多 WordPress

1. 启用必要模块

sudo a2enmod rewrite proxy_fcgi setenvif ssl  # 若未启用
sudo systemctl restart httpd

2. 创建站点配置(/etc/httpd/conf.d/site1.conf

<VirtualHost *:80>
    ServerName site1.example.com
    ServerAlias www.site1.example.com
    DocumentRoot /var/www/site1.example.com/html

    <Directory /var/www/site1.example.com/html>
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch .php$>
        SetHandler "proxy:fcgi://unix:/run/php-fpm/www.sock"
    </FilesMatch>

    ErrorLog logs/site1-error.log
    CustomLog logs/site1-access.log combined
</VirtualHost>

3. 创建 site2.conf(同上,改域名和 DocumentRoot)

4. 同样修改各站点的 wp-config.php

5. 重启 Apache

sudo apachectl configtest
sudo systemctl restart httpd

四、可选优化与安全建议

项目 建议
HTTPS 使用 Let’s Encrypt(certbot)为每个域名申请证书;Nginx/Apache 中配置 listen 443 sslssl_certificate
隔离性 确保每个站点有独立 DB、独立目录、独立 WP 插件/主题(避免冲突)
备份 定期备份数据库 + 网站目录(如用 mysqldump + rsync
性能 开启 OPcache、Redis 对象缓存(需单独安装 Redis + phpredis)
安全 限制 PHP 上传大小、禁用危险函数、定期更新 WP 核心/插件

五、常见问题排查

  • 502 Bad Gateway → 检查 PHP-FPM 是否运行:systemctl status php-fpm
  • 403 Forbidden → 检查目录权限和 SELinux:getenforce;若为 Enforcing,可用 setsebool -P httpd_can_network_connect 1 或给目录加 context:
    sudo chcon -R -t httpd_sys_content_t /var/www/site1.example.com/html
  • 无法写入 wp-content/uploads → 确认所有者是 apache:apache,且无只读属性

需要我提供:

  • 一键脚本(含自动创建站点)?
  • Docker 方案对比?
  • 子目录模式(如 example.com/blog1, example.com/blog2)的配置?

欢迎告诉我你的具体需求 😊