Optimizing Your Headway Powered Website – Caching Options for LAMP (Apache) based VPS'

Until now our tutorials have focused on getting most of the server up and running. In fact, you could stop at the end of the last tutorial and run your site for some time, installing to occasional updates to both server packages and anything related to WordPress.

These next few tutorials are considered more “advanced” in that you will be building upon your previous experience of making it through the other tutorials. Your site, will typically be getting a decent amount of traffic per day (upwards of 5,000 unique visitors) before you want to attempt any of these next tutorials.

However, if you are like me, and you want to try and get the fast possible load time for your pages under extreme conditions, then you’re going to want to stick around for these!

Our next set of tutorials are going to focus on installing caching related packages on our server and getting them setup to work with WordPress. So let’s get started!

Installing APC, Alternative PHP Cache

APC is great at saving PHP operation code (opcode) in the RAM memory so if requested again, executes it from there. This basically bypasses the parsing and compiling steps and minimizes some unnecessary loads on the server.

Since this tutorial builds upon our previous LAMP tutorials, you’ll need to be at a point where Apache, MySQL and PHP are installed.

Our first step is to take care of a couple dependencies. So let’s install those first with the following command:

sudo apt-get install php-pear php5-dev make libpcre3-dev

Next, we can install APC using pecl:

sudo pecl install apc

There’s going to be a series of questions but unless you know exactly what you are enabling (which to this day I sometimes have trouble with this part), let’s just go with the defaults by hitting Enter.

Our final step of the installation process is to add the following line to the bottom of our php.ini file:

extension = apc.so

If you don’t remember where your php.ini file is located, you can find it here:

/etc/php5/apache2/php.ini

Save the file and restart apache!

Nginx Users

For Nginx users, rather than restart Apache, you just need restart php fpm with the following command.

sudo /etc/init.d/php5-fpm restart

Checking APC

If you’ve been following along with this series, you’ll remember that I love to make sure we have things working correctly! So we’re going to check and see if APC is now enabled.

We can check by loading up our info.php file. If you don’t have one, we’ll go through creating one right now.

Let’s add an empty php file in our www directory:

nano /var/www/phpinfo.php

We’ll add the following code to the file:

<?php phpinfo(); ?>

Save, exit, and open the browser. We should see APC enabled in there.

*Note It’s generally not a wise choice to keep this file up here all the time. Remove it when we’re done. Or you can protect the file with your .htaccess file.

Configuring APC

So now that we have installed APC and we know it’s running, we need to configure a couple of things.

First, reopen the php.ini file and at the end of the file paste the following:

apc.shm_size = 64

What this does is allocate 64MB from the RAM to APC to cache opcode. We can increase this number depending on what VPS you are using.

Another line that you can paste below is the following:

apc.stat = 0

The apc.stat setting checks the script on each request to see if it was modified. If is was, it will recompile it and cache the new version. This is the default behavior. Setting it to 0 will tell APC not to check for changes in the script. It improves performance but it also means that there if there are changes to the PHP script, they will not be reflected until the VPS is restarted. Therefor setting it to 0 is only recommend on production sites where you are certain this is something you want.

So now that APC is up and running, there is a nifty little page you can use to check its status and performance. You can locate an apc.php file in /usr/share/php/ folder. You have to move this file somewhere accessible from the browser — let’s say our www directory we’ve been using:

cp /usr/share/php/apc.php /var/www

Now we can navigate to:

http://your-domain-here.com/apc.php

If all works as expected you’ll get some interesting statistics about APC. What you need to pay attention to is that APC has enough memory to store its information and that there is not too much fragmentation.

A good indicator that APC is doing its job is if the Hits rate is significantly higher than the Misses rate; the first should be somewhere over 95% after a few requests already.

What’s Next?

APC is very easy to install and manage for your sites hosted on a VPS. With APC installed you can use some more advanced features of WP Total Cache. If you want some homework, I would suggest playing around with WP Total Cache and hook up APC to it. This will be a fun little exercise!

If you run into issues let me know! Next tutorial, we will be looking at installing Varnish on a LAMP VPS.

Spread the word!

Leave a reply

Copyright © 2016 Vesped Inc. All Rights Reserved. Proudly Powered by Headway and WordPress