Running a Drupal site on AWS

The affordability, availability, and scalability of cloud hosting has made it popular among individuals and companies of all sizes. Leading the way is Amazon Web Services (AWS). We've evaluated the pros and cons of setting up Drupal on Amazon AWS, and found that running an optimized Drupal stack with AWS can be very affordable and simple to set up. Here, I will show how to set up an optimized Drupal server using AWS.

Initial Server Setup and Configuration

First you will need to create an account at Once your account is created, login to the management console. From there, you can access all your services.

Launching a New Server

Once in the dashboard, open EC2 and select launch instance. A dialog will display to step you through the process. Select the classic wizard and you will have a list of choices similar to the ones displayed below.


I prefer the Ubuntu version of Linux due to the large number of packages available in the repository. So, from here select the Ubuntu Server LTS option with 64-bit selected. On the next screen, select the instance type from the dropdown in the upper right. The costs associated with each instance type can be found on Once you have selected your instance type, click on the continue button to move to the next screen.

The next screen has some details regarding the Linux kernal, RAM disk ID, user data, etc. You don’t need to make any changes on this screen, so move on to the next one.

The next screen is the storage device configuration setup. The mounted drives are detailed in the table displayed. Again, you don’t need to make changes here, so move on to the next screen.

This screen allows you to add tags to the instance. These are useful to identify different servers on the EC2 dashboard. The default name key is used for this purpose.

The key pairs screen is where you can add a new key pair or select an existing one to connect to the server. To create a new key pair, enter a name in the text box, then select download. The key goes in the “.ssh” folder for a user on your local computer. Make sure to make a backup of the key because if lost, you will not be able to login to your server.

The security groups screen is where you can select or modify firewall rules. Make sure to add rules for port 80 (HTTP), and 22 (SSH).

On the last screen, you can review all the details of the instance about to be created. Once everything looks good, click on the launch button, and the instance with be created. Typically, the instance will be ready in 30 - 60 seconds.

Setup and Attach Additional Storage

Before connecting to the server, we need to setup any additional volumes need from the Elastic Block Store (EBS). These volumes attach to the server as additional drives. I like to use these for database files, web files, and sometimes log files. The EBS volumes are redundant and persistent. In the case the instance has a hardware failure, EBS will detach and wait to be connected with a new instance.

In the dashboard, select volumes under the elastic block store heading. Click on the create volume button to bring up a dialog.

Select standard as the volume type and enter the amount of storage needed. In the availability dropdown, you will need to select the same zone the instance was create in. The instance zone can be found when looking at the instance details.

Next, right click on each new EBS volume and select attach. This is where you can attach the volume to the server instance. Make sure to set a unique name for each volume. For example, if the first is set to “/dev/sdf”, then set the second to “/dev/sdg”, and so on.

Connect to the Server

Next, connect to the server via ssh. The username for the instance is initially set to “ubuntu”. Initially, you can connect with the public dns setup by Amazon, then once the server is setup, use the elastic ip address or a url pointing to the server.

[text] ssh ubuntu@[public dns/elastic ip/hostname] -i ~/.ssh/[name of key file] [/text]

Install Software Updates

After logging into the server, the next step is to update the system software packages. This is accomplished using the apt-get package manager. Run the following commands to update.

[text] sudo apt-get update [/text][text] sudo apt-get dist-upgrade [/text]

Format and Mount EBS Volumes

Now, we need to format and found the created EBS volumes, but first, create a location for each volume you created and change ownership to the "ubuntu" user.

[text] sudo mkdir /www [/text][text] sudo mkdir /db [/text][text] sudo chown ubuntu:ubuntu /www /db[/text]

Format each drive with the ext4 file system. Ubuntu will change the name of an attached drive. For example, if the attached location is “/dev/sdf”, Ubuntu will name it “/dev/xvdf”.

[text] sudo mkfs.ext4 /dev/xvdf [/text][text] sudo mkfs.ext4 /dev/xvdg [/text]

Add each volume to /etc/fstab. This will mount the EBS volumes after a reboot and at the specified location.

[text] /dev/xvdf /www auto defaults,nobootwait,noatime 0 0 /dev/xvdg /db auto defaults,nobootwait,noatime 0 0[/text]

Run the following command to mount the volumes without having to reboot.

[text] sudo mount -a[/text]

Install Software Packages

Now it is time to install all the other software needed for a Drupal site to function. Most of the installation is straightforward, with the exception of varnish, where you will need to add an external repository to apt-get.

Install Apache.

[text] sudo apt-get install apache2 [/text]

Install MySQL. You will be prompted to enter the root user password. Make sure it is a strong password.

[text] sudo apt-get install mysql-server [/text]

Install PHP5 and APC. APC stands for alternative php cache and helps to speed up PHP by caching compiled code.

[text] sudo apt-get install php5 php5-gd php5-mysql php-apc [/text]

At this point, you have installed all the software needed to run a Drupal site. For faster page load response times, we need to install some additional software to cache requests in memory.

Install memcached.

[text] sudo apt-get install memcached libmemcached-tools php5-memcached [/text]

We need to add a repository to apt-get before we can install varnish. To do that, run the following commands.

[text] curl | sudo apt-key add - [/text][text] echo "deb precise varnish-3.0" | sudo tee -a /etc/apt/sources.list [/text]

Now we are ready to install varnish.

[text] sudo apt-get update [/text][text] sudo apt-get install varnish [/text]

Drush is a very useful tool for Drupal administration from the command line. To install, copy the url from and then run the commands below from the home directory.

[text] wget [drush url] [/text][text] tar -xvzf [name of drush file] [/text][text] sudo ln -s /home/[user name]/drush/drush /usr/bin/drush [/text]

That’s it for the software install. Now we can move on to configuration.

Software Configuration

Some of the installed packages above require little to no configuration, while others require quite a bit. We will go through each of them here.

Apache Configuration

The first thing we need to do is enable the necessary apache modules for Drupal.

[text] sudo a2enmod rewrite expires headers [/text]

Change apache to listen on port 8080 instead of 80. Varnish will run on port 80 and forward non-cached requests to apache. Edit the file at /etc/apache2/ports.conf. Change the following values in the configuration file.

[text] NameVirtualHost *:8080 Listen 8080 [/text]

Change default vhost file. Edit the file at /etc/apache2/sites-available/default and change the following.

[text] *:80 to *:8080 [/text][text] AllowOverride None to AllowOverride All [/text][text] All instances of /var/www to /www [/text]

Last, restart the apache server to load the changes.

[text] sudo service apache2 restart [/text]

MySQL Configuration

There are some changes we need to make to the MySQL configuration to optimize performance and provide more redundancy in data storage. Let’s start by creating our own configuration file for MySQL at /etc/mysql/conf.d/local.cnf. Add the following lines:

[text] [mysqld] bind-address = [internal or elastic ip] tmpdir = /run/mysqld [/text]

The bind-address property needs to be changed to if you intend to use external tools to administer and backup databases. The tmpdir property specifies the location for MySQL temporary storage in memory, rather than on disk.

Next, let’s move the mysql table storage to an EBS volume. First, stop the mysql service.

[text] sudo service mysql stop [/text]

Move the database files and create a symbolic link from the old location.

[text] sudo mv /var/lib/mysql /db [/text][text] sudo ln -s /db/mysql /var/lib/mysql [/text]

Update apparmor configuration at /etc/apparmor.d/usr.sbin.mysqld.

[text] Change /var/lib to /db [/text][text] Add new line /run/mysqld/ rw, [/text]

Start the mysql service.

[text] sudo service mysql start [/text]

Memcached Configuration

After installing memcached, make sure the module is enabled in /etc/php5/conf.d/memcache.ini. There should be a line that reads:

[text] [/text]

Now, we need to update the amount of memory used by memcached. Edit the file at /etc/memcached.conf. Change the line “-m” to whatever value is appropriate for your server.

Restart apache and memcached.

[text] sudo service memcached restart [/text][text] sudo service apache2 restart [/text]

There are a couple of steps to integrate Memcached with Drupal. We will get to those steps a little later.

Varnish Configuration

To configure varnish, we need to edit a couple of files. The first is located at /etc/default/varnish. Scroll down to where you see the following five lines. Change the port on the first line to 80 and the amount of memory used on the last line to a level appropriate for your server.

[text] DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m" [/text]

Next, edit the file at /etc/varnish/default.vcl and replace with a Drupal optimized version located at

At the top of the file, you will notice the following lines.

[text] backend default { .host = ""; .port = "8080"; } [/text]

Change host to the assigned internal or elastic ip. If you are using a different port for apache, change the port to that number.

Restart apache and varnish.

[text] sudo service apache2 restart [/text][text] sudo service varnish restart [/text]

Install and configure Drupal

In the apache configuration section, we had set the web root directory to /www. Now, we are ready to install and configure Drupal using drush. Start by navigating to the /www directory and downloading Drupal.

[text] drush dl drupal [/text]

Copy the files from the created directory to the current directory, then delete the directory created by drush. Copy the default.settings.php file to settings.php in sites/default and make the file user writable.

Create a database for the new site.

[text] mysql -u root -p [/text][text] create database [database name] [/text]

Open a browser using the public DNS, elastic IP, or hostname and follow the instructions to install Drupal. Once installed, we need to edit the settings.php file. Add the following lines.

[php] $conf[‘cache_backends’][] = ‘[path to memcache module]/’; $conf[‘cache_default_class’] = ‘MemCacheDrupal’; $conf[‘memcache_key_prefix’] = ‘[unique prefix]’; [/php]

That’s it for the installation and configuration of Drupal on your new AWS server.