Amazon AWS EC2
This chapter shows how to create a new vhost server instance with a LAMP stack on the Amazon AWS EC2 cloud service.
Table of contents
- Introducton
- Requirements
- Setting up Amazon AWS EC2 LAMP
- Enable Apache mod_rewrite
- Set up swap
- Test your configuration
- Final word
Introduction
This document is part of the internal documentation for Hannemyr Nye Medier AS. Its main audience is staff helping clients to set up managed hosting for Drupal on IaaS provided by Amazon AWS. It is not read-protetected by choice. We are a transparent company and also want our clients and others to have public access to this information.
Amazon AWS is a cloud service provider that rents access to virtual machines, called instances suitable for use as a virtual private server (VPS) to run as a web host.
This chapter describes to select, set up, and interact with one such virtual machine.
This note was originally written in 2013 and only some bits have been updated since then. I suspect that this is now seriously outdated. The instance described below is probably some RedHat derivative (RHEL, Fedora, CentOS). I no longer use AWS EC2, nor do I use RedHat. As of 2018, I consider DigitalOcean and Ubuntu a better platform for running Drupal (YMMV). Here are my corresponding page about setting up a DigitalOcean Droplet.
Requirements
[2018 update]: I think Ubuntu 18.04 LTS is a better distribution of Gnu/linux than the RedHat derivative I set up in 2013. For Drupal 8, the instance type named “t2_small” seems adequate for the type of simple Drupal site I usually create. The instance type I use provides:
- Ubuntu 18.04 LTS
- 2 GiB RAM
- 4 GiB Swap
- 1 vCPU (kernel)
- EBS (disk)
The EBS (Elastic Block Storage) let you specify the size of disk in a separate step. I run about 50 Drupal sites on a single AWS EC2 instance, so I have a 50 GiB disk. You obviously need to find what size is best for your intended use. The great thing about AWS EC2 is that it is so simple to scale.
Setting up Amazon AWS EC2 LAMP
In this section I'm going to show how to create a LAMP server on Amazon AWS EC2 cloud service. There are only four steps you need to go through.
- Register an AWS account with Amazon and log in with the browser.
- Create a new server instance from an Amazon AMI and log in using ssh.
- Install MySQL, PHP and Apache.
- Test your configuration.
Register an account
It is simple to get started with Amazon AWS. Just use your favourite web browser and sign up for their Free Usage Tier. The free usage tier only allows you to use a micro instance.
After creating the account log on and select the
. You're now in the AWS EC2 dashboard and ready to launch an AWS EC2 instance.Create a new server instance
Go through the following ten steps to create and start a new server instance.
- Click the button.
- Make sure the
- Locate the following image , make sure is selected, and click .
- Make sure that the free T1 micro instance ( ) and launch instance is selected , and click .
- On the next screen ( ) leave the defaults and click on .
- On the next screen ( ) leave the defaults and click on .
- Fill in the column with the name you want to associate this instance (it will show up in your dashboard). Click on .
- Unless you have already done so,
mykey.pem
.
. Creating this key pair is important because it is needed to connect to your server using ssh.
Enter the name for your key pair, then click on . Make sure you save the file with the key pair and set its permissions to 600 (it will not work otherwise). Name the file - Create A Security Group. This sets up the firewall rules for your server instance. Create a new security group and add both HTTP and SSH access.
- Click on to create the instance and start the server.
Expect it to take some minutes before the new server instance is fully set up and ready for use.
When the instance is ready for use, you will be able to log in in the new server instance with ssh. The username will be ec2-user and the hostname will be available in your AWS EC2 dashboard. In this example, I will use the hostname ec2-107-22-130-254.compute-1.amazonaws.com. However, when you go through this exercise, you will be assigned a different hostname. Make sure you use the hostname assigned to you.
Having the file with the key pair, the user name and the hostname is what is needed to log in using ssh. Here is how I log in to my instance:
$ ssh -i mykey.pem ec2-user@ec2-107-22-130-254.compute-1.amazonaws.com
After logging in, the next step is to install the necessary software to set up an interactive website (i.e. MySQL, PHP and Apache).
Install MySQL, PHP and Apache
After logging in, use the following commands to install MySQL, PHP and Apache (httpd):
$ sudo yum update $ sudo yum install mysql mysql-server $ sudo yum install php php-mysql php-xml php-mcrypt php-mbstring php-cli php-gd $ sudo yum install php-pear $ sudo yum install php-devel $ sudo yum install httpd
Then configure Gnu/Linux to re-start Apache and MySQL automatically after a reboot:
$ sudo /sbin/chkconfig httpd on $ sudo /sbin/chkconfig mysqld on $ sudo /sbin/service httpd start $ sudo /sbin/service mysqld start
Set a SQL password for root @ localhost:
$ /usr/bin/mysqladmin -u root password 'the-password'
You should of course pick something a bit more obscure than “the-password” for the password.
Alternatively, you can run (recommended for production):
$ /usr/bin/mysql_secure_installation
This will also give you the option of removing the test database and the anonymous database user (created by default).
Enable Apache mod_rewrite
In order to have clean URLs with Drupal, it is necessary to allow
directives in the installation's .htaccess
file to
override the default configuration. To faciltate this, navigate to
/etc/httpd/conf
and edit the file named
httpd.conf
.
Locate the following block of text:
<Directory /var/www/html> […] AllowOverride None […] </Directory>
And change the AllowOverride
directive to:
<Directory /var/www/html> […] AllowOverride All […] </Directory>
When done, restart Apache:
$ sudo service httpd restart
Set up swap
The Amazon AWS EC2 Micro instance only have 613 MB of memory. This is unfortunately too little for Drupal and MySQL. MySQL will crash when it runs out of memory. A typical symptom is that a lock file created by MySQL is not removed, locking Drupal out of its database. A typical error message will look like this:
PDOException: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in lock_may_be_available() (line 167 of /var/www/html/includes/lock.inc).
Since there is no default swap space for Amazon AWS EC2 Micro instances, running out of memory is fatal. To avoid this you need to set up a swap space.
The steps below show how to make a swap space on an Amazon AWS EC2 Micro instance:
$ dd if=/dev/zero of=/swapfile bs=1M count=1024 $ mkswap /swapfile $ swapon /swapfile
To automatically enable swap file after each reboot. add this line to /etc/fstab
:
/swapfile swap swap defaults 0 0
Some useful command related to managing memory and swap devices:
$ free # Display the amout of free and used memory. $ swapon -s # Display swap usage summary by device. $ swapon -a # Enable swapping for all devices. $ swapoff -a # Disable swapping for all devices.
For any real Drupal website, you should not use a micro instance.
Test your configuration
After setting up, you should run some simple tests to verify that everything works as it should. Here are some suggestions.
To test MySQL:
$ mysql -u root -p -e 'SHOW DATABASES' Enter password: +--------------------+ | Database | +--------------------+ | information schema | | mysql | | performance schema | | test | +--------------------+
To test that the Apache HTTP server is operating correctly, just point your browser to the domain you've been allocated. In my case, this is: http://ec2-107-22-130-254.compute-1.amazonaws.com.
If Apache is working, and you've not yet created any content, you'll get a page with the heading “Amazon Linux AMI Test Page”. This page also tells you how to proceed to create real content on your new web server.
Creating a file /var/www/html/index.php
with the following content will let you test PHP:
<html> <head><title>PHP test</title></head> <body> <h1>Hello, world!</h1> <?php phpinfo(); ?> </body> </html>
If you again point your browser to the domain you've been allocated after creating and installing this file in the webroot, and the browser shows a page with a long table with detailed information about the PHP configuration, PHP works.
Final word
[TBA]
Last update: 2018-09-10 [gh].