Open Source

Build a Drupal installation on Amazon EC2

Nick Hardiman performs a Drupal install on his Amazon EC2 machine and provides a cheat sheet of the steps and commands that he used to do it.

Here's a holiday recipe for infrastructure nerds like me: how to build a working Drupal install on an EC2 machine. I build Drupal on my Amazon Basic Linux micro-instance, and not the more popular Ubuntu-flavoured AMIs. I'll cut straight to the action here, then explain why I've done it this way in a later post.

Below are the couple dozen commands required to make Drupal go, in the form of a cheat sheet. I have included very few explanations of the commands, config or anything else to make it easy to learn. This is all CLI voodoo, only understood by Linux sysadmins and other computer nerds.

The three key ingredients are PHP, MySQL, and Drupal. It should not be followed by anyone unwilling to spend time researching what on earth is going on and to waste time troubleshooting. In fact, a web developer could spend his entire career getting to know these ingredients.

Time taken to complete is wildly variable. If everything goes smoothly, this process can take minutes. If something goes wrong there is no end to the time that can get sucked up in troubleshooting. There is a huge amount that can go wrong here. I have made one simple mistake and wasted days trying to find it. Computers are unforgiving.

Install PHP

The first thing to cook up is a PHP processor. Follow these four steps (I often use four steps to build an enterprise application).

  1. Install. The command is yum install php
  2. Configure. The most common change is tweaking the memory limit.
  3. Run. The web server needs a restart to pick up the new PHP processor module.
  4. Check. php -v
  5. The next thing to do is spice up PHP with a selection of optional extras:

    yum install php-mysql  php-xml  php-mcrypt  php-mbstring php-gd

    Each of these packages depends on other packages. Installing php-mysql also installs mysql-libs and php-pdo; php-xml depends on libxslt; and php-mcrypt needs libmcrypt and libtool-ltdl. The yum package install system takes care of working all this out automatically.
  6. I need a few tools to help me get even more PHP extras: PEAR (PHP Extension and Application Repository), PECL (PHP Extension Community Library) and files needed for building PHP extensions:

    yum install php-pear

    yum install php-devel
  7. Drupal can seem a little slow. I can speed it up with APC (Alternative PHP Cache): yum install php-pecl-apc

Install MySQL

  1. Install. yum install mysql-server
  2. Run. /etc/init.d/mysqld start
  3. Configure. mysqladmin -u root password 'Pa55word'
  4. Check. mysql -u root -p

Install a compiler

A compiler is used to build tools from source code. Not every optional extra is available as a binary installation package in Amazon's repository. Sometimes I need to compile code.

Security experts don't like the taste of a compiler on a production machine. They will not allow a compiler near the general public because of a history of abuse: in the right hands, it could give full system privileges.

yum install gcc

Install Drupal

Copy

  1. wget http://ftp.drupal.org/files/projects/drupal-7.10.tar.gz
  2. gunzip drupal-7.10.tar.gz
  3. tar xf drupal-7.10.tar
  4. mv drupal-7.10/* /var/www/html/
  5. mv drupal-7.10/.*  /var/www/html/

Configure

  1. cd /var/www/html/sites/default/
  2. cp default.settings.php settings.php
  3. chmod 666 settings.php
  4. vi settings.php (this is where I tell Drupal where the database is)
  5. chmod 777 .

Clean up

  1. cd
  2. rm -rf drupal-7.10/
  3. gzip drupal-7.10.tar

Create a database and user

These are SQL commands, not shell commands.

  1. GRANT usage ON *.* TO drupal7user@localhost IDENTIFIED BY 'Pa55w0rd';
  2. CREATE DATABASE drupal7db;
  3. GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES, CREATE TEMPORARY TABLES ON `drupal7db`.* TO 'drupal7user'@'localhost' IDENTIFIED BY ‘Pa55w0rd';

The final piece of command line work is a restart of the Apache HTTP Server.

  1. /etc/init.d/httpd stop
  2. /etc/init.d/httpd start

The next part involves configuring Drupal using a web browser.

The last part of the install process takes place in the new Drupal web site so it's time to stop using the command line and start using a web browser. The URL to use is along the lines of 'http://ec2-1-2-3-4.eu-west-1.compute.amazonaws.com/install.php". Typing this URL into a web  browser instructs the web server to run a script named "install.php". This script guides you through the final steps.

Note: This install URL is an example, not a real one. The DNS name for your EC2 machine will definitely not contain the text "1-2-3-4", and it may not have "eu-west-1" either. If you copy this example URL into a web browser, it will fail to find the web site and display an error along the lines of "can't find ec2-1-2-3-4.eu-west-1.compute.amazonaws.com".

Test Drupal

View the resulting example homepage at http://ec2-1-2-3-4.eu-west-1.compute.amazonaws.com/.

Cool and serve

I've built it. Now the editors need to add content and I need to maintain it.

System administration will be ongoing. For instance, my Drupal site does not have much memory to play with, so if the site gets popular, that will need to be managed. If customers perceive my site as slow, I can speed it up by adding a cache, optimising the database, and complaining to editors about their page sizes.

About

Nick Hardiman builds and maintains the infrastructure required to run Internet services. Nick deals with the lower layers of the Internet - the machines, networks, operating systems, and applications. Nick's job stops there, and he hands over to the ...

3 comments
Nick Hardiman
Nick Hardiman

It's not obvious where to run those MySQL commands. 


Run them at the Mysql interface. You can get to the interface using this command (it's in the 'Install MySQL' section).


mysql -u root -p

whoiskevin
whoiskevin

Hey Nick. I think I'm missing the point here. Was it to get started and take this further? This is really just a single machine instance that happens to be a cloud instance. But it seems that using the cloud to full benefit will require separating at least the database (Amazon RDS) and then eventually having a load balancer in front of one or more web/php servers. Other pieces could be abstracted out as well of course.

Nick Hardiman
Nick Hardiman

You're right. If a technical architect handed me a one-box enterprise solution, I would start calling him names. However, this is not the finished product. I will be developing this theme and distributing the components.