If you are using Amazon EC2 instance to run WordPress, then one of the biggest challenges you will encounter is running out of system memory.  The server works fine for vanilla installs, but issues typically arise once you add some plugins.  For instance, these system memory problems can manifest themselves in the form of database crashes or system downtimes.  Luckily, this is an easy problem to fix without having to deal with expensive hardware upgrades.  Instead, all you need to do is add a swap drive in order to increase the memory available for your applications.


What is a Swap Drive?

When a computer runs it’s system and user applications will use up memory.  However, the operating system treats this memory differently from other storage space.  Once the system memory is full, the system cannot run any new applications.  Thankfully, Operating Systems have a way to expand the system memory by allowing users to configure a swap space.

Swap space is hard drive storage space that is specially allocated to extend the memory.  Things in memory that are not immediately necessary (a background application for instance) can go into the swap space until they are needed again.  It does take some time to swap a request out to this swap space, but it is faster than closing and re-opening a program.


How do I add a Swap Drive

The steps to create and turn on a swap drive are simple and can provide more power to your server immediately.

First, we create the swap file.  I name my space “swapdrive”, but you can call it whatever you want.

sudo /bin/dd if=/dev/zero of=/var/swapdrive bs=1M count=1024

Then we configure the file to be swap space.

sudo /sbin/mkswap /var/swapdrive

Next, we set the permissions

sudo chmod 600 /var/swapdrive

Finally, we turn it on and assign our file

sudo /sbin/swapon /var/swapdrive

Now you have another gigabyte of memory available to your system.  You can adjust the “1024” in step one to add more or less.  The last thing you want to do is to setup the swap drive to startup when the system starts, so it is always available.  Add this line to the /etc/fstab file.

/var/swapdrive swap swap defaults 0 0

That’s it.  You now have more memory available to your server, and the extra GB of memory has been more than enough in our experience with WordPress.  If your results are different, let us know.


Further Reading

Here are some other articles to help you customize your servers:

Consider checking out the following books.  These are an excellent source of details about understanding and configuring your Amazon Linux instances.

[sgmb id=”1″]

 

Rob Broadhead

Rob is a founder of, and frequent contributor to, Develpreneur. This includes the Building Better Developers podcast. He is also a lifetime learner as a developer, designer, and manager of software solutions. Rob is the founder of RB Consulting and has managed to author a book about his family experiences and a few about becoming a better developer. In his free time, he stays busy raising five children (although they have grown into adults). When he has a chance to breathe, he is on the ice playing hockey to relax or working on his ballroom dance skills.

Leave a Reply