1. Home
  2. Training Library
  3. Amazon Web Services
  4. Courses
  5. How to Install and Run Wordpress on AWS

Optimizing performances with Varnish

Start course

WordPress is an open-source CMS originally built as a web publishing platform, quickly becoming a de-facto standard for blogs. Thanks to its huge third-party plugin ecosystem, WordPress has been adopted for use in many different situations never imagined by its creators, including dynamic websites, e-commerce platforms, and online newspapers. It's a terrific software package with a huge user base, but getting the most out of it can be tricky.

This course demonstrates installing and running WordPress on Amazon Web Services. Expert Linux System Administrator David Clinton will guide you through installation, from the easy way (using a Cloudformation template), up to deploying a highly customizable instance on EC2 and RDS. You will learn to use optimization tools like Varnish and Route53 and to monitor availability and costs with CloudWatch.

If you have thoughts or suggestions for this course, please contact Cloud Academy at support@cloudacademy.com.

Intended Audience

This is an intermediate course that will assume some basic knowledge of the AWS system. Some familiarity with the Linux Command Line Interface and MySQL might also be helpful.To move to the next step, check out our EC2 and RDS courses, and our introductory AWS video. You might also enjoy our courses on CloudFormation, LAMP stacks on AWS and Security on Linux based AWS instances, which make great follow-ups. If you want to challenge yourself, check out our questions.



Hi and welcome to CloudAcademy.com's video series on running your WordPress site on Amazon's AWS. In this video, we're going to discuss optimizing the speed with which your WordPress pages load using cache software, like Varnish, well actually precisely Varnish. Varnish is a caching HTTP reverse proxy. That means when a client browser, one of your users on the internet, requests a page from your WordPress site or some data that is handled from the database on your WordPress site, if Varnish hasn't seen such a request before, it will turn to the back end, meaning your WordPress instance, and retrieve the data.

Once it's retrieved the data though, it doesn't just give it away and forget about it. It caches it, it stores that data in its own database. Any repeat requests, in other words, any time a client browser will request data that Varnish has already given, has already delivered, Varnish this time will deliver the data from cache, meaning it's going to be a whole lot faster. Not only can Varnish work with a single back end, a single WordPress instance or database, but it can actually link together several back ends to allow load balancing. You could have a number of servers all feeding data into the cache of Varnish, and Varnish serving up all that super fast cached data to users. Just a little bit of conceptual detail. The back end server in Varnish's way of phrasing it, a back end server is really your WordPress instance built on top of a database, but it's also described as .host. In some of the Varnish configuration files, any .host is a reference actually to the back end server.

Of course, that's also synonymous with whatever your address is, MySite.com, or whatever your address happens to be. Let's now install and configure Varnish.

We now want to download Varnish and install it on our EC2 instance, but we don't necessarily want to use the repo version, the version that's in the normal Ubuntu repositories, because they sometimes are a version or two behind the one you can actually download from Varnish's own sources. So let's first sudo apt-get update to make sure that Ubuntu here is familiar with all the possible repository information available. Now, sudo apt-get install apt-transport-https.

This will allow us to enable external repos in the apt-get source's .list file. That's done. Now, it very often, more often than not, you have to be the root for the next step. We're going to take the contents of a Varnish webpage, which includes a GPG key, and we're going to pipe that to apt key, to add this key to our system. Now, we're going to add a line to a file called Varnish-cache.list, and the line which we are applying using the echo command, will add a repository, a Debian repository, which is a source for up to date Varnish software to that file.

That's done. Now, let's just exit the SU administrator permissions shell, and go back to our regular sudo. It's always safer not to be the administrator full time, if you can. So, sudo apt-get update once again to let Ubuntu know that there's a new repository to it. And now, sudo apt-get install Varnish.

We certainly do want to install this, even though it's going to take some time. There's quite a lot to download. And that's done. Varnish should now be installed. Let's check to make sure that it's actually up and running now, by typing sudo service varnish status, which will tell us the system status of Varnish.

It's running. Varnish will now read any traffic coming in, using Port 6081, and output nothing. We have to tell Varnish to use whatever is available on Port 80 and output it through 6081. We do that by sudo nano, by editing in other words, /etc/varnish/default.vcl. We are specifically looking for the host and port lines. In the host line, rather than the, which is local host, we're going to use the IP address of our instance. Of course, if you have a proper URL, a DNS URL of yoursite.com, you would use that. In our case, it happens to be right now, And the port we would like to be 80. Let's hit control X, Y to say yes, we want to overwrite this file, and enter. In order for Varnish to pick up the change, we have to hit sudo service varnish reload. Not restart, but in this case, reload.

Now, to move Varnish itself down to Port 80, we'll have to edit using nano, the terminal text editor that I use, the file /etc/default/varnish. That's a file obviously that was created when Varnish was installed. We have to edit one single line and remove the hash marks from all these, because effectively, these six lines are really all one line of bash command. And we know that because each one ends with a backslash, telling Ubuntu that whatever comes on the next line is to be read as part of the same command. At any rate, we're going to change the port that DAEMON_OPTS will access from 6081 to 80.

Control X, Y to save the file, and once again, sudo service varnish reload. Everything should be up and running.

About the Author
David Clinton
Linux SysAdmin
Learning Paths

David taught high school for twenty years, worked as a Linux system administrator for five years, and has been writing since he could hold a crayon between his fingers. His childhood bedroom wall has since been repainted.

Having worked directly with all kinds of technology, David derives great pleasure from completing projects that draw on as many tools from his toolkit as possible.

Besides being a Linux system administrator with a strong focus on virtualization and security tools, David writes technical documentation and user guides, and creates technology training videos.

His favorite technology tool is the one that should be just about ready for release tomorrow. Or Thursday.