插件窝 干货文章 Nginx环境下WordPress的多站点功能怎么配置

Nginx环境下WordPress的多站点功能怎么配置

WordPress 配置 Nginx php 912    来源:    2025-04-27

Nginx环境下WordPress多站点配置指南

要在Nginx环境下配置WordPress多站点功能,需要完成以下几个步骤:

1. 准备工作

  1. 确保你的WordPress已经安装并正常运行
  2. 确认你拥有服务器管理权限
  3. 备份你的网站和数据库

2. 启用WordPress多站点功能

  1. 编辑WordPress根目录下的wp-config.php文件,在/* That's all, stop editing! Happy blogging. */这行之前添加:
/* Multisite */
define('WP_ALLOW_MULTISITE', true);
  1. 保存文件并重新加载WordPress后台

3. 配置多站点网络

  1. 进入WordPress后台:工具 > 网络设置
  2. 选择多站点类型:

    • 子域名(如site1.yourdomain.com)
    • 子目录(如yourdomain.com/site1)

    注意:子域名方式需要配置通配符DNS记录

  3. 填写网络标题和管理员邮箱

  4. 点击"安装"按钮

4. 更新WordPress配置

安装完成后,WordPress会提供需要添加到配置文件的代码:

  1. 将提供的代码添加到wp-config.php中(通常在WP_ALLOW_MULTISITE下方)
  2. 将提供的.htaccess规则替换为Nginx等效配置(见下一步)

5. Nginx服务器配置

编辑你的Nginx配置文件(通常在/etc/nginx/sites-available/yourdomain.com):

对于子目录方式:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /path/to/wordpress;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # 多站点规则
    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;
    }
}

对于子域名方式:

server {
    listen 80;
    server_name ~^(www\.)?(?<sitedomain>.+)\.yourdomain\.com$;
    root /path/to/wordpress;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # 多站点规则
    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;
    }
}

6. 配置通配符子域名(如果使用子域名方式)

  1. 在你的DNS管理面板中添加一个A记录:

    *.yourdomain.com -> 你的服务器IP
    
  2. 在Nginx配置中确保已包含通配符配置(如上所示)

7. 重启Nginx服务

sudo systemctl restart nginx

8. 完成网络设置

  1. 登录WordPress后台,你现在会看到"我的站点"菜单下有"网络管理"选项
  2. 在这里你可以添加新站点、管理用户和主题等

常见问题解决

  1. 404错误:检查Nginx重写规则是否正确
  2. 权限问题:确保Nginx用户对WordPress目录有读写权限
  3. PHP执行问题:确认PHP-FPM配置正确且运行中
  4. 数据库错误:检查wp-config.php中的数据库连接信息

完成以上步骤后,你的WordPress多站点网络应该可以在Nginx环境下正常运行了。