Setting up Amazon EC2 instances

Screenshot of SSH client

I use Amazon Web Services for file storage (S3) with access through my own domains (CloudFront). S3 is great for backing up files and data, and serving them with redundancy and scalability, but what if I could take it a step further and serve entire websites from it? This is not possible with S3, but it is with EC2.

The main problem with S3 for serving websites is that it doesn’t have access to a PHP interpreter (for my situation), nor is it meant to be a full-fledged server environment.

The benefits of EC2 are that you’re literally given a machine instance to work with, just like the computer in front of you. You have full root access and can install whatever you want. You can also stop and start each instance as often as needed (thereby avoiding monthly or long-term costs). It’s literally a “pay for what you use,” on-demand service.

My history with EC2

I think EC2 has been around the longest (of all Amazon Web Services), but I never ventured into it because:

  1. I already pay third-party companies to host my websites, albeit in a more fixed manner.
  2. I was confused by the large barrier of entry for EC2 – it seemed complicated to get set up.

I’ve taken steps regarding #2 above, thanks to some helpful articles. I started here. This post was also very helpful.

Creating my first EC2 instance

Very quickly I was able to create a new EC2 instance and access the URL through a web browser:

Screenshot of EC2 default webpage

Exciting! Now how do I upload and manage files on the server?

I could already connect via SSH to the server (mentioned in the article above), but I really needed to use a graphical interface like WinSCP because I use that every day to connect to other servers via SSH/SFTP. Plus I don’t know all of the SSH commands for editing files, and it just seems more time-consuming to do everything through the command line.

Screenshot of Putty SSH client

WinSCP is a SSH, SFTP, FTP, SCP client, so it’s easy to connect to your EC2 instance just like you would through a strictly command-line program like PuTTY. So I could connect right away through WinSCP, but I couldn’t upload/edit files. I was getting “Permission denied” errors.

Screenshot of WinSCP client

“Permission denied” errors

After heavy searching, I discovered I had to authenticate as the “root” user after establishing an SSH connection to my EC2 server via WinSCP.

(I’m not a server administrator, so a lot of this is new to me.)

To authenticate as the “root” user, I just issued this command via Putty:

sudo su -

Now I was accessing the server as the root user via Putty. (To be clear, I had a connection open via Putty and WinSCP – Putty for pure SSH commands, and WinSCP for graphical access.)

I navigated to the public web directory using WinSCP (/var/www/html).

But I still couldn’t upload/edit files through WinSCP. This is because WinSCP was still treating my connection as the non-root user (in this case, the user was “ec2-user”).

To allow user “ec2-user” write access to the public web directory, I ran this command via Putty (as the root user):

chown -R ec2-user /var/www/html

I also made sure permissions on that entire folder were correct:

chmod -R 755 /var/www/html

Now I could upload/edit files to my EC2 instance using WinSCP!

Uploading a PHP file

The first thing I did was create an index.php file and put some basic PHP code in it:

echo "test";

Screenshot of WinSCP client

When loading my EC2 URL in a web browser, it output “test” as it should! So that means the PHP is being interpreted as expected.

Screenshot of browser loading my EC2 page

Unique IP address

The next thing I did was assign my EC2 instance a unique IP address. The default URL looks something like this:

http://ec2-101-30-8-29.compute-1.amazonaws.com/

This is somewhat long and ugly if I were to share the site with anyone. Instead I can associate an IP address to my EC2 instance:

Screenshot of AWS Management Console

Now I can access my EC2 webpage using the IP address alone.

Where to go from here…

As I continue to play with EC2, I imagine I’ll find a lot of useful things. I still haven’t set up a MySQL database, or installed other software on my EC2 server. This post was just to mention my initial experience with EC2.

Would I replace my existing third-party hosting with EC2? Probably not. For most of my personal sites, I don’t need such incredible redundancy and scalability, and having to manually configure everything can be a pain for a novice user like myself.

But for quickly getting a web server up-and-running (to test things or share information), and shut it down as soon as you’re done, it’s a very nice service to have.

4 thoughts on “Setting up Amazon EC2 instances

  1. Awesome, in every single step I was struggling!!! After 3 hours of headache, this was very helpful.

  2. Wow dude you have no idea how much this helped me.. The stupid WinSCP upload error to the public web directory was killing me for hours.. I reinstalled the program, searched for hours on forums, friend couldn’t figure it out.. than found your blog and you’re advice helped me solve the problem in a matter of seconds.. I feel so stupid now lol.

    I’m not a system administrator, and this was my FIRST EC2 launch, so considering with help I think I did pretty good.

    Now if only the upload speed could go faster when transferring over WinSCP .. my average upload speed here was 3kbps so it took over an hour to send 7 mb lmao.. when my typical upload speed is over 900 kbps :/

    Thanks again!,
    Ryan

  3. This is so much more sensible and easy to follow than anything Amazon has on their site. Thank you so very much!

  4. Great info! I was struggling with the same issue. Do you mind sharing the command to allow access to the entire root directory on WinSCP? Thanks again

Comments are closed.