×

Archive

Redis cache for speeding up your WordPress site

A few weeks back, we wrote about how reducing MySQL queries and suggested that Redis was one available option for those who wanted to take their caching to the next level. Now, a quick tutorial on installing Redis and configuring WordPress to communicate with Redis as a caching layer.

Redis is an open-source data structure store, and it’s popular among WordPress users for its simplicity and highly optimized approach to caching, which can help pages load significantly faster.

Why caching? In a normal WordPress installation, when a user browses to the index page (index.php), the MySQL database is queried for the posts within the loop. This is usually pretty quick, but if you have multiple loops or many concurrent users, the sheer volume of database queries can become overwhelming for your VPS. A caching layer like Redis will query the database once and create a plain HTML file that it will serve instead of normal index.php file, which will boost page loads and dramatically cut back on database queries.

Prerequisites

  • A VPS running Ubuntu 16.04
  • A working WordPress installation.

Step 1. Installing Redis

Redis is available in the default package repositories for all our OS options, so installation is a breeze.

$ sudo add-apt-repository ppa:chris-lea/redis-server
$ sudo apt-get update
$ sudo apt-get install redis-server php-redis

Now, we’ll quickly verify that Redis was installed correctly by running redis-cli. If you see the prompt change to 127.0.0.1:6379>, you’re off to a good start, and if you run the ping command, you should see PONG in response.

$ redis-cli
127.0.0.1:6379> ping
PONG

Step 2. Configuring Redis

Next, we need to set up a few configurations to make PHP and Redis work together, and for Redis to work as a cache. Open the /etc/redis/redis.conf file in the editor of your choosing, and add the following lines to the end of the file.

maxmemory 256mb
maxmemory-policy allkeys-lru

The RAM allotted in the maxmemory 256mb line can be increased according to your needs.

Finally, we’ll restart the Redis server.

$ sudo service redis-server restart

Step 3. Connecting WordPress to Redis

The easiest way to connect WordPress to Redis is by using a popular plugin called Redis Object Cache. Simply install the plugin via the WordPress dashboard and then navigate to the settings. You should see a screen like the following.

Enabling the cache

The plugin immediately recognizes our Redis installation, and all you have to do is click the Enable Object Cache button to start using Redis as a caching layer for WordPress.

Step 4. Double-check that the cache is working

Before we move on, let’s take a moment to ensure that Redis is actually being used as a caching layer for WordPress. Type in the following command, which will enable a real-time log of Redis activity.

$ redis-cli monitor

If the cache is working properly, you’ll start to see output like the following:

1501199997.504554 [0 127.0.0.1:58530] "PING"
1501199997.505818 [0 127.0.0.1:58530] "GET" "wp_:default:is_blog_installed"
1501199997.509033 [0 127.0.0.1:58530] "GET" "wp_:options:notoptions"
1501199997.509179 [0 127.0.0.1:58530] "GET" "wp_:options:alloptions"

Hit Ctrl + c to end output.

And with that, your WordPress implementation should be cached and ready to serve pages faster than ever before! Proof that not every speed boost needs to come with incredibly complex administration—there’s a good reason why so many people love and use Redis on their VPS.

What is RAID 10? (and how does it help your VPS?)

You’ve probably seen it on the homepage of your favourite hosting company…

Featuring RAID 10 storage!

So it must be pretty important…right?

I mean, they use an exclamation point and everything!

But if you don’t have a ton of experience in server configurations, you’re probably wondering what exactly it is…and why it should matter to you.

Well, we’ll tell you:

What is RAID 10?

The redundant array of independent disks (RAID) is a way to store data across multiple hard drives in order to offer better speed or redundancy, or both. RAID 10 combines both disk mirroring (RAID 1) and disk striping (RAID 0) configurations to keep your data safer.

Why is RAID 10 important?

At SSD Nodes, we use a RAID 10 (or RAID 1+0) configuration on all of our VPS because it keeps our cloud servers humming along. The setup “offers both tremendous speed and maximum file redundancy,” which is a critical aspect of a high-performing SSD-based hosting experience.

But for those who aren’t experienced in the nuances of server hardware configurations, it’s yet another piece of jargon that’s difficult to parse—is it a real benefit, or does it just sound good?

With transparency in mind, we thought we’d take a moment to dig into what RAID 10 means to us, and why it’s so important to your hosting experience.

The Redundant Array of Independent Disks (RAID)

At its core, the redundant array of independent disks (RAID) is a way to store data across multiple hard drives in order to offer better speed or redundancy, or both. While your desktop computer probably doesn’t use a RAID, and your laptop certainly doesn’t, it’s used quite universally inside of web servers and data centres.

RAID 0 splits, or stripes, data evenly across two or more disk for the sake of performance. It could be said it’s similar to two people, each armed with a shovel, sharing the work of digging a hole, rather than one person with a really large shovel. Striping a large number of disks together creates big performance boosts, but if something does wrong with one of the disks, all the data, across every drive, is corrupted.

RAID 1 involves creating a mirror of a disk’s data onto one or more other disks. The same data is mirrored across all the different drives and is limited to the smallest disk in the array. This creates great redundancy but offers little benefits in the way of speed.

RAID 10 is also known as RAID 1+0, in that it combines disk mirroring and disk striping. In a RAID of four disks, there are two pairs in a RAID 1, and then those pairs are placed into a RAID 0. An illustration is often best:

A diagram of the basic RAID 10 configuration. Image courtesy of Wikipedia editor NudelSuppe.

The pros of RAID 10

By combining the performance enhancements of RAID 0 with the hardware-level mirroring protection of RAID 1, RAID 10 offers both speed and resilience.

When you write data to your server, it can be written to multiple disks simultaneously, which improves the overall speed. Same goes for reading that same data, which makes it a great solution for applications that are heavy on I/O. RAID 10 is just one of the many ways we’ve managed to churn out more than 1GB/s of I/O on our platform.

This doesn’t really concern you, as a user of our platform, but RAID 10 is also a wonderful option in case something does go wrong with one of the disks. Restoring data is quick and relatively painless compared to other RAID options.

The cons of RAID 10

One of the major disadvantages of a RAID 10 configuration is that only 50 percent of a given drive is usable because of the mirroring effects.

RAID 10 != built-in backups

Some people incorrectly believe that a RAID setup with mirroring is equivalent to having a backup.

It’s true that the drives are mirrored copies if one drive becomes corrupted, it’s very easy for the other’s data to become unreadable as well. This applies to any RAID, whether it’s inside your local computer or in a data centre, and it applies to your VPS as well.

But RAID 10 doesn’t protect you from hackers, viruses, database corruptions, or user error.

Simply put, there’s no replacement for an effective backup solution that protects the critical data you keep on your VPS.

Read how to simplify your VPS backup strategy.

In reality, the pros and cons of RAID 10 are rather invisible to the end-user. In an ideal situation, you’ll never have to experience the benefits of RAID 10’s mirroring in the event of disk failure. And the performance is built-in, so you’re ready to move fast from the first day. Even then, it’s important to know what makes your server move.

Need some ideas about what you can do with all that performance? Let us know how you’re taking advantage of the RAID 10-powered performance.

How To Install Linux, Apache, MySQL, PHP (LAMP) stack On CentOS 7

Introduction

A “LAMP” stack is a group of open source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the Apache web server. The site data is stored in a MySQL database (using MariaDB), and dynamic content is processed by PHP.

In this guide, we’ll get a LAMP stack installed on an CentOS 7 VPS. CentOS will fulfill our first requirement: a Linux operating system.

Note: The LAMP stack can be installed automatically on your Droplet by adding this script to its User Data when launching it. Check out this tutorial to learn more about Droplet User Data.

Prerequisites

Before you begin with this guide, you should have a separate, non-root user account set up on your server.

Step One — Install Apache

The Apache web server is currently the most popular web server in the world, which makes it a great default choice for hosting a website.

We can install Apache easily using CentOS’s package manager, yum. A package manager allows us to install most software pain-free from a repository maintained by CentOS.

For our purposes, we can get started by typing these commands:

sudo yum install httpd

Since we are using a sudo command, these operations get executed with root privileges. It will ask you for your regular user’s password to verify your intentions.

Afterwards, your web server is installed.

Once it installs, you can start Apache on your VPS:

sudo systemctl start httpd.service

You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):

http://your_server_IP_address/

You will see the default CentOS 7 Apache web page, which is there for informational and testing purposes. It should look something like this:

CentOS 7 Apache default

If you see this page, then your web server is now correctly installed.

The last thing you will want to do is enable Apache to start on boot. Use the following command to do so:

sudo systemctl enable httpd.service

How To Find your Server’s Public IP Address

If you do not know what your server’s public IP address is, there are a number of ways you can find it. Usually, this is the address you use to connect to your server through SSH.

From the command line, you can find this a few ways. First, you can use the iproute2 tools to get your address by typing this:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

This will give you one or two lines back. They are both correct addresses, but your computer may only be able to use one of them, so feel free to try each one.

An alternative method is to use an outside party to tell you how it sees your server. You can do this by asking a specific server what your IP address is:

curl http://icanhazip.com

Regardless of the method you use to get your IP address, you can type it into your web browser’s address bar to get to your server.

Step Two — Install MySQL (MariaDB)

Now that we have our web server up and running, it is time to install MariaDB, a MySQL drop-in replacement. MariaDB is a community-developed fork of the MySQL relational database management system. Basically, it will organize and provide access to databases where our site can store information.

Again, we can use yum to acquire and install our software. This time, we’ll also install some other “helper” packages that will assist us in getting our components to communicate with each other:

sudo yum install mariadb-server mariadb

When the installation is complete, we need to start MariaDB with the following command:

sudo systemctl start mariadb

Now that our MySQL database is running, we want to run a simple security script that will remove some dangerous defaults and lock down access to our database system a little bit. Start the interactive script by running:

sudo mysql_secure_installation

The prompt will ask you for your current root password. Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter. Then the prompt will ask you if you want to set a root password. Go ahead and enter Y, and follow the instructions:

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorization.

New password: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
 ... Success!

For the rest of the questions, you should simply hit the “ENTER” key through each prompt to accept the default values. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.

The last thing you will want to do is enable MariaDB to start on boot. Use the following command to do so:

sudo systemctl enable mariadb.service

At this point, your database system is now set up and we can move on.

Step Three — Install PHP

PHP is the component of our setup that will process code to display dynamic content. It can run scripts, connect to our MySQL databases to get information, and hand the processed content over to our web server to display.

We can once again leverage the yum system to install our components. We’re going to include the php-mysql package as well:

sudo yum install php php-mysql

This should install PHP without any problems. We need to restart the Apache web server in order for it to work with PHP. You can do this by typing this:

sudo systemctl restart httpd.service

Install PHP Modules

To enhance the functionality of PHP, we can optionally install some additional modules.

To see the available options for PHP modules and libraries, you can type this into your system:

yum search php-

The results are all optional components that you can install. It will give you a short description for each:

php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-enchant.x86_64 : Enchant spelling extension for PHP applications
php-fpm.x86_64 : PHP FastCGI Process Manager
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
. . .

To get more information about what each module does, you can either search the internet, or you can look at the long description in the package by typing:

yum info package_name

There will be a lot of output, with one field called Description which will have a longer explanation of the functionality that the module provides.

For example, to find out what the php-fpm module does, we could type this:

yum info php-fpm

Along with a large amount of other information, you’ll find something that looks like this:

. . .
Summary     : PHP FastCGI Process Manager
URL         : http://www.php.net/
License     : PHP and Zend and BSD
Description : PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI
            : implementation with some additional features useful for sites of
            : any size, especially busier sites.

If, after researching, you decide you would like to install a package, you can do so by using the yum install command like we have been doing for our other software.

If we decided that php-fpm is something that we need, we could type:

sudo yum install php-fpm

If you want to install more than one module, you can do that by listing each one, separated by a space, following the yum install command, like this:

sudo yum install package1 package2 ...

At this point, your LAMP stack is installed and configured. We should still test out our PHP though.

Step Four — Test PHP Processing on your Web Server

In order to test that our system is configured properly for PHP, we can create a very basic PHP script.

We will call this script info.php. In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the “web root”.

In CentOS 7, this directory is located at /var/www/html/. We can create the file at that location by typing:

sudo vi /var/www/html/info.php

This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:

<?php phpinfo(); ?>

When you are finished, save and close the file.

If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Now we can test whether our web server can correctly display content generated by a PHP script. To try this out, we just have to visit this page in our web browser. You’ll need your server’s public IP address again.

The address you want to visit will be:

http://your_server_IP_address/info.php

The page that you come to should look something like this:

CentOS 7 default PHP info

This page basically gives you information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are being applied correctly.

If this was successful, then your PHP is working as expected.

You probably want to remove this file after this test because it could actually give information about your server to unauthorized users. To do this, you can type this:

sudo rm /var/www/html/info.php

You can always recreate this page if you need to access the information again later.

Conclusion

Now that you have a LAMP stack installed, you have many choices for what to do next. Basically, you’ve installed a platform that will allow you to install most kinds of websites and web software on your server.

How To Install WordPress on CentOS 7

Introduction

WordPress is a free and open source website and blogging tool that uses PHP and MySQL. WordPress is currently the most popular CMS (Content Management System) on the Internet and has over 20,000 plugins to extend its functionality. This makes WordPress a great choice for getting a website up and running quickly and easily.

In this guide, we will demonstrate how to get a WordPress instance set up with an Apache web server on CentOS 7.

Prerequisites

Before you begin with this guide, there are a few steps that need to be completed first.

You will need a CentOS 7 server installed and configured with a non-root user that has sudoprivileges.

Additionally, you’ll need to have a LAMP (Linux, Apache, MySQL, and PHP) stack installed on your CentOS 7 server. If you don’t have these components already installed or configured, you can use this guide to learn how to install LAMP on CentOS 7.

When you are finished with these steps, you can continue with the installation of WordPress.

Step One — Create a MySQL Database and User for WordPress

The first step that we will take is in preparation. WordPress uses a relational database to manage information for the site and its users. We have MariaDB (a fork of MySQL) installed already, which can provide this functionality, but we need to make a database and a user for WordPress to work with.

To get started, log into MySQL’s root (administrative) account by issuing this command:

mysql -u root -p

You will be prompted for the password that you set for the root account when you installed MySQL. Once that password is submitted, you will be given a MySQL command prompt.

First, we’ll create a new database that WordPress can control. You can call this whatever you would like, but I will be calling it wordpress for this example.

CREATE DATABASE wordpress;

Note: Every MySQL statement or command must end in a semi-colon (;), so check to make sure that this is present if you are running into any issues.

Next, we are going to create a new MySQL user account that we will use exclusively to operate on WordPress’s new database. Creating one-function databases and accounts is a good idea, as it allows for better control of permissions and other security needs.

I am going to call the new account wordpressuser and will assign it a password of password. You should definitely use a different username and password, as these examples are not very secure.

CREATE USER wordpressuser@localhost IDENTIFIED BY 'password';

At this point, you have a database and user account that are each specifically made for WordPress. However, the user has no access to the database. We need to link the two components together by granting our user access to the database.

GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'password';

Now that the user has access to the database, we need to flush the privileges so that MySQL knows about the recent privilege changes that we’ve made:

FLUSH PRIVILEGES;

Once these commands have all been executed, we can exit out of the MySQL command prompt by typing:

exit

You should now be back to your regular SSH command prompt.

Step Two — Install WordPress

Before we download WordPress, there is one PHP module that we need to install to ensure that it works properly. Without this module, WordPress will not be able to resize images to create thumbnails. We can get that package directly from CentOS’s default repositories using yum:

sudo yum install php-gd

Now we need to restart Apache so that it recognizes the new module:

sudo service httpd restart

We are now ready to download and install WordPress from the project’s website. Luckily, the WordPress team always links the most recent stable version of their software to the same URL, so we can get the most up-to-date version of WordPress by typing this:

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

This will download a compressed archive file that contains all of the WordPress files that we need. We can extract the archived files to rebuild the WordPress directory with tar:

tar xzvf latest.tar.gz

You will now have a directory called wordpress in your home directory. We can finish the installation by transferring the unpacked files to Apache’s document root, where it can be served to visitors of our website. We can transfer our WordPress files there with rsync, which will preserve the files’ default permissions:

sudo rsync -avP ~/wordpress/ /var/www/html/

rysnc will safely copy all of the contents from the directory you unpacked to the document root at /var/www/html/. However, we still need to add a folder for WordPress to store uploaded files. We can do that with the mkdir command:

mkdir /var/www/html/wp-content/uploads

Now we need to assign the correct ownership and permissions to our WordPress files and folders. This will increase security while still allowing WordPress to function as intended. To do this, we’ll use chown to grant ownership to Apache’s user and group:

sudo chown -R apache:apache /var/www/html/*

With this change, the web server will be able to create and modify WordPress files, and will also allow us to upload content to the server.

Step Three — Configure WordPress

Most of the configuration required to use WordPress will be completed through a web interface later on. However, we need to do some work from the command line to ensure that WordPress can connect to the MySQL database that we created for it.

Begin by moving into the Apache root directory where you installed WordPress:

cd /var/www/html

The main configuration file that WordPress relies on is called wp-config.php. A sample configuration file that mostly matches the settings we need is included by default. All we have to do is copy it to the default configuration file location, so that WordPress can recognize and use the file:

cp wp-config-sample.php wp-config.php

Now that we have a configuration file to work with, let’s open it in a text editor:

nano wp-config.php

The only modifications we need to make to this file are to the parameters that hold our database information. We will need to find the section titled MySQL settings and change the DB_NAMEDB_USER, and DB_PASSWORD variables in order for WordPress to correctly connect and authenticate to the database that we created.

Fill in the values of these parameters with the information for the database that you created. It should look like this:

// ** 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', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'password');

These are the only values that you need to change, so save and close the file when you are finished.

Step Four — Complete Installation Through the Web Interface

Now that you have your files in place and your software is configured, you can complete the WordPress installation through the web interface. In your web browser, navigate to your server’s domain name or public IP address:

http://server_domain_name_or_IP

First, you will need to select the language that you would like to install WordPress with. After selecting a language and clicking on Continue, you will be presented with the WordPress initial configuration page, where you will create an initial administrator account:

WordPress Web Install

Fill out the information for the site and administrative account that you wish to make. When you are finished, click on the Install WordPress button at the bottom to continue.

WordPress will confirm the installation, and then ask you to log in with the account that you just created:

WordPress Success

To continue, hit the Log in button at the bottom, then fill out your administrator account information:

WordPress Login

After hitting Log in, you will be presented with your new WordPress dashboard:

WordPress Dashboard