Detailed explanation of WordPress multi-site configuration under Nginx environment

Detailed explanation of WordPress multi-site configuration under Nginx environment

The multi-site feature of WordPress allows you to install one WordPress program and create multiple sites (that is, a set of programs that can be bound to multiple domain names or subdomains).

Each site has its own themes, plugins, articles, and pages.

This can greatly reduce the trouble of maintaining and updating multiple WordPress installations.

Moreover, each site can be independent of each other and not affect each other.

There are two ways to use WordPress multisite: subdirectory and subdomain. Here we mainly introduce the subdomain method.

That is to say, based on the main domain name, we will create a subdomain, for example: http://shop.jb51.com.

At the same time, we can map this subdomain to a first-level domain such as http://shop.com.

For visitors, what they access is an independent first-level domain name.

1. Preparation

WordPress introduces its multisite feature page: Site network management page

Next, we prepare several domain names as follows:

Site 1: www.jb51.com (primary domain name), which is the domain name used when installing WordPress Site 2: blog.jb51.com, the second-level domain name Site 3: news.com, the mapped second-level domain name news.jb51.com
Site 4: shop.com, mapped second-level domain name shop.jb51.com
Note: After installing WordPress, please do not modify the domain name in the backend without authorization. Even if you change the domain name with www to without www, or vice versa, it may cause the error "Redirected you too many times."

Then, log in to the resolution page of the domain name service provider and set the A records of the above domain names to the server IP where WordPress is installed.

You can also test it on your local computer by directly modifying the hosts file and adding the following line:

127.0.0.1 www.jb51.com blog.jb51.com news.com shop.com

2. Nginx configuration Create a new configuration file in the Nginx configuration directory as follows:

$ sudo vi /etc/nginx/conf.d/jb51.conf

The content is:

server {
listen 80;
server_name www.jb51.com blog.jb51.com news.com shop.com;

root /usr/share/nginx/wordpress;
index index.php;

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

location ~ /favicon.ico {
access_log off;
log_not_found off;
}

location ~ \.php$ {
try_files $uri /index.php;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

access_log /var/log/nginx/$host-access.log;
error_log /var/log/nginx/wpms-error.log;
}

Here we use the $host variable to allow Nginx to generate a separate access log for each domain name.

For example: news.com-access.log and shop.com-access.log.

However, the error log cannot use the $host variable, so all errors will be recorded in a file.

Restart the Nginx server:

$ nginx -s reload

3. Install WordPress

Install WordPress following the normal WordPress installation steps.

4. Enable multisite functionality

Open the wp-config.php file with a text editor and add the following line before the comment: "/* That's it! Please don't edit any more. Please save this file. Have fun! */":

/* Multisite settings */
define( 'WP_ALLOW_MULTISITE' , true );

We will edit this file several more times.

After saving, log in to the WordPress backend, click: Tools > Network Settings, select the subdomain, network title and network administrator email address.

Then install the terminal.

After a while, two code blocks appear on the interface, prompting you to add wp-config.php and .htaccesss files respectively.

We are using Nginx here, so we don't need to worry about the .htaccess part.

Open the wp-config.php file and add the following lines before the comment: "/* OK! Please do not edit any more. Please save this file. Have fun! */":

define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('DOMAIN_CURRENT_SITE', 'www.jb51.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);
Log out of the WordPress admin panel, and log in again.

Log out of WordPress and log back in.

Open My Sites > Network Management > Sites in the upper left corner of the panel.

Click the Add New button to open the Add New Site form, and add the three subdomains blog, news, and shop in turn.

After adding, select all sites, edit the two subdomains news.jb51.com and shop.jb51.com, and the site titles are news and shopping mall respectively.

Change the site address (URL) to: news.com and shop.com respectively.

After completing this step, we can visit blog.jb51.com. It is already an independent site with independent resources.

But to be able to access news.com and shop.com, you need to continue reading.

5. Set up domain name mapping

Open My Site > Network Management > Plugins in the upper left corner of the panel.

Install the WordPress MU Domain Mapping plugin here, you can search directly or download and install it, and then enable it.

Then copy the sunrise.php file in the plugin directory (directory wp-content/plugins/wordpress-mu-domain-mapping) to the wp-content directory.

Open the wp-config.php file and add the following line before the comment: "/* OK! Please do not edit any more. Please save this file. Have fun! */":

define('SUNRISE', 'on');

Save, then return to the browser and open My Site > Network Management > Settings in the upper left corner of the panel in the background.

Then select Domain Mapping and modify Domain Options as shown below:

Then save it.

The function of the configuration here is to redirect all second-level domain names (such as news.jb51.com) to their respective external domain names (such as news.com), including the management page (/wp-admin).

Next, we need to map the top-level domain names to the various site IDs.

By default, the site ID is not displayed in the background, so we use the simplest method to display the site ID directly in the background.

This method is to use the Must-use plugin of WordPress.

Create a mu-plugins directory in the wp-content directory, and then create a file named wpms_blogid.php in the newly created directory.

The content of this PHP file is:

<?php
add_filter( 'wpmu_blogs_columns', 'do_get_id' );
add_action( 'manage_sites_custom_column', 'do_add_columns', 10, 2 );
add_action( 'manage_blogs_custom_column', 'do_add_columns', 10, 2 );

function do_add_columns( $column_name, $blog_id ) {
if ( 'blog_id' === $column_name )
echo $blog_id;
return $column_name;
}

function do_get_id( $columns ) {
$columns['blog_id'] = 'ID';
return $columns;
}

After saving, visit Site > All Sites in the backend, and there will be an additional ID column in the site list, which will be used in the next step.

Switch to Settings > Domains in the backend control panel and add two domain names:

Site ID: 3 (based on your actual situation)
Domian: news.com
Primary:√

as well as:

Site ID: 4 (based on your actual situation)
Domian: shop.com
Primary:√

If the domain name has www, the same operation is used.

6. Results

After completing the above steps, it is basically OK.

The main site domain name remains unchanged, still www.jb51.com.

Use news.com to access news sites.

You can visit the mall site using shop.com.

The blog can still be accessed using the second-level domain name blog.jb51.com.

At the same time, the backends of these sites also have independent addresses:

http://www.jb51.com/wp-admin/
http://blog.jb51.com/wp-admin/
http://news.com/wp-admin/
http://shop.com/wp-admin/

You can no longer install themes and plugins on every site.

All are configured in the network management (My Site > Network Management in the upper left corner of the panel)

The above is the detailed method of configuring the multi-site function of WordPress under Nginx environment. I hope it will be helpful to everyone.

You may also be interested in:
  • Six steps to teach you how to build a personal blog based on WordPress from scratch
  • Detailed explanation of the whole process of building a personal blog with nginx+WordPress
  • Detailed steps for setting up host Nginx + Docker WordPress Mysql
  • Example of how to set WordPress pseudo-static in Nginx
  • How to configure wordpress with nginx
  • Detailed graphic tutorial on how to solve the slow access problem of WordPress website

<<:  Manually implement the two-way data binding principle of Vue2.0

>>:  MySQL installation tutorial under Linux centos7 environment

Blog    

Recommend

Highly recommended! Setup syntax sugar in Vue 3.2

Table of contents Previous 1. What is setup synta...

Complete steps to build NFS file sharing storage service in CentOS 7

Preface NFS (Network File System) means network f...

Linux kernel device driver character device driver notes

/******************** * Character device driver**...

HTML table markup tutorial (48): CSS modified table

<br />Now let's take a look at how to cl...

Nginx server adds Systemd custom service process analysis

1. Take nginx as an example Nginx installed using...

Use nginx + secondary domain name + https support

Step 1: Add a secondary domain name to the Alibab...

Summary of Vue's cross-domain problem handling and solutions

When you send a network request, the following sa...

Specific operations of MYSQL scheduled clearing of backup data

1|0 Background Due to project requirements, each ...

JavaScript to achieve full or reverse selection effect in form

This article shares the specific code of JavaScri...

Mysql optimization tool (recommended)

Preface While browsing GitHub today, I found this...

Summary of a CSS code that makes the entire site gray

In order to express the deep condolences of peopl...

Detailed explanation of Docker container network port configuration process

Exposing network ports In fact, there are two par...