How to Install WordPress with Nginx & Redis on a CentOS VPS or Dedicated Server

WordPress is a MySQL and PHP-based, open-source Content Management System (CMS) often utilized by many blogging platforms. It’s a powerful application that must be installed on a web server either as a network host or an internet hosting services in its own right to function. One of the best web servers used for WordPress installation is the Nginx server. Nginx is a free and open-source system that provides high performance, ease of use, and outstanding scalability for WordPress platforms.

On the other hand, Redis is an open-source, in-memory data structure store that functions as a cache, database, and message broker. The system supports multiple data structures including strings, lists, sets, hashes, bitmaps, sorted sets with numerous queries, geospatial indexes with multiple radius queries, and hyperlog logs.

This guide is compiled for webmasters looking to install WordPress with Nginx and Redis to solve performance problems. It provides easy-to-follow, step-by-step instructions to help you learn how to install WordPress to run on a Nginx server and utilize Redis cache on CentOS.

Let’s get started!

Special Note: if you are a WordPress hosting beginner we recommend that you start with one of the basic WordPress plans offered by leading Hosting Services. You can start by consulting Hostadvice's Best WordPress Hosting page to browse the best WP hosting companies, including prices, features, expert and user reviews.

PART ONE

Pre-installation requirements

Before we proceed, ensure the following conditions are met.

  • The EMP (Nginx, MariaDB, and PHP) stack must be installed on CentOS
  • PhpMyAdmin must be installed with Nginx on CentOS
  • The SELinux must be disabled on CentOS

Once the above essentials are in place, we can proceed with our installation:

STEP 1: Configure Nginx Virtualhost

The first thing when installing WordPress with Nginx is creating a Virtual host for the installation. Navigate to /etc/nginx/conf.d directory to access the configuration files for the Virtual host. The typical virtual host file encloses the following details: Domain name, document root, port number, fast CGI, log location, etc.

To create the virtual host we’ll assume the following,

Domain name: wordpress.HostingAdvice.local

Document root: /usr/share/nginx/ wordpress.HostingAdvice.local

Port number: 80

Logs: /usr/share/nginx/ wordpress.HostingAdvice.local/logs

Run the command below to create the virtual host:

sudo vi /etc/nginx/conf.d/default.conf

Add the content below:

server {
 listen 80;
 server_name YOURIPADDRESS;
 
 # note that these lines are originally from the "location /" block
 root /usr/share/nginx/html/wordpress;
 index index.php index.html index.htm;
 
 location / {
 try_files $uri $uri/ =404;
 }
 error_page 404 /404.html;
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html;
 }
 
 location ~ \.php$ {
 try_files $uri =404;
 fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
}

Use the command below to create logs directory and document root:

mkdir/usr/share/nginx/wordpress.hostingadvice.local
mkdir/usr/share/nginx/wordpress.hostingadvice.local/logs

Run: nginx –t to authenticate the configuration files. If your virtual host content is spot-on you will get the following:

nginx: configuration file/etc/nginx/nginx.conf syntax is ok
nginx: configuration file/etc/nginx/nginx.conf test is successful

Once you get your virtual host entries right, you can restart the service using the command below:

systemctl restart nginx
systemctl restart php-fpm

At this point, if the SELinux was not disabled properly you will get an error while restarting your Nginx service. Use the command below to disable the SELinux:

setenforce 0

STEP 2: Create a database

Once the virtual host for WordPress installation is fully configured, we now create the database for our installation. In this case, we’ll create the desired database using MariaDB.

Login to MariaDB

mysql-u root -p

To create the database, start with

CREATE DATABASE wordpress;

Next, create a user,  

CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'wppassword';

Grant the user the permission to access the create database:

GRANT ALL PRIVILEGES ON wordpress. *TO 'wpuser'@'localhost';

Exit from the database (MariaDB) shell

STEP 3: Configure WordPress

By now, the virtual host and the database for WordPress installation are fully configured. We can now proceed with WordPress installation:

Download the latest version of WordPress:

wget http://wordpress.org/latest.tar.gz

Extract it

tar -zxvf latest.tar.gz

Next, move the WordPress files to the document root

mv wordpress/* /usr/share/nginx/wordpress.hostingadvice.local

Locate the file; wp-sample-config.php, copy and mark it as a wp-config.php file

cp/usr/share/nginx/wordpress.hostingadvice/wp-config-smaple.php /usr/share/nginx/wordpress.hostungavice.local/wp-config.php

Modify the config file and include your database information:

Vi/usr/share/nginx/wordpress.hostingadvice.local/wp-config.php

You default configuration will be as below:

//**mysql-settings - you can get this infor from your web host **//
/**the name of the database for wordpress */
define('DB_NAME', 'database_name_here');
/**MySQL database username */
define('DB_USER', 'username_here');
/**MySQL database password*/
define('DB_PASSWORD', "password_here');
/**MySQL hostname*/
define('DB_HOST, ' 'localhost');

Now we’ll edit the entries to include details of the created database and database user. The modified entries will be:

//**MySQL SETTINGS - You can get this info from your web host **//
/**The name of the database for WordPress*/
define(DB_NAME', 'wordpress');
/**MySQL database username */
define('DB_USER', 'wpuser');
/**MySQL database password */
define('DB_PASSWORD', 'wppassword');
/88mYsql HOSTNAME */
define('DB_HOST', 'localhost);

Next, we make the Nginx User the owner of the WordPress directory using the following command:

chown -R nginx: nginx /usr/share/nginx/wordpress.hostingadvice.local/

STEP 5: Install WordPress

By now the stage is set for the installation of WordPress. Open your WordPress:

http://wordpress.hostingAdvice.local

Enter your website information and hit the install WordPress button.

The window below will appear

Click login, enter your password

You will access the WP admin section:

That’s all! You have successfully installed WordPress with Nginx.

PART TWO

In the second part of this guide we’ll help you configure WordPress to use Redis.

STEP 1: Redis installation

The first step when configuring WP to utilize Redis is installing the Redis server and the necessary PHP extension to allow WordPress to communicate with the service (Redis). Run the following command:

apt-get install php5-redis-server

STEP 2: set up the cache

Next we need to set up Redis as the cache for WordPress. To accomplish this, we first make a number of modifications in our configuration. First, locate the file below:

/etc/redis/redis.conf

Edit the file to

vi .etc/redis/redis.conf

Add the content below, immediately after the last command line of the file:

maxmemory 128mb
maxmemory-policy allkeys-lru

Here we have set the maxmemory as 128MB. Make sure you set this line according to the maximum RAM of your server. If you are concerned about the amount of RAM, you can create a swap file.

STEP 3: Modify the WP configuration file

Next, locate the wp-config.php configuration file and edit it to include the settings that will permit caching:

vi wp-config.php

Find the section:

authentication unique keys and salts

At the end of this section add the command lines:

define('WP_CACHE_KEY_SALT', 'string');
define('WP_CACHE', true);

Note: The value for ‘string’ is not fixed. You can substitute it with anything provided it’s unique.

STEP 4: Using Redis Object Cache

Once you edit the WP configuration, next is to use a script called Redis Object Cache to instruct WP installation to utilize Redis. The original Redis Object Cache script was written by Eric Mann and can be access on GitHub. Upload the Redis Object Cache to:

/YourWPInstall/wp-content

Note: The Redis Object Cache should be placed in the wp-content and not the wp-content/plugins folder.

STEP 5: Restart the services

By now our installation is complete and you need to restart your Redis Server followed by Apache using the commands below:

service redis-server restart
service apache2 restart

Everything is set!

Navigate around your WP admin area and keep an eye on the Redis monitor, to confirm if the WordPress/Redis integration is okay. Run the following command:

redis-cli monitor

Log entries will appear if Redis is communicating with the WordPress; it’s a positive sign that Redis is ready for use. You can now enjoy the performance-boosting capabilities of Redis as the cache for your WP.

Conclusion

By now you have successfully set up your WordPress platform to leverage the power of Nginx web server. You have also enhanced the performance by adding the Redis Cache which is good for speed and scalability of the WP platform.

 

Check out the top 3 WordPress hosting services:

Was this article helpful?