Hosting websites on an AWS EC2 micro instance

A few months ago, I signed up for an account with Amazon Web Services to take advantage of the first year free usage on EC2.

It was a great learning experience as I get to configure everything in detail.

With shared hosting, someone else does the setup and maintenance for you. You can configure a few things in detail but you can’t have full control. It’s like public transport, you cannot change the parts in the vehicle, but you can choose where you want to sit, but you have to share the ride with a lot of strangers.

And it sucks if you have to share it with a serial fartist. i.e. Some silly person who took down the whole server with poorly written codes that take up lots of CPU, or whose website is the target of a DDOS attack and all other websites on the same server got brought down with it.

There’s also co-location and dedicated hosting but those are not affordable to the average web developer.

Then comes the age of cloud computing.

You get full control of a server at the cost slightly higher than shared hosting. You have dedicated CPU and RAM resources so no other server on the same physical machine as you can affect you. (Though I’m not sure what happens if those servers get DDOSed).

Long story short, my shared hosting account with DreamHost was expiring and so I migrated my remaining sites to my AWS micro instance server and here’s the things that happened and what you need to do if you want to run multiple websites on a micro instance as well.

At first, I did not configure my Apache and MySQL installations to work on low RAM machines (micro instances get only 613MB of RAM). They came with the default configuration settings that are meant for dedicated machines that have higher amounts of RAM.

A micro instance does not have a swap file configured. You have to do it yourself and here’s a simple guide:

Switch to root and follow these steps to add the swap space –

Type the following command with count being equal to the desired block size:
dd if=/dev/zero of=/swapfile bs=1M count=1024

Setup the swap file with the command:
mkswap /swapfile

To enable the swap file immediately but not automatically at boot time:
swapon /swapfile

To enable it at the boot time, add the following entry into /etc/fstab:
/swapfile swap swap defaults 0 0

 

Source: Adding swap space to Amazon EC2 Linux Micro Instance to Increase the Performance

Next, edit your apache config file and look for the Server-Pool Size Regulation section.

Most likely you are using the prefork MPM. Reduce the MaxClients to about 10. This varies depending on how much RAM each process takes. From my observations, my httpd processes take up about 40M of RAM at most. So I use 10, which allows me to have 10 running httpd proccesses all taking up 400M of RAM.

It would also be a good idea to reduce the MaxRequestsPerChild to something around 1000 in case of memory leaks. Situations where a process takes up more than 40M of RAM and keeps growing.

Better explanation here.

Without the above. My apache kept spawning child processes so much that it used up all available RAM and my MySQL process got killed off frequently. That means all my sites that are database driven are immediately crippled.

On a related note, back when I was a child, I loved how I could change parts on those Tamiya cars which gave me the illusion that it would run faster. Looks like my fetish for performance tuning is still there.

RL

I really like your framework. I use it at work and at personal projects.

I’ve recommended it to others even though there’s no benefit for me.

But I have a problem and a few others have the same problem too.

But you won’t make the patch for us because you have other more important things to attend to.

You said if we want that feature, we should code it and submit a pull request for it.

So I coded the patch, learned how to work with GitHub, submitted a pull request.

Other people who shared the same problem were happy.

Days go by and yet there’s no response from you.

I wondered if there’s a problem with my solution so I tried to propose an alternative.

But you said the original solution was fine, just be patient because you guys have real lives too.

Weeks later, still no response.

And now, I sit here wondering why did I even bother in the first place.

Just a small rant.

Steve Jobs

Today is the day Steve Jobs passed away.

When I was a kid, hi-tech gadgets in movies amaze me.
The fact that it’s fictional, disappoints me.

Thanks to his vision and Apple, I get to enjoy a “futuristic” life while I’m still alive. The things they do should be the standard to be called innovation.

Light and powerful computers with a pretty user-interface.

A touch phone that reminds me of my schedule, communicate, stay up to date with the news, write notes, listen to music or watch a video. All this while on the move.

These 2 gadgets have become an important part of my life. Helping me to stay organized, learn new things, and earn some income.

A part of me feels that Apple will no longer be as innovative as when Steve Jobs was still around. Another, hopes that I’m wrong.

And I do hope I’m wrong.

MacBook Air, 2 months later

It’s been 2 months since I’ve started using MacBook Air for development work.

I have not found a reason to dislike it so far.

If I really have to squeeze some out, it would be Firefox. But that is probably not Apple’s fault. (Firefox 6 hangs when I try to browse my history and other random moments). Or when you go into time machine mode, it slows down maybe because of the space effects in the background.

With SSD, everything works very fast. Startup, shutdown. Starting up software, etc.

With a 7-hour long battery life, it’s long enough for me to keep working until I’m satisfied. On my previous laptop, I had just a bit more coding to do but forced to stop because of low battery after 2 hours.

Thanks to the long battery life, I stopped shutting down. Just close the lid and next day, resume from where I stopped.

The keys are comfortable to type on. The touchpad is more sensitive than most other laptops I’ve worked with. The gestures are handy.

Love the thin size, light weight and aluminium body. Makes it amazing how they can pack 7 hours of battery life into such thin space.

Best of all, you don’t have to worry about device drivers. Apple has them all covered.

On Windows, I would have to go to every website of every device on my PC/laptop and download the latest drivers, BIOS/firmware for them if I want the best performance or bug fixes. On a Mac, the “Software Update” program will handle everything.

My previous laptop seems to have a bluetooth problem. After I updated the BIOS, it stopped booting up and Dell had to send someone down to replace the motherboard. Another problem is the USB port. It becomes ultra slow when you try to transfer a large file to a portable harddisk. I’m sure it’s not a power issue.

But it’s just 2 months. Let’s see if I can find any more problems with it.

A good use of your phone’s camera

In my many years of dabbling with computers, one good idea I’ve come up with is to take photos of your hardware.

Because you might be browsing for a new sound card at the stores but can’t remember if you have any available PCI slot to use.

You might come across an article one day that claims that a batch of SSDs are faulty, and you end up having to open up your PC to find out the serial number of your SSD to see if you’re affected.

Keep them stored on your phone and dropbox so it’s easily accessible.

A folder of links to your hardware’s details on their manufacturer’s website is useful to keep too. So you can easily check for any new BIOS updates or look up specs.

Digital copies of receipts and warranties too!

In case something spoils and you want to know as soon as possible if it’s still covered under warranty.

And to save yourself from turning your house upside down to find the receipt of your purchase made years ago, always keep them securely together in a folder at a fixed location.