Tuesday, January 16, 2007

Web serving on the cheap

Have you ever wanted to set up a small web server at home that can handle a small but reasonable amount of traffic? Perhaps you have a pet project or a home business and can't justify the cost of professional hosting. Maybe you have a fetish for low powered servers on small internet connections. Or possibly you just want to see if a Slashdotting of your home DSL line will trigger a call from your ISP. In any case, here are some tips on how to get the most out of a small setup.

Understand your limitations

If you don't have a large budget, then you obviously are going to have two big problems:
  • A lack of CPU power

  • A lack of bandwidth
Any effort to improve the performance of your small web site should be directly aimed at alleviating one of these two problems. I'll discuss each of these two issues below. There are other issues, such as a lack of RAM or disk space or disk performance, but usually your CPU and bandwidth will dominate the situation. I'll also ignore the most obvious solution to these problems, which is simply to buy a better processor and a bigger tube.

Bandwidth

More often than not, bandwidth will be your biggest hurdle, especially if you're running your server on your home internet connection; most home connections have terrible upstream speeds, which really hurts you when you're a content producer and not a consumer. In addition, some connections, like DSL, will have high latencies.

To mitigate your lack of speed, you're simply going to need to push fewer bytes down the pipe. Look at your web pages and appreciate all of those pretty pictures while you can, because they're the first thing to go. A typical image can be anywhere from 10 to 200 kilobytes, which is simply too large for a small connection, especially if you have 20 of them on the front page. If you can stand it, remove every single GIF, JPEG, and PNG from your site. You may need to redesign your site around the new image-less paradigm, but you won't regret it in a few weeks when you get your bandwidth bill.

Next, move to a CSS-based design instead of a pure HTML one. You should be able to slim down your HTML this way, which will make it that much faster for a user to download. For an added bonus, you should put all of the CSS commands into a separate file. This will slow things down a tiny bit for the user's first visit to the site, but it also means that the same CSS file will be cached on every subsequent page view.

Finally, you should go for the biggest savings of all: compressed web pages. The idea is simple: the web server compress any text files before sending them to a user and the user's web browser will automatically decompress them before reading them. Every modern web browser supports compressed web pages, and you can see immense space savings from using it. Page compression can be a tricky thing to get right, especially if you're short on CPU power, because it obviously takes some effort by the web server to compress things. There are a few ways to get around this, one of which is to pre-render compressed versions of frequently accessed pages and then dish those out to users. You need to experiment with compression to see how much it affects your server's CPU.

CPU

If you're on a budget, then you most likely have an old computer with an outdated processor. This isn't necessarily a problem -- even very old computers can saturate a small internet connection -- but you're going to need to code your site correctly if you want to prevent it from being your big bottleneck.

The very first thing to go is the database. Sure, a database like MySQL is nice to have and does provide some convenience, but it can totally kill your web server's performance. Obviously, not everyone can do this, but many people can; it's usually a waste to store every page of a small web site in a full-fledged database system. Many small web sites could eliminate MySQL completely if they just stored data directly in files instead. For those that absolutely must have a database, you will need to at least remove any direct database query on your front page. Sometimes you can even just keep a copy of a MySQL query in a file and have a program update that file every so often.

Another way to avoid making database calls or any other expensive operation is to pre-render entire pages. If you know that your home page is only updated a few times a day, then why dynamically generate it every time someone views it? Just take the rendered page's HTML and save it in a file; the next time a user requests that page, just throw the rendered copy at them. You need to be careful to not give users stale pages, but it usually isn't too hard to figure it out on a small site. This concept ties in well with the previously mentioned tactic of saving pre-compressed copies of pages.

Finally, try to avoid web scripting in general. It is usually hard to avoid, given how much power it provides, but a poorly coded PHP or Perl page can chew through your CPU and RAM; it is best to use it only when it is truly needed. If you manage to free yourself of scripting on all but a few pages, you can even take advantage of a new class of lighter and faster web servers like lighttpd or tux; you'll still need to run a heavier web server process for those few scripted pages, but most of your traffic will hopefully run through the fast and light web server process.

3 Comments:

At 9:20 AM, Blogger Marcos said...

Short vesion: write clean pages, cache the results and use a lightweight server if you can.

Caching has also the benefit of reducing the time your viewers will wait for the page, even if you have plenty of processing power. It is almost always a nice thing to do.

But I disagree about the database. Avoid a database if you have litlle RAM, but it will increase processor use, not decrease it. Any sane database (without hundreds of tables, with sane primary keys and well chosen indexes) uses a lot less processing power than most flat files accesses.

Of course, flat files can be faster than databases if you write very good indexes, and a nice system for accessing it. But this is normaly a lot of work (and brainpower) to put into a small (or medium, or anything that isn't huge) site, and you can't screw it anywhere, since it will destroy the entire system.

 
At 5:22 AM, Blogger p0wer said...

Another setup. Not so long ago I've popped on a small web server called nginx. It's another alternative to lighttpd and such, but IMO has more features at no additional cost. Mixed with PHP as CGI and eAccelerate, this could be really nice

 
At 2:39 AM, Anonymous Anonymous said...

Web hosting provider if often provided as a general access to the World Wide Web. There are many free and paid providers offering web hosting.
There are many web hosting companies, which will try to sell you their services and products. There are even companies which are prepared to offer you free web hosting. Why would they want to do that? It is because, in most cases, the web host wants to put his own advertisement all over your website. It is a free advertising billboard for the companies. Having their ads plastered all over your web site might disturb the purpose of your business seriously. Do you want this?

 

Post a Comment

<< Home