Hate UML?

Draw sequence diagrams in seconds.
http://www.websequencediagrams.com

How to run a linux based home web server
Posted on: 2009-07-23 08:00:00

There are plenty of places you can go if you just want to put up some static web pages for free, or very low cost. But costs go up very quickly if you need to do any more than that, or if you get spikes in traffic. Sometimes you need complete control over the server, and don't want to pay $20 to $40 a month for a VPS. In this article, I'll describe step by step how to set up a home web server using Ubuntu, capable of handling modest spikes in traffic.

There are several things you need:

  • Connection
  • Hardware
  • Domain name
  • DNS server
  • Web server software properly configured for your system

The Connection

Many large internet providers forbid running any servers in their usage agreement. They might not actually check for a long time, but it's no fun to have your Internet cut off without warning after months with no problems. You can only do this if you are lucky enough to find an ISP that allows servers. In Canada, try Teksavvy.

You will want as high an uplink speed as you can get. Unless your website is extremely popular, bandwidth caps will not be an issue, but be aware of what they are.

The Hardware

Over your home internet connection, traffic will be naturally smoothed by the low uplink bandwidth. Incoming requests will be queued and served in order, not all at once. CPU speed is not going to be an issue, and a one GHz machine is plenty of power. Although this blog's web server has only 512 MB of memory, I recommend at least 1 GB of RAM.

Your router will also be an important part of the system, but quality varies. I have a Linksys WRT54GL. I downloaded and installed dd-wrt on it. The new firmware unlocks a lot of hidden features, including dynamic DNS updating. However, this is not strictly necessary, because your linux system can handle this step just as easily.

Domain name

You can get a domain name for 10 to 15 dollars. Try DomainsAtCost or GoDaddy. After you buy one from a registrar, you will get an account with them. After logging in, you can renew the domain name. But the most important priviledge is to be able to set the name server. That will have to wait until the next step...

A few months after getting your domain name you will start getting emails from companies claiming they have registered it in another country, and offering to sell it back to you. You can safely ignore these scams.

DNS server

Because your IP address will often change, you will need a dynamic DNS updating service. DynDns.com provides very good service, with updates taking effect within seconds. However, they do charge a fee. ZoneEdit provides a free service for up to five domains, although updates can take several minutes to propagate.

After signing up with a dynamic DNS updating service, you will get:

  • a username and password
  • IP addresses of nameservers
Save the username and password for later. Go back to your domain name registrar and enter the nameservers into their system.

Configuring your server

Install the latest Ubuntu Desktop edition, and then add the additional software you will need for web serving:
sudo apt-get install mysql-client mysql-server php5 apache2 php5-mysql ddclient

You will be prompted several times for your mysql root password. This is not the same as your linux root password. As long as you are behind a firewall, it is okay to press enter and leave it blank.

Now check that your web server works using a web browser browsing to http://localhost. Apache should show a default page telling you that it works.

Fixing ddclient

DDclient is a program that continually monitors your true IP address. When it changes, it updates your dynamic DNS service so that your domain name will resolve to your home web server.

Unfortunately, the version of ddclient that you can install on Ubuntu 9.04 (and most possibly later ones) is screwed up. Lets fix it.

wget http://downloads.sourceforge.net/sourceforge/ddclient/ddclient-3.8.0.tar.gz
tar xvzf ddclient-3.8.0.tar.gz
sudo cp ddclient-3.8.0/ddclient /usr/sbin/ddclient
sudo ln -s /etc/init.d/ddclient /etc/rc2.d/S99ddclient

Edit /etc/ddclient/ddclient.conf. If you use zoneedit, the file should look like this. Replace username and password and domain with your true values.

protocol=zoneedit1 
use=web
server=dynamic.zoneedit.com 
login=yourlogin 
password='yourpassword' 
yourdomain.com

Now erase the ddclient cache and restart it.

sudo rm /var/cache/ddclient/*
sudo /etc/init.d/ddclient stop
sudo /etc/init.d/ddclient start

After a few minutes you should be able to get to your web server by entering your domain name. If things aren't working, running 'sudo ddclient --verbose' will help you figure out what it's doing.

Configuring Apache2

There is one last thing. Because your machine has limited memory and bandwidth, you will need to set some parameters in /etc/apache2/apache2.conf.

The KeepAlive setting keeps your server busy for a few seconds after a page is served, allowing clients to download images without starting a new connection. The default setting is far too long, and it would cause your server to choke under heavy load. Set KeepAliveTimeout to 1 or 2 seconds.

The MaxClients setting determines how many connections your web server can handle at one time. Each connection takes around 5 megabytes of unshared RAM, so you will have to set this value taking into account the amount of RAM on your machine, while leaving room for other system processes. For 1 GB, a nice safe value is 130, but your mileage may vary. If your server becomes unresponsive, reboot it and lower this number.

After any change to server settings, use "sudo apache2ctl restart" to safely restart everything. Your users won't notice a thing.

Optimize for bandwidth

When your bandwidth is limited, the number of visitors you can handle is directly proportional to size of the page, and all the images. If you have 120 KB/s of uplink bandwidth, and your page is 120 KB in size, then you can handle no more than one visitor per second. The single most important thing you can do is make your images as small as possible. Use low quality JPG for photos, and 32 or 256 colour PNG files for everything else.

Type 'sudo a2enmod deflate' and restart apache to allow your HTML, scripts, and css files to be transmitted compressed.

If you use common Javascript libraries, use the Google hosted versions of them.

Include all your own CSS and Javascript in the same file as your HTML, so that only one page needs to be requested. During times of heavy load, 99.9% of your visitors only load a single page of your web site anyway, so it makes no sense to split things up into different files.

In conclusion

If you need 99.999% reliability, you should look for a hosted VPS solution. However, for modest needs, you can achieve 98.9% reliability for no cost other than what you are paying your internet provider.

Further reading...

Want more programming tech talk?
Add to Circles on Google Plus
Subscribe to posts

Post comment

Real Name:
Your Email (Not displayed):

Text only. No HTML. If you write "http:" your message will be ignored.
Choose an edit password if you want to be able to edit or delete your comment later.
Editing Password (Optional):

jldugger

2009-07-23 14:23:27
Why use MySQL if you don't have explicit dependencies, when PostgreSQL is available?

Steve Hanov

2009-07-24 20:15:41
Why use PostgreSQL, when MySQL is available?

abdulhaq

2009-08-28 13:27:29
One very real cost that you're overlooking is cost of the power consumed by your machine. This can add up to a considerable amount over the month, surpassing the cost of a cheap VPS provided (e.g $15 per month)

Ben Gutierrez

2009-08-28 14:05:42
I've done this with Comcast in San Francisco. My IP doesn't change very often, so I've been able to get away with running my own DNS as well.

julien

2009-08-28 15:18:22
you also need to have port 80 allowed by your ISP in order to do that (unless your visitors know that they can reach url.com:8888). Not all ISPs allow you to serve data on port 80.

Yeroc

2009-08-28 15:42:27
If you're concerned about bandwidth it's probably worth mentioning mod_deflate as well...

Yeroc

2009-08-28 15:43:13
Oops... missed that paragraph of the article somehow! Please disregard previous brain fart.

Fred Source

2009-08-28 16:47:13
OK -- there is any easier way.

Take a look at the Linux based home server offering from www.amahi.org

All the basic work is done for you (and you get a whole lot more (including VPN access to your home network!)

ehsanul

2009-08-29 06:14:38
Thanks for the write-up.

I'd outsource image-serving, and anything else that takes bandwidth to Amazon S3 or something.

Nick Tulett

2011-04-05 09:33:52
A plug computer will provide a decent gigahertz server at under 5 watts and node.js rather than Apache will make the 512MB memory less of an issue.

aviNasHhh

2012-02-07 08:54:51
i have a question.....

when web servers are not capable of handling dynamic web pages and we dont have the wish to take any active web pages then what should we do?

Nick

2013-03-26 23:51:21
Just a question, I am trying this myself using an old PC and either Ubuntu or Centos, (any recommendations?) My question is, if I use a DNS service and register one of my old domains, can I know use this domain as a namserver and add more domains to point to my server?

Marcus

2013-05-20 21:51:03
This is an older post, but I'd just like to point out that unless you have a business-contract with your ISP, it's against their TOS to run a web server on your network.

me70w0s7nn

2014-04-20 09:29:25
[b][/b]

[b][/b]

[b][/b]

[b][/b]

[b][/b]

xg28a5z3hz

2014-04-23 10:05:26
[b][/b]

[b][/b]

[b][/b]

[b][/b]

[b][/b]

Email
steve.hanov@gmail.com

Other posts by Steve

Yes, You Absolutely Might Possibly Need an EIN to Sell Software to the US How Asana Breaks the Rules About Per-Seat Pricing 5 Ways PowToon Made Me Want to Buy Their Software How I run my business selling software to Americans 0, 1, Many, a Zillion Give your Commodore 64 new life with an SD card reader 20 lines of code that will beat A/B testing every time [comic] Appreciation of xkcd comics vs. technical ability VP trees: A data structure for finding stuff fast Why you should go to the Business of Software Conference Next Year Four ways of handling asynchronous operations in node.js Type-checked CoffeeScript with jzbuild Zero load time file formats Finding the top K items in a list efficiently An instant rhyming dictionary for any web site Succinct Data Structures: Cramming 80,000 words into a Javascript file. Throw away the keys: Easy, Minimal Perfect Hashing Why don't web browsers do this? Fun with Colour Difference Compressing dictionaries with a DAWG Fast and Easy Levenshtein distance using a Trie The Curious Complexity of Being Turned On Cross-domain communication the HTML5 way Five essential steps to prepare for your next programming interview Minimal usable Ubuntu with one command Finding awesome developers in programming interviews Compress your JSON with automatic type extraction JZBUILD - An Easy Javascript Build System Pssst! Want to stream your videos to your iPod? "This is stupid. Your program doesn't work," my wife told me The simple and obvious way to walk through a graph Asking users for steps to reproduce bugs, and other dumb ideas Creating portable binaries on Linux Bending over: How to sell your software to large companies Regular Expression Matching can be Ugly and Slow C++: A language for next generation web apps qb.js: An implementation of QBASIC in Javascript Zwibbler: A simple drawing program using Javascript and Canvas You don't need a project/solution to use the VC++ debugger Boring Date (comic) barcamp (comic) How IE <canvas> tag emulation works I didn't know you could mix and match (comic) Sign here (comic) It's a dirty job... (comic) The PenIsland Problem: Text-to-speech for domain names Pitching to VCs #2 (comic) Building a better rhyming dictionary Does Android team with eccentric geeks? (comic) Comment spam defeated at last Pitching to VCs (comic) How QBASIC almost got me killed Blame the extensions (comic) How to run a linux based home web server Microsoft's generosity knows no end for a year (comic) Using the Acer Aspire One as a web server When programmers design web sites (comic) Finding great ideas for your startup Game Theory, Salary Negotiation, and Programmers Coding tips they don't teach you in school When a reporter mangles your elevator pitch Test Driven Development without Tears Drawing Graphs with Physics Free up disk space in Ubuntu Keeping Abreast of Pornographic Research in Computer Science Exploiting perceptual colour difference for edge detection Experiment: Deleting a post from the Internet Is 2009 the year of Linux malware? Email Etiquette How a programmer reads your resume (comic) How wide should you make your web page? Usability Nightmare: Xfce Settings Manager cairo blur image surface Automatically remove wordiness from your writing Why Perforce is more scalable than Git Optimizing Ubuntu to run from a USB key or SD card UMA Questions Answered Make Windows XP look like Ubuntu, with Spinning Cube Effect See sound without drugs Standby Preventer Stock Picking using Python Spoke.com scam Stackoverflow.com Copy a cairo surface to the windows clipboard Simulating freehand drawing with Cairo Free, Raw Stock Data Installing Ubuntu on the Via Artigo Why are all my lines fuzzy in cairo? A simple command line calculator Tool for Creating UML Sequence Diagrams Exploring sound with Wavelets UMA and free long distance UMA's dirty secrets Installing the Latest Debian on an Ancient Laptop Dissecting Adsense HTML/ Javascript/ CSS Pretty Printer Web Comic Aggregator Experiments in making money online How much cash do celebrities make? Draw waveforms and hear them Cell Phones on Airplanes Detecting C++ memory leaks What does your phone number spell? A Rhyming Engine Rules for Effective C++ Cell Phone Secrets