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:
- I already pay third-party companies to host my websites, albeit in a more fixed manner.
- I was confused by the large barrier of entry for EC2 – it seemed complicated to get set up.
Creating my first EC2 instance
Very quickly I was able to create a new EC2 instance and access the URL through a web browser:
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.
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.
“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 (
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:
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.
Unique IP address
The next thing I did was assign my EC2 instance a unique IP address. The default URL looks something like this:
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:
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.