15 Sep

How to setup Nginx for wordpress multisite with subdirectories..

Running WordPress on Nginx is always a good choice, but also have some complications in configurations. Here is the clear and precise way of how to setup Nginx for wordpress multisite with subdirectories.You can also configure Nginx with subdomain multisite and it all depends on your requirement .

You can setup wordpress multisite on Nginx like:

  1. site1.example.com, site2.example.com
  2. www.example.com/sub1, www.example.com/sub2

Here is the steps to setup  Nginx for wordpress multisite with subdirectories:

Step1:

Install wordpress fresh on your system, and enable multisite option by adding a line to your wp-config.php file. Add this line just above where it says “/* That’s all, stop editing! Happy blogging. */”:
Add this line :
/* Multisite */
define( ‘WP_ALLOW_MULTISITE’, true );

Now you can see a new option will appear in the Admin panel of WordPress under Tools -> Network Setup
vhscloud-wp-nginx

Step2:

Now you need to choose the subdirectory install option and click on Install. Once it is done, now you’ll have to add some more lines in your wp-config.php file. These lines may varies based on your installation and you can get these lines after the install has been completed.
vhscloud-wp-nginx1

Now copy these new lines in wp-config.php .Now you will see a separate “Site” called Network Admin in the upper left of your Admin section.
vhscloud-wp-nginx2

Step3:

Now you can create a new site that you want to install and also provide subdirectory installation path for the new website.Now we have done setting up new website in subdirectory and we still need to link it through URl.
Now you have to go Under the Network Admin section, install a new Plugin called Nginx Helper. Once it get installed and activated, visit the plugin settings page and check the box next to “Enable Nginx Map.” This will display a new section showing a file system path to your map file and the actual site mappings that are being generated.
vhscloud-wp-nginx3

Each new site in your network will have both a directory path as well as a blog id. The mapping links the subdirectory in the URL with the proper site in the database.

Step4:

Now we are almost there in setting up are multisite on Nginx.Now you need to access terminal access to the Nginx vhost file through SSH.
SSH to the Nginx site configuration file, on Debian/Ubuntu it’s at /etc/nginx/sites-available.
Now you need to modify the .vhost file

At the start of the file, outside the server{} block add this :

map $http_host $blogid {
default 0;
include /var/www/pathtoyoursite/wp-content/uploads/nginx-helper/map.conf;
}

By this way it will let the plugin update the mappings file automatically as you add new sites and you won’t have to change your Nginx config again.

Now at atlast in your .vhost file add the following Nginx directives at the bottom of the server{} block, replacing any existing WordPress directives you’ve already defined, usually following the location @php{} directive:

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

location ~ ^/files/(.*)$ {
try_files /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1 ;
access_log off; log_not_found off; expires max;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}

location ^~ /blogs.dir {
internal;
alias /var/www/pathtoyoursite/web/wp-content/blogs.dir ;
access_log off; log_not_found off; expires max;
}

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;
}

Now the alias line in the second to last block, you’ll need to update that with the path to your wordpress install.

Finally:

Restart Nginx and enjoy your multisite.

Share this

Comments (1)

Anonymous
September 15, 2015 Reply

Leave a reply