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.
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.
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:
Where the line says DAEMON-OPTS=”-a :6081 , change it to match the following:
You should now see something like the image below:
You can now save the file and exit out of it.
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:
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:
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:
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:
Change the part inside the red rectangle to match the following:
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:
To see that the varnish caching is working, run the following command: varnishstat. This should display the following:
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.
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.
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:
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:
To exit from these stats, press the Ctrl and C keys.
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:
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.