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.

Erik’s guide to planning social gatherings in Singapore

You love to meet up with your friends and chat over dinner or drink.

You think organizing a meetup is as easy as creating an event in Facebook, adding people to it and just wait for the day to come.

And one day before the event, you check the event page on Facebook only to find that no one has responded or put their attendance as maybe.

What went wrong?

Nothing. It’s simply in Singaporean’s blood to “bo-chup” (don’t care attitude).

1) Decide a default location and time.

If you ask everyone about their opinions, you would most likely get none. And if you get opinions, great! Change it accordingly.

Those who do not respond are usually fine with any place.

2) Plan in advance. At least 2 weeks or even better, 4.

Most people usually have plans made for the week and the next.

Planning in advance shows that you really want to meet up with those invited. It’s like how people would book advance tickets for a show/performance they like as soon as the sales for those tickets begin.

And if you get the excuse that you’re planning way too soon, ignore it. It only goes to show that that person wants to reserve his/her time on that day for something else, and values that event more than meeting up with you.

3) When confirming attendance, call them or SMS them.

I’ve never seen a successful meetup planned using Facebook unless it’s a small group of very very close friends, or you’re someone so important that people want to be around you 24/7 and even when you go to the toilet.

Start confirming at least a week before so you know the group size and make reservations at the restaurant accordingly.

For those who sound unsure, tell them that you are making reservations at a certain date and you need to hear an answer from them by then.

4) Expect late-comers and prepare for it.

Despite Singapore being so small it takes less than an hour to travel from one end of the country to the other by car, people will still be late. But they’re not entirely to be blamed. Our “world-class transport system” has to have world-class failures too sometimes.

Plan your meetup in such a way that those who arrive on time won’t have to wait for those who are late.

If it’s a dinner meetup, go into the restaurant first with whoever is already there and start ordering and chat.

Or meet up at a game center first where people can play some games while waiting.

The last few movie gatherings I did for my anime club, we gathered at a fastfood restaurant so we can have dinner and chat before the movie starts.

Troublesome isn’t it? But after everything’s over, everyone will appreciate you for putting in all that effort to get them together. That alone is rewarding enough.