This tutorial will step us through the creation of a Memcached server on Rackspace cloud servers.  The ultimate goal will be to have a dedicated Memcached server which will be accessed by multiple web servers that are load balanced in a Rackspace cloud hosting environment.

There are a few reasons to set up a dedicated Memcached server:

  • Performance: We will custom-prepare the server for running Memcached, not web pages, this server will serve as a shared caching environment across all of our web servers
  • Scale: We will be able to scale our web site or web application quickly by adding additional web servers behind a load balancer, without having to worry about our caching server

Creating the New Server

  1. Log into our Rackspace cloud account at https://mycloud.rackspace.com/, and click on the “Servers” tab in the primary navigation menu
  2. Once the “Servers” page has loaded, click on the “Create Server” button
  3. Once the form loads, start by naming our new server “Memcached
  4. Under the “Regions” drop-down, Look for the “First Generation Servers” and select the “Dallas (DFW)” option
  5. Under the “Image” heading, select the “Ubuntu 12.04 LTS” operating system
  6. Under the “Flavor” heading, select top option, which is “512 MB RAM / 20 GB Storage” (If at some point our websites require additional caching resources, we can scale up the RAM and disk space)
  7. Finally, click on the “Create Server” button at the bottom of the form
  8. Save the password that Rackspace issues, we will use it to login and configure our server in the next section of this tutorial

When the server is done being built, we will be able to access the new server from the “Servers” tab in our Rackspace Cloud account.  Rackspace will assign two IP address to our server.  One is called the “PublicNet” IP address, that is the external IP address used accessing our server from the Internet.  The other is the “ServiceNet” IP address, this is the internal Rackspace IP address that our web servers will be accessing.

Configuring the Memcached Server

We are now ready to access our new server.  To log in to our server, we will need to use the secure shell (SSH) protocol. Open a SSH terminal window and enter the following command, inserting the PublicNet IP corresponding to the server.  When prompted, use the password Rackspace issued for our server in the previous step.

ssh root@[PublicNet IP Address]

Type yes to verify the identity of our Cloud Server. This will prompt an RSA key to be stored in our computer’s list of known hosts. This message will not be presented upon subsequent logins.

We need to check for the latest versions of Ubuntu’s installed packages, so we need to make a list of what is already installed. To do this we will run the following command:

sudo apt-get update

This command will update the package listing for the operating system to interact with and the information is cached for the Aptitude package manager to use in operating system upgrade.

We can now check for the latest version of the items in the installed packages list, enter this command:

sudo apt-get upgrade

Apt-get will prompt to review the packages. When prompted to continue, enter a capital ‘Y’. This step will take several minutes.

During the update process the OS will appear and prompt with some options, make sure to choose “Install the package maintainers version” to ensure use of the latest version of GRUB.

Once the upgrades are finished running we will reboot the server to ensure that it is running the latest and greatest version with all current packages.

sudo reboot

Next we can configure Ubuntu’s “uncomplicated firewall” (UFW).

First we’ll will turn off all ports on the server except for the specific ones we’ll need for security reasons.  We can do this by simply enabling UFW with the following command:

sudo ufw enable

We will receive a note this may disrupt SSH connections. No worries since we will open the SSH (22) port next. When prompted “Proceed with operation (y|n)”, press “y” to continue.

Now we will open the ports required for Memcached (11211) and SSH (22) with the following commands:

sudo ufw allow 11211
sudo ufw allow 22

Now the firewall will only listen for connections on the ports we have specified. These ports apply to the Memcached and SSH protocols.

Installing and Configuring Memcached

Now that the Operating System has been updated and secured, we are ready to install and configure all of the software needed for running Memcached on your server. The process will not take long but it does involve several steps, start by running the following command:

sudo apt-get install php5 php5-memcache php-pear memcached

Next, if you do not have a compiler on your server, you can download build-essential in order to install memcache:

sudo apt-get install build-essential

Finally use PECL (PHP Extension Community Library) to install memcache:

sudo pecl install memcache

We will say “yes” when prompted by pressing enter during the installation when asked if we would like to “Enable memcache session handler support? [yes] :”

Once we have completed the installation of memcache with PECL on the VPS, we will add memcached to memcache.ini:

echo "extension=memcache.so" | sudo tee /etc/php5/conf.d/memcache.ini

Adding Memcached to Web Servers

We are now ready to add Memcached to the web servers in our hosting environment by repeating the Memcached installation process on our web servers.

We will also need to make sure and update the IP Tables on our web servers to allow for Memcached traffic on port 11211.

Once Memchaced is installed, we will need to set the IP to local internal Rackspace IP address of Memcached server, to do that we will open the memcached.conf file and update the port from 127.0.0.1 to your new IP Address:

sudo nano /etc/memcached.conf

Now we will restart our Memcached service:

service memcached restart

Rinse and Repeat on All Web Servers

Install Memcached and update the config files on all the servers that are sharing the same cached files.

WordPress and W3TotalCache

If we are running a WordPress website, we can simply change the Memcached server location in the W3TotalCache config file by pointing to the internal Rackspace IP address of the Memcached server

[Path to WordPress install]/wp-content/w3tc-config/master.php

References (for more detailed information, be sure and follow these links)
https://www.digitalocean.com/community/articles/how-to-install-and-use-memcache-on-ubuntu-12-04
http://www.rackspace.com/blog/setting-up-memcached-on-cloud-servers/

Comments

  1. Don Wilson

    For a dedicated memcached server, there’s no reason to install the following services through apt-get, they’re just wasted resources:

    mysql-server php5-mysql php5 php5-memcache php-pear

    1. Alex

      Interesting statement. Let us know your approach, that will come in handy.

Leave a Reply

On The Web

Categories