Skip to content

Amazon EC2

How to Host Simple Content with Amazon Elastic Cloud Computing (EC2)

Two weeks ago, I demonstrated how to sign up for a free Amazon Elastic Cloud Computing (EC2) account, and how to log into your virtual server. Today, let’s look at how to do something useful – to wit, hosting static content.

First, however, I need to address a problem with the previous article. In demonstrating how to log in to a virtual server, I created an instance of the SuSE Enterprise Linux Amazon Machine Image (AMI). As several commenters noted – and as I found out when I checked my EC2 bill – this AMI is not free, even if you only use a micro instance of the image. I have revised the original article to reflect this, and also discussed this issue in last week’s article, where I reviewed the costs of cloud computing.

Setting Up a Free Linux Instance

First, let’s correct our (okay, my) mistake in the last article, and set up a free micro instance of a standard Linux AMI. Launching it is easy enough: just click Launch Instance on your EC2 Dashboard, and select the first entry.

We can then change our PuTTY configuration (on Windows) to log in to our EC2 server just like we logged in to our SuSE Enterprise Server, with one key difference. On a normal Linux installation, EC2 insists that we not log in as root, but as the user ec2-user. Because of this, we’ll use the Linux command sudo whenever we need to perform an operation with administrator permissions.

Installing Apache

So now we have a basic Linux server. But it’s missing a few things we need to host Web content – such as, well, a Web server. Fortunately, Amazon Linux supports the yum package manager, which installs RPM packages from a repository. The following command installs both Apache and the latest release of PHP on our new Linux server:

sudo yum -y install httpd php

Once Apache is installed, we can configure it to start on server boot:

sudo chkconfig httpd on

Before starting our server, we should adjust Apache’s httpd.conf in conformance with our dreams and desires. For my installation, I decided to host Web content out of the ec2-user home directory, in the subdirectory public_html. I used chmod 755 /home/ec2-user to make this directory accessible to the HTTP daemon, then tweaked the DocumentRoot command in /etc/httpd/conf/httpd.conf:

DocumentRoot /home/ec2-user/public_html

Two notes before we proceed:

(1) By default, the only text editor available is vi. For those who loathe vi with a fiery passion, you can install either pico or emacs using the yum command.

(2) Using the well known user ec2-user for hosting your content isn’t the best strategy. In a future tutorial, I’ll demonstrate how to create a new user, and generate a new public/private key pair for authenticating that user’s SSH sessions.

Opening Port 80 on EC2

We’re almost there. In order to access our Web server publicly, we need to open up port 80 to HTTP traffic. By default, EC2 blocks all services. To free up port 80, we go to the EC2 Dashboard and select Security Groups from the left-hand navigation tab. If we had more than one virtual server, we’d want to create multiple security groups to reflect the different services and capabilities of our machines. Since we’re only managing the one to start, we can modify the default security group by selecting HTTP from the drop-down list at the very bottom of the screen and clicking Save. The EC2 Dashboard will fill in the other values on our behalf.

Give It a Test Run

Let’s go back to our public_html directory and create a test HTML file:

<html> 

<body> 

<p>Amazon EC2 server is up and running!</p> 

</body> 

</html>

And that’s that! We can now navigate to the public DNS name for our server, which is available from the EC2 Dashboard by clicking Instances, selecting our instance, and scrolling down to the value Public DNS at the bottom of the screen. With just a little effort, we’ve raised a virtual Web server!

The “God” Benefit of EC2

Now that we’ve come this far, I wanted to say a quick word about the benefits of using EC2. My last article brought a bitter dose of reality to the discussion around cloud computing. As I noted, even with Amazon’s new “free” account, you’re liable to rack up a monthly cost when developing an application using EC2.

What I failed to mention is that you’re getting something worthwhile for your money. Beyond scalability, there is one great benefit to using EC2 – namely, you are God. While EC2 is expensive compared to virtual hosting accounts, what you gain with that cost is tremendous flexibility in your server configuration. If you’re at ease installing and administrating a Linux server, and your application environment needs permissions and resources that a virtual host isn’t willing to grant for security reasons, EC2 may be your best bet in spite of the costs.

What’s Next?

In my next installment, we’ll really get down to business. I’ll discuss how to set up PHP, run a basic PHP Web server application, and configure the DNS record for a domain so that it points to our EC2 virtual server.

Jay Andrew Allen is a Technical Writer and software developer based out of Seattle, WA. Find out more about him at his Web site, Jay Allen Writes.

Be Sociable, Share!
    The following two tabs change content below.

    11 Comments (Add Yours)

    1. I suggest to avoid Apache and go for Lighttp or Nginx.

    2. Hi Jay,

      this post is the best I’ve found on the web to start my Amazon AMI instance.
      Great stuff. Just useful inputs ! Thank you very much for sharing.
      Look forward to the next articles like not using ec2-user /public_html to host the files !

      Paulo.

    3. Thanks for the very help article!
      One comment/question: I found it necessary to manually start the web server via “sudo apachectl -k start”, before trying to access the web page… not sure if I missed anything?

      Thanks again – Steve

    4. Jay, unlike SuSE Linux, but the Basic 32-bit Amazon Linux instance does not seem to allow you to login as root. Without that level of access, you can’t edit /etc/httpd/conf/httpd.conf. Am I missing something?

      • I know you figured it out, but for anyone who is confused:

        To start the webserver: sudo service httpd stop

        After editing httpd.conf: sudo service httpd restart

        And, instead of putting your documents under /user It is rather easier to put them in /var/www/html, not least because the permissions are already setup correctly.

    5. Never Mind. Duh… sudo is my friend.

    6. Can you still log into your instance after changing the permission of the home dir? Because I can’t…

    7. can you explain more on the document root i am not able to start the page

    Add Your Comment (Get a Gravatar)