Setting Up a Web Development Server – Part 5 – Installing Varnish Caching and Memcached

Varnish Caching and Memcached

Image sourced from Aljt Media.

In Part Four, we installed the L.A.M.P stack – consisting of an Apache web server, MySQL database management system, and the popular open-source language PHP. In Part Five, we will be installing two applications that can increase website performance – Varnish Caching and Memcached.

To begin Part Five, we will install Varnish Caching – which is a web server application that enables faster loading time for web pages, and an application I highly recommend for development and production machines.

Step One

To begin installing varnish caching, run the following command: apt-get install varnish. Accept any prompts you see by typing the letter y and pressing the Enter key.

Step Two

In order for varnish caching to work on our machine, we will need to do some extra configuration. To begin, run the following command: vim /etc/default/varnish.

Navigate down the file until you see this:

Varnish Caching Initial Configuration

Where the line says DAEMON-OPTS=”-a :6081 , change it to match the following:

DAEMON-OPTS=”-a :80

You should now see something like the image below:

Varnish Caching Configuration Altered

You can now save the file and exit out of it.

Step Three

We will need to open another one of varnish caching’s configuration files – called default.vcl. This file is use to tell varnish where it should look for webserver content. To edit this file, run the following command: vim /etc/varnish/default.vcl. Navigate to the line in this file where it says “# Default backend definition. Set this to point to your content server.”, and note what is on the four lines after. If yours looks like the image below, this file doesn’t need any editing:

Varnish Cache Default VCL File

Step Four

To make varnish caching work with Apache, we will need to change some more settings. In the previous image, varnish caching is configured to run on port 8080, but Apache is currently listening on port 80. To make the necessary changes, run the following command to open up the ports.conf file: vim /etc/apache2/ports.conf. Upon opening the file, you should see something like the following:

Apache Ports Configuration File

The part of the file with the red rectangle is where we will make the required changes. Change this part to match the following image:

Apache Ports Configuration File Altered

Step Five

Once you have saved the changes and closed the file, we will need to apply the same changes to Apache’s site default configuration. Run the following command to open up the relevant file: vim /etc/apache2/sites-available/default. This should show you the following:

Apache Default Site Configuration

Change the part inside the red rectangle to match the following:

Apache Default Site Configuration Altered

Save the changes and close the file.

To reload the changed configuration, we will need to restart the Apache and Varnish services. To do this, run the two following commands:

service apache2 restart
service varnish restart

If all goes well, you should see the following output:

Restart Apache Server and Varnish Caching Services

Step Six

To see that the varnish caching is working, run the following command: varnishstat. This should display the following:

Varnish Caching Is Working

If you open localhost:8888/phpinfo.php and refresh the browser, you can see the numbers above update in real time. This means that our varnish caching installation is complete.

Step Seven

Now we have installed Varnish Caching, we can move on and install Memcached. Memcached is a memory object caching system that can speed up database and A.P.I-driven websites, by storing related information in RAM to lessen the number of API and database calls.

Before we install Memcached, we should check our machine is up-to-date. Run the following command to do so: apt-get update && apt-get upgrade. After the updating has completed, run the following command to install Memcached: apt-get install memcached php5-dev. If prompted to install extra packages (dependencies required by Memcached to run), type in the letter y and press the Enter key.

Step Eight

Now Memcached has been installed, we can test it to see if it is working. To do this, run the following command: ps –eaf | grep memcached. If it is working, you should see something like the image below:

Memcached Working Test One

To run an extra test, and to see more verbose stats for Memcached, run the following command: echo “stats settings” | nc localhost 11211. You should see something like the following image:

Memcached Verbose Stats

To exit from these stats, press the Ctrl and C keys.

Step Nine

To enable PHP and Memcached to work together, our development machine needs the ‘memcache’ extension. To see if it is installed and working, run the following command: php -i | grep memcache. If it is installed and working, you should see the following output:

Memcached Has Been Successfully Installed

We can ignore the PHP warning that is displayed – the rest of the output shows that Memcache is installed and working. This concludes our install of Memcached, and also Part Five.

Part Five Conclusion

In Part Five, we installed two applications which can speed up website load times and performance – Varnish Caching and Memcached. We also made changes to Apache’s default site configuration to factor in the Varnish Caching installation. In Part Six, the last of this series, we will be installing PhpMyAdmin – a popular web-based tool for managing MySQL databases.

Part Five Resources

Posted in Apache, Linux, MySQL, PHP, Systems Administration, Web Development Tagged with: , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*