AWS: Spinning Up Some EC2 – Part 1 – PHP

Avoiding Pain

One thing that is frequently a pain is setting up dev servers to work on. This is especially true if you are working at home. Running a bunch of different services on your main system isn’t great as they are frequently performance hogs. Another option is to have a second piece of hardware running everything for you, but I’ve always had two issues with that:

  1. It’s yet another piece of hardware driving my energy bills sky high.
  2. It’s only a clean install once. Wiping the MySQL, Apache, Cassandra, <insert hot tech of the week>, install on a regular basis just isn’t something that happens.

One option I’ve utilized to avoid these issues are cloud services like Amazon’s EC2. They’re relatively easy to configure (once you get the hang of it) and depending on your usage, they may cost little more than the cost of the electricity for that second system you’re currently using. This is especially true if you’re a new AWS customer and you qualify for their Free Usage Tier. And best of all, a fresh system can be spun up rather quickly.

Getting Started

This is going to be a really brief explanation of setting up an EC2 instance. There are so many options out there that trying to cover even a tenth of, say, the Amazon Machine Images would take far too long.

After you signup for AWS, you will login to the AWS Management Console. Select EC2 from the list of services and then click the Launch Instance button. You’ll be presented with two options, Quicklaunch or Launch Classic Wizard. For this writeup, I selected the wizard. I typically start with the basic 64 bit Amazon Linux Amazon Machine Image. Select the Instance Type you need (the Mirco instance is the one that qualifies under the free tier) and the Available Zone you want and then hit Continue. Hit Continue again for the Advaned Options. For the list of keys, I’d recommend just putting a Name key in there. On the next page, create a new key pair if you need to, and make sure to save the key somewhere safe, you will need it shortly. I setup the security groups using the same configuration as this post. You can set them up now, or add them later.

You should now be able to launch the instance. After it’s started, and your security groups are setup, you should setup your SSH connection to it. You will need to utilize that key you downloaded earlier. You can utilize whatever SSH client you’re most familiar with. This is a decent writeup on how to connect if you utilize Putty like I do. In general, you will log in as ec2-user and your password will be the passphrase on your key.

Now that you’ve got your system up and running, and you can connect to it via SSH, there’s a couple of things you need to do to get Apache up and running and ready to serve some PHP. Not going to discuss these in depth really, as I would probably be the 13838th person to write this up. Here are the steps I took:

  1. sudo yum update
    The system notified me that there were a number of updates available upon my first login.
  2. sudo yum install httpd mod_ssl
    Install Apache.
  3. sudo /etc/init.d/httpd start
    Start Apache.
  4. sudo /sbin/chkconfig httpd on
    Setup Apache to start automagically upon reboot.
  5. sudo /sbin/chkconfig –list httpd
    Confirm it was setup correctly
  6. sudo yum install php-common php-gd php-mcrypt php-pear php-pecl-memcache php-mhash php-mysql php-xml
    Install PHP
  7. sudo /etc/init.d/httpd reload
    Reload Apache.
  8. sudo chown ec2-user /var/www/html
    Give the ec2-user the access it needs. Not required, but this will make your life easier later.

If everything went well, you should be able to go to your instance’s Public DNS entry with your web browser and see this:

Apache Test Page
Apache Test Page

Further Configuration – Elastic IP and DNS

Accessing that via an instance’s Public DNS entry isn’t the most exciting thing, and it’s not very handy. To make things easier, you may want to assign your new instance an Elastic IP. In the EC2 AWS Management Console, select Elastic IPs from under Network & Security. Click Allocate New Address and it will give you a new IP address. Now select Associate Address and select the Instance you just created. It may take up to a minute to finish the process, but once it does you should be able to enter it into your browser and see the same test page as you saw before.

This next step will vary a little bit, based on your DNS setup. The end result is that you want to create a new Address (A) record and point it at the new Elastic IP. I created a subdomain and pointed it at the new Elastic IP address, so now I can visit subdomain.example.com and it will pull up that same test page as well. Notice that after you assign the Elastic IP, your Public DNS entry will change.

The final issue to overcome is getting your files over to the server. I’m a big WinSCP user in general, so I just use that:

WinSCP
WinSCP

It’s pretty simple. Enter the hostname for your instance, ec2-user for the user, and then select the private key you used earlier. Save that session, and then login. When you login it will prompt you for the passphrase that you created earlier.

Apache will be setup to serve files from /var/www/html, but you can change that if you want by modifying the /etc/httpd/conf/httpd.conf file and restarting Apache. If you are having issues with PHP files being served properly, make sure your httpd.conf file has this line:

AddType application/x-httpd-php .php

If it doesn’t, add the line and then restart (sudo /etc/init.d/httpd restart). If you haven’t chown’d the /var/www/html directory, you’ll need to do so before you can copy files into it.

Now, you may be saying “This is no easier than just installing everything on one of my systems!”. And you’re right, the first time it is a little bit of work. But one of the great features of EC2 is that you can configure a system just the way you like it, and then save an image of that system to be reused again and again. I’ll cover that a little more in the near future, but the next time through this process should only take a couple of minutes.

This entry was posted in AWS, PHP, Web Development and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 8 = sixteen

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>