How to run a linux based home web server
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:
- Domain name
- DNS server
- Web server software properly configured for your system
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.
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.
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.
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
Configuring your serverInstall 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 ddclientDDclient 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 Apache2There 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 bandwidthWhen 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.
In conclusionIf 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.