Home » Categories » Linux Cloud Server » NGINX |
How To Configure Single and Multiple WordPress Site Settings with Nginx |
Article Number: 291 | Rating: Unrated | Last Updated: Sat, Jan 4, 2014 at 5:59 AM
|
WordPress is the most popular CMS (content management system) used on the internet today. WordPress sites can be served using a HTTP server such as Apache or NGINX, while Apache is a great option to serve websites, many sites have moved to NGINX because of it's scalable event-driven architecture, low resources and better delivery of statics files. In this tutorial you will learn how to configure NGINX for various types of WordPress installations, including multisite configurations, rewrite rules and the use of .conf files to apply repeated configurations. RequirementsIn this guide, you will needsudoto install and edit files. I assume that you have gone through the initial server setup. You will need to install MySQL, PHP & NGINX. You can follow these guides to install LEMP on Ubuntu or Debian. Note that our server blocks will be different & that in this tutorial we are making PHP-FPM use a UNIX Socket. Basic NGINX OptimizationAdjust NGINX Worker Processes & ConnectionsIt is often recommended to set the number of NGINX workers equal the number of processors, you can determine the number of processors using:
Open up the main NGINX configuration file:
Increase or decrease the number of workers depending on your system's specs:
NGINX limits the number of connections that a worker can maintain at one time, if your websites have many visitors you might want to increase the limit of connections. In theory the maximum number of connections = workers * limit.
Enabling GzipFiles can be compressed using Gzip to accelerate WordPress, the smaller the data size requested by the user, the faster the response. Think about CSS files & HTML files, they have many similar strings, repeated text and white spaces. Gzip uses an algorithm called DEFLATE that removes duplicate strings by linking to the previous location of that identical string and creates a much smaller file. Find the Gzip section and enable it:
Save & exit. Creating NGINX .conf filesSince you might be hosting more than one WordPress website, we are going to create a few .conf files that can be loaded from the server blocks instead of writing the same configuration many times on each server block. In the next steps we will create 3 files that will hold our configurations:
We are going to create all the files in a directory called "global" but first we will need to create the mentioned directory:
I am going to set /etc/nginx/global as the current directory just to make things easier.
common.conf fileLet's create our first .conf file applicable to any kind of websites.
This will open an empty file, copy the following configurations:
Save and exit. wordpress.conf fileLet's create a .conf file applicable to all WordPress sites:
This will open an empty file, copy the following configurations:
More Rules: You might want to add more NGINX rules, for example, if you use the same WP Plugins that require custom rules on all your installations as I do, you can add more rules in this .conf file, e.g. I use Yoast SEO on all my websites therefore I am adding the rewrite rules required here, in this way I do not have to copy the same rewrite rules for each server block. multisite.conf fileUnlike single site WordPress, which can work with "ugly" permalinks and thus does not need any URL rewrite, a MultiSite installation requires custom rewrite rules to format URLs for your subsites. Let's create a .conf file applicable to multisite WordPress installations:
This will open an empty file, copy the required rewrite rules:
Save & exit. Small NoteOur current working directory is /etc/nginx/global, if you want to change it you can type:
Creating Server BlocksIt is time to create our first server block. Since we already have everything configured in our .conf files there is no need to duplicate the default server block file. Let's disable the default server block:
And create a server block file:
This will open an empty file, copy the following configurations depending on what you want to achieve: Simple WordPress InstallationImagine that you want to configure a WordPress site with this domain www.demo.com. First we will have to create a server block
Remember to change the following data to fit your needs:
You can see that there are two server blocks, that's because www.demo.com & demo.com are different URLs. You probably want to make sure that Google, Bing, users...etc pick the URL that you want, in this case I want my website to be www.demo.com so I have configured a permanent redirect from demo.com to www.demo.com. It is also possible to specify multiple domains:
Multisite with SubdirectoriesIf you want a multisite installation with subdirectories you will need to include the rewrite rules stored in multisite.conf:
Multisite with SubdomainsIf you want a multisite installation with subdomains you will need to configure this server block to listen to a domain with a wildcard:
HTML & Other websitesIf you want to host simple html websites or other webapps you might need to specify custom rules or create more .conf files and include them in the server block:
Remember to save & exit. Enabling Server Block FilesThe last step is to activate the host by creating a symbolic link between the sites-available directory and the sites-enabled directory:
We’ve made a lot of the changes to the configuration. Reload NGINX and make the changes visible.
Final NotesTo create additional virtual hosts, you can just repeat the process above, being careful to set up a new document root with the appropriate new domain name each time. It is also possible to combine multiple server blocks in just one file:
|
Attachments
There are no attachments for this article.
|
How To Install Linux, Nginx, MySQL, PHP (LEMP) Stack on Debian 7
Viewed 3864 times since Fri, Dec 27, 2013
Docker Explained: How To Containerize and Use Nginx as a Proxy
Viewed 11884 times since Sat, Jan 4, 2014
How To Install Wordpress with nginx on CentOS 6
Viewed 9208 times since Sat, Jan 4, 2014
How To Install and Configure Django with Postgres, Nginx, and Gunicorn
Viewed 4252 times since Fri, Jan 3, 2014
How To Install nginx on Ubuntu 12.04 LTS
Viewed 2792 times since Tue, Dec 24, 2013
How to Scale Django: Beyond the Basics
Viewed 2277 times since Fri, Jan 3, 2014
How To Deploy Node.js Applications Using Systemd and Nginx
Viewed 9568 times since Sat, Jan 4, 2014
How To Install (LEMP) nginx, MySQL, PHP stack on Arch Linux
Viewed 13592 times since Sun, Dec 29, 2013
|