要在Nginx环境下配置WordPress多站点功能,需要完成以下几个步骤:
wp-config.php
文件,在/* That's all, stop editing! Happy blogging. */
这行之前添加:/* Multisite */
define('WP_ALLOW_MULTISITE', true);
选择多站点类型:
注意:子域名方式需要配置通配符DNS记录
填写网络标题和管理员邮箱
安装完成后,WordPress会提供需要添加到配置文件的代码:
wp-config.php
中(通常在WP_ALLOW_MULTISITE
下方).htaccess
规则替换为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;
}
}
在你的DNS管理面板中添加一个A记录:
*.yourdomain.com -> 你的服务器IP
在Nginx配置中确保已包含通配符配置(如上所示)
sudo systemctl restart nginx
wp-config.php
中的数据库连接信息完成以上步骤后,你的WordPress多站点网络应该可以在Nginx环境下正常运行了。