🤯 50% Off! 700+ courses, assessments, and books

From Zero to Cloud: Setting Up an EC2 Sandbox, Part 3

    John Degner
    Share

    This is the final article in a three part series focused on setting up EC2 as a sandbox for application developers. I assume you have an AWS account with Amazon; if you don’t, please read Part 1 to learn how easy it is to sign up. I also assume you have configured your development environment and installed an AMI; if you haven’t, please read Part 2. In this installment, we’ll learn how to install Apache, MySQL and PHP in our running AMI, and then clone the AMI to make our own.

    Setting up the LAMP Stack

    Now we’re ready to get our LAMP stack set up. As it is outside the scope of this article and because there are many perspectives on best practice, we won’t get into setting up a multi-tiered ecosystem. Instead we will install MySQL, Apache, and PHP in our instance, knowing that when we write our application we’ll be sure to structure the code such that we can change our data source easily.

    First, make sure we can do anything we want on this box by launching an interactive shell with administrator privileges.

    ubuntu@ip-10-112-58-53:~$ sudo -i

    Next, install MySQL via apt-get. We will be prompted to choose and verify a root password for MySQL along the way.

    root@ip-10-112-58-53:~# apt-get install mysql-server mysql-client

    Then install Apache via apt-get.

    root@ip-10-112-58-53:~# apt-get install apache2

    Of course we also want to install PHP and the corresponding Apache module.

    root@ip-10-112-58-53:~# apt-get install php5 libapache2-mod-php5

    Finally, we need to install at least the php5-mysql package. I like to install a few more… use apt-cache search php5 to find the packages you want.

    root@ip-10-112-58-53:~# apt-cache search php5
    root@ip-10-112-58-53:~# apt-get install php5-mysql php-pear php5-cgi php5-cli php5-common php5-curl php5-gd php5-gmp php5-ldap php5-odbc php5-pgsql php5-snmp php5-sqlite php5-xmlrpc php5-xsl php5-imagick

    Restart Apache after all the new packages are installed.

    root@ip-10-112-58-53:~# service apache2 restart
     * Restarting web server apache2
     ... waiting    ...done.

    After we’re done, we should do a sanity check to make sure everything is configured correctly. Create a file called phpinfo.php in the web root with the following contents:

    <?php
    phpinfo();

    Now is the moment of truth. Take the Public DNS entry we used before, paste it into a web browser and visit the file you just created. Hopefully we’ll to see the output of phpinfo().

    A phpinfo Page

    Dividends Paid

    We’re not done just yet! We’ve worked hard to build a solid baseline LAMP stack, so let’s create our own AMI so we can start an identical instance to the one we’ve just configured. To do so, we use ec2-create-image.

    MacBook-Pro:.ec2 john2$ ec2-create-image i-c795bfa6 -n lamp-starter
    IMAGE	ami-43b5752a

    Click on AMIs in the IMAGES section of the EC2 Management Console and we should see our image.

    AWS Management Console

    Now that we’ve successfully gone from zero to cloud, let’s have a little fun and pretend we’ve built a multi-tiered web application that has gone live and is tremendously successful. Assuming we’ve architected the application appropriately, and ignoring the necessary load balancing setup, we could launch 10 new web servers with a few key presses.

    MacBook-Pro:.ec2 john2$ ec2-run-instances -k john2.kp --instance-type m1.large -n 10 ami-43b5752a
    RESERVATION	r-4f433b20	582155754520	default
    INSTANCE	i-2daa9f4c	ami-43b5752a			pending	john2.kp	0		m1.large	2011-08-22T04:49:45+0000	us-east-1a	aki-427d952b			monitoring-disabled					ebs				paravirtual	xen		sg-f5a0899c	default
    INSTANCE	i-2faa9f4e	ami-43b5752a			pending	john2.kp	1		m1.large	2011-08-22T04:49:45+0000	us-east-1a	aki-427d952b			monitoring-disabled					ebs				paravirtual	xen		sg-f5a0899c	default
    INSTANCE	i-31aa9f50	ami-43b5752a			pending	john2.kp	2		m1.large	2011-08-22T04:49:45+0000	us-east-1a	aki-427d952b			monitoring-disabled					ebs				paravirtual	xen		sg-f5a0899c	default
    INSTANCE	i-33aa9f52	ami-43b5752a			pending	john2.kp	3		m1.large	2011-08-22T04:49:45+0000	us-east-1a	aki-427d952b			monitoring-disabled					ebs				paravirtual	xen		sg-f5a0899c	default
    INSTANCE	i-35aa9f54	ami-43b5752a			pending	john2.kp	4		m1.large	2011-08-22T04:49:45+0000	us-east-1a	aki-427d952b			monitoring-disabled					ebs				paravirtual	xen		sg-f5a0899c	default
    INSTANCE	i-37aa9f56	ami-43b5752a			pending	john2.kp	5		m1.large	2011-08-22T04:49:45+0000	us-east-1a	aki-427d952b			monitoring-disabled					ebs				paravirtual	xen		sg-f5a0899c	default
    INSTANCE	i-3baa9f5a	ami-43b5752a			pending	john2.kp	6		m1.large	2011-08-22T04:49:45+0000	us-east-1a	aki-427d952b			monitoring-disabled					ebs				paravirtual	xen		sg-f5a0899c	default
    INSTANCE	i-3daa9f5c	ami-43b5752a			pending	john2.kp	7		m1.large	2011-08-22T04:49:45+0000	us-east-1a	aki-427d952b			monitoring-disabled					ebs				paravirtual	xen		sg-f5a0899c	default
    INSTANCE	i-3faa9f5e	ami-43b5752a			pending	john2.kp	8		m1.large	2011-08-22T04:49:45+0000	us-east-1a	aki-427d952b			monitoring-disabled					ebs				paravirtual	xen		sg-f5a0899c	default
    INSTANCE	i-01aa9f60	ami-43b5752a			pending	john2.kp	9		m1.large	2011-08-22T04:49:45+0000	us-east-1a	aki-427d952b			monitoring-disabled					ebs				paravirtual	xen		sg-f5a0899c	default

    You’ll see this action reflected in your console, and you’ll understand why Amazon asked for your credit card information up front.

    AWS Management Console

    This concludes our three part series on setting up a development sandbox with Amazon EC2. We started with our head in the clouds and ended with an AMI in the cloud. Amazon is a surprisingly cheap sandbox so long as you are mindful of your usage. Turn instances off when they’re not in use and be sure to familiarize yourself with their pricing.

    Image via Lightspring / Shutterstock