A Comprehensive WordPress Multisite Setup Tutorial for Nginx

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn how to setup WordPress multisite on an Nginx web server. This tutorial is intended for new WordPress installs; however, the same concepts can also be used for existing websites.

While the official WordPress documentation and installation process for multisite is tailored to Apache, the Nginx documentation that I could find was quite outdated (i.e. blogs.dir was removed from WordPress back in version 3), so that’s why I decided to make this tutorial.

How to Enable WordPress Multisite

Whether you choose to setup your multisite installation with subdomains or subdirectories, the first thing you need to do is enable WordPress multisite. To do this, edit your wp-config.php file in the root of your web directory and add the following to allow multisite.

define( 'WP_ALLOW_MULTISITE', true );

/* That's all, stop editing! Happy publishing. */

This enables the Network Setup option under the Tools menu in the WordPress dashboard.

WordPress network setupPin

Next, depending on which option you want, you can jump to either subdomain or subdirectory below.

Nginx Subdomain Configuration for WordPress Multisite

You can configure WordPress multisite to create new sites at subdomains. Examples of WordPress multisite subdomain sites are:

  • like.example.com
  • comment.example.com
  • subscribe.example.com

1. Add a Wildcard DNS Record

Wherever you registered your domain name or wherever you’re DNS record settings exist, you will need to add a wildcard DNS A record. This simply is an A record with an asterisk.

Wildcard DNS A recordPin

2. Enable Wildcard Subdomain in Nginx Conf

Next, you must enable the wildcard subdomain in your Nginx configuration file. The important change is highlighted below.

upstream php-handler {
        server unix:/var/run/php/php7.4-fpm.sock;
}

server {
        listen 80;
        server_name example.com *.example.com;
        root /var/www/wordpress;
        index index.php;

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass php-handler;
        }
}

3. Access the Network Setup Wizard

Because you enabled multisite in the previous step, you can now navigate to Tools > Network Setup to kick off the installation.

4. Choose the Subdomain Option

If not already selected, choose the Sub-domains option to install WordPress multisite in that way.

WordPress multisite subdomainsPin

5. Configure Subdomain Install

As instructed by the setup wizard, the next step is to add additional configuration in your wp-config.php file.

define( 'WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('DOMAIN_CURRENT_SITE', 'example.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

/* That's all, stop editing! Happy publishing. */

That’s all! After logging back into the WordPress dashboard, you now have a working network of sites that can be created an accessed by subdomains.

Nginx Subdirectory Configuration for WordPress Multisite

You can configure WordPress multisite to create new sites at subdirectories. Examples of WordPress multisite subdomain sites are:

  • example.com/like
  • example.com/comment
  • .example.com/subscribe

1. Access the Network Setup Wizard

Because you enabled WordPress multisite above, you can now navigate to Tools > Network Setup to kick off the WordPress multisite installation.

2. Choose the Subdirectory Option

If not already selected, choose the Sub-directory option to install WordPress multisite in that way.

WordPress multisite subdirectoriesPin

3. Configure Subdirectory Install

As instructed by the setup wizard, the next step is to add additional configuration in your wp-config.php file.

define( 'WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false);
define('DOMAIN_CURRENT_SITE', 'example.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

/* That's all, stop editing! Happy publishing. */

4. Enable Wildcard Subdomain in Nginx Conf

Next, you must add rewrite rules to support WordPress multisite in your Nginx configuration file. The important change is highlighted below.

upstream php-handler {
        server unix:/var/run/php/php7.4-fpm.sock;
}
server {
        listen 80;
        server_name example.com www.example.com;
        root /var/www/wordpress;
        index index.php;

        # Rewrite requests to /wp-.* on subdirectory installs.
        if (!-e $request_filename) {
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;
                rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
                rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
        }

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass php-handler;
        }
}

That’s all! After logging back into the WordPress dashboard, you now have a working network of sites that can be created an accessed by subdirectories.


Meet Tony

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

1 thought on “A Comprehensive WordPress Multisite Setup Tutorial for Nginx”

  1. Hi, Tony!

    Great tutorial!

    I want to do something maybe very crazy.
    I have my domain I want to use to deploy websites I create by coding everything, and I want to use a subdomain where I can create my multisite wordpress where I can have all of my WordPress sites.

    For example:

    wpsites.domain.com would be where I install my WordPress multisite. I would have a website called:

    site1.wpsites.domain.com.

    Or, since I am already separating the domain from my WordPress sites, I could use a subdirectory structure:

    wpsites.domain.com/sites.

    I am very noob with VPS and the command line and nginx. I have had multistes before with apache, although none of them were this complex, the normal multisite with subdomains.

    Do you think what I want to do is even possible? If so, could you guide me?

    I installed hestia cp on Contabo because it runs on nginx, but I was absolutely unable to follow your tutorial due to the conf being super different, so I installed nginx via command line and set up everything manually.

    Reply

Leave a Comment