Running a Drupal Site on Amazon Web Services

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). Running an optimized Drupal stack has never been easier or more affordable. 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 aws.amazon.com. 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 aws.amazon.com. 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.

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

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.

sudo apt-get update
sudo apt-get dist-upgrade

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.

sudo mkdir /www
sudo mkdir /db
sudo chown ubuntu:ubuntu /www /db

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”.

sudo mkfs.ext4 /dev/xvdf
sudo mkfs.ext4 /dev/xvdg

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

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

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

sudo mount -a

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.

sudo apt-get install apache2

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

sudo apt-get install mysql-server

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

sudo apt-get install php5 php5-gd php5-mysql php-apc

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.

sudo apt-get install memcached libmemcached-tools php5-memcached

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

curl <a href="http://repo.varnish-cache.org/debian/GPG-key.txt">http://repo.varnish-cache.org/debian/GPG-key.txt</a> | sudo apt-key add -
echo "deb <a href="http://repo.varnish-cache.org/ubuntu/">http://repo.varnish-cache.org/ubuntu/</a> precise varnish-3.0" | sudo tee -a /etc/apt/sources.list

Now we are ready to install varnish.

sudo apt-get update
sudo apt-get install varnish

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

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

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.

sudo a2enmod rewrite expires headers

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.

NameVirtualHost *:8080
Listen 8080

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

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

Last, restart the apache server to load the changes.

sudo service apache2 restart

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:

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

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.

sudo service mysql stop

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

sudo mv /var/lib/mysql /db
sudo ln -s /db/mysql /var/lib/mysql

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

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

Start the mysql service.

sudo service mysql start

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:

extension=memcache.so

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.

sudo service memcached restart
sudo service apache2 restart

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.

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

Next, edit the file at /etc/varnish/default.vcl and replace with a Drupal optimized version located at https://fourkitchens.atlassian.net/wiki/display/TECH/Configure+Varnish+3+for+Drupal+7.

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

backend default {
  .host = "127.0.0.1";
  .port = "8080";
}

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.

sudo service apache2 restart
sudo service varnish restart

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.

drush dl drupal

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.

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

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.

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

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

Blog Type: 
Tags: 

Comments

503 problem

I followed your post but I am getting a 503 error page.

Check the apache and php

Check the apache and php error logs. You might find additional details there.

Clean URLs

This post has been very helpful for someone new to AWS. The screenshots are only slightly outdated (as of today) but all of the information is still there. I didn't follow through all of the steps since I'm just trying to get a demo site online and not worry about some of the performance steps like varnish, etc.

One thing that did take me a while to figure out was getting clean-urls enabled. I was installing commerce_kickstart and noticed there was no .htaccess file in the drupal root folder. I pulled the latest from drupal core and added it but that still wasn't taking effect. What I needed to do was...
1) open up /etc/apache2/sites-available/default
2) Change "AllowOverride None" to "AllowOverride All" and save the file.
3) Restart apache
After that I was able to turn on clean-urls in drupal.

Thanks for the great write-up!

Just ignore me

I went back over the blog post and noticed I simply missed that step. It's all there. Just ignore me. :-)

Apparmor blocking use of tmpdir

Despite adding line "/run/mysqld/ rw," and reloading apparmor with "sudo service apparmor reload", I kept getting this error:

/usr/sbin/mysqld: Can't create/write to file '/run/mysqld/ibcrox5X' (Errcode: 13)
140228 0:16:04 InnoDB: Error: unable to create temporary file; errno: 13
140228 0:16:04 [ERROR] Plugin 'InnoDB' init function returned error.
140228 0:16:04 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
140228 0:16:04 [ERROR] Unknown/unsupported storage engine: InnoDB
140228 0:16:04 [ERROR] Aborting

I used the workaround of changing apparmor to complain mode for mysqld:

1) sudo apt-get install apparmor-utils
2) sudo aa-complain /usr/sbin/mysqld

Then I was able to start MySQL without problems.

Another option

I was unaware that this was an option to quickly test Drupal on AWS:

http://goo.gl/mJj9BG

Bitnami provides a free distribution in the Amazon Marketplace.

bitnami

Is there any reason only to use that Bitnami AMI for testing? What are the benefits of following the steps above?

Bitnami

I haven't tried using Bitnami.

Drupal on amazon cloud

I am quite new to cloud, but do I need to update Mysql server versions and Apache myself when using Amazon ?

Yes, you would need to update

Yes, you would need to update them yourself. That isn't too difficult to do if you are used to using a package manager.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Image CAPTCHA
Enter the characters shown in the image.