back to contents

Systems Stuff: building a simple web server

first of all

this builds on top of the linux guide. you should read that first, really. you should have a good grasp of the following:

actually set up the server

primarily, you need to set up the box. the server. the machine. whether it's virtual or physical. I recommend virtual, but if you have a spare computer just sitting around, go ahead and use that.

and contrary to the popular belief, a web server really doesn't have to be a powerful computer. some web servers are old IBM boxes running in peoples' basements. the whole point of using linux is that it can run on pretty much anything and be stable. it doesn't take up a lot of resources.

a good, stable web server might be a 1GHz Pentium 4 with 512MB of RAM and a 20GB hard drive. Actually, that's probably a mid-range web server. You could do a lot worse. You're probably not going to be doing anything that requires more than that.

if you don't remember how to to do the initial setup of a linux machine, you're actually going to have to go back to the linux guide and do a few steps from there, haha. I won't repeat myself here.

here's the guide. if you don't remember how to set up a server, follow the sections before anything, you need a server and ok so you have a server, cool and then come back here

i love LAMP

the packages we're going to install on the server to make up our web host are commonly referred to as a LAMP server. well, that's one option anyway.

LAMP stands for Linux, Apache, MySQL, and PHP. So you have Linux as the OS, Apache as the actual web service, MySQL as the database, and PHP as the scripting language.

This is how the vast, vast majority of web servers are set up. But I think it's outdated. Honestly, I think Apache is the part that's outdated. Apache was great back in the day... but now it's kind of bloated compared to others.

Almost every chance I get, I use lighttpd instead of Apache. lighttpd (pronounced "lighty") is a newer, fresher approach. it's an extremely lightweight web server, focused on an asynchronous environment.

I'll teach you how to do both... use either Apache or lighttpd -- I mean honestly the difference is just which packages you install. you should seriously consider lighttpd when you have thousands upon thousands of users, maybe.

So the next two sections, choose one of them. You can't do both. (Well you can but it's messy and I won't teach you that yet.)

Going with Apache with the PHP module

If you go with Apache, you install PHP as a module of apache, so the two are linked. you'll see what i mean.

first, let's install apache. specifically, we use apache version 2.2, which is listed as the apache2 package.

> apt-get install apache2

Once that's done, open a browser, any browser. you should be able to paste in the IP of your server into the address bar, hit enter, and... bam! You should see a page! That's the default apache page.

Congrats, you now have a simple web server. That's all it took! If you put files in /var/www (the web root) they'll be accessible via a browser.

But it can't do any PHP yet... it can't do any scripting languages. It just serves files when asked for them.

Let's demonstrate this. Go to /var/www

> cd /var/www

Now let's make an HTML page

> nano what.html

in there write whatever you want. some HTML if you want. doesn't matter. save it by pressing CTRL+O and then exit nano by pressing CTRL+X.

now in your browser, add /what.html after the IP of the server! Tada! Magic.

So now let's get PHP running. Here's what you need to do:

> apt-get install libapache2-mod-php5

That's it! You now have PHP installed and you can access it through apache.

the configuration files for Apache live in this directory: /etc/apache2 and there's a few of them. The important ones are apache2.conf and the files in the sites-enabled directory. take a look at them if you want.

And remember that if you make any changes, you have to reload or restart apache to make the changes stick, like so:

> /etc/init.d/apache2 reload

Let's test it out. (Skip the lighttpd section.)

going lighttpd with PHP CGI

if you go with lighttpd, you install PHP separately as its own thing, in this case a CGI process. CGI stands for Common Gateway Interface and it has been around for a very long time.

as I said, lighttpd is much lighter and faster, but it requires a little more work to get it running how you want. apache and its php module are really easy to install, it's just two packages.

first, let's install lighttpd.

> apt-get install lighttpd

That's it. Now you have a web server! It'll serve whatever files are within /var/www and if you go to the server's IP address in a browser, it should show a placeholder page!

So if you go to /var/www and put your own HTML file in there, you can access it in your browser, as you would any other page! Magic.

The configuration file for lighttpd is here: /etc/lighttpd/lighttpd.conf

You have to enable some modules to get PHP to work, but first, let's install it.

Right now all lighttpd does is serve files. You ask for one via the URL, and it gives it to your browser. We need to add PHP as a CGI process in order for it to process PHP for you.

So we install this package:

> apt-get install php5-cgi

After that's done installing, we now have lighttpd serving files and PHP willing to work as a CGI interface. But how do we connect the two? Apache's PHP Module does that work for us, but in lighttpd we need to enable the FastCGI module ourselves.

How do we do that? Well, first go to lighttpd's configuration directory:

> cd /etc/lighttpd

if you run the ls -la command you'll see the conf-available and conf-enabled directories. One holds configuration files you could load (the available one) and the other holds the ones you've chosen to load (the enabled directory). Pretty straightforward.

So look inside the available modules:

> ls -l conf-available/

That'll return the list of configuration files available for you to use. There are a lot. We are only interested in two of them. 10-fastcgi.conf and 15-fastcgi-php.conf

The first file will tell lighttpd to use FastCGI. This is a way of it using any CGI interface. The second file tells lighttpd to use PHP, specifically.

To enable them, simply copy or soft-link the files into the conf-enabled folder. You can do it this way:

> cd conf-enabled/
> ln -s ../conf-available/10-fastcgi.conf 10-fastcgi.conf
> ln -s ../conf-available/15-fastcgi-php.conf 15-fastcgi-php.conf

What did we just do there? We went into the conf-enabled directory and created two soft links (shortcuts) to the conf files in conf-available.

We could've just as easily copied them. Probably should have copied them. Oh well. Moving on.

Make sure you reload the lighttpd service to make it save the changes to the config:

> /etc/init.d/lighttpd reload

(And make sure you do that every time you change any configuration options or files.)

That's all you need to do to get php working with lighttpd!

test out php!

Now go to /var/www and make a file called info.php with nano and put in the following:


Save that and close nano. The phpinfo() function is a built-in PHP function that displays a lot of information about the current PHP install. Try going to info.php in your browser.

(Note: if your browser downloads info.php instead of actually showing you anything, you probably need to restart apache or lighttpd by typing this into the console: /etc/init.d/apache2 restart or /etc/init.d/lighttpd restart )

You should see a long page with a lot of information. This is a huge list of information regarding PHP and the libraries it has loaded. You'll see things like the dom and json and SimpleXML libraries.

if you are using apache, the config file for php lives here: /etc/php5/apache2/php.ini and it's just a text file.

if you are using lighttpd, the config file for php lives here: /etc/php5/cgi/php.ini and it's just a text file.

PHP now works with your web server. Now you can move on to installing MySQL.


installing MySQL works the same way regardless of what web service you choose. it's pretty simple:

> apt-get install mysql-server

That's it... it'll take you through a lot of options, like setting the MySQL root user's password. that's important, don't forget it.

Seriously, it's that simple. When it's done installing, mysql should be running. You can check by typing:

> mysql --version

which will print out the current version of mysql running on your server. if you want to go into mysql and start running SQL commands, do this:

> mysql -u root -p

and enter your mysql root password when asked. now you're in the mysql shell! kind of like the linux shell, except you live inside mysql instead of a file system. type exit to leave and go back to the linux shell. pretty cool, huh?

getting PHP to work with MySQL

So once again we have two pieces and now we need a way for them to work together! If you noticed in your info.php page, there probably isn't a MySQL section. we need to install that, too.

thankfully it's pretty easy!

> apt-get install php5-mysql

(There are a lot of php5 libraries available, if you search with this command: apt-cache search php5 and hit enter, you'll see a list of available libs.)

Now to actually make that new library available, you need to reload the web service again! You should know how to do that by now, so go ahead.

After it reloads, check your info.php page again. It should now have a mysql section. Now your install of PHP has all the mysql functions! How wonderful.

if something breaks

Sometimes PHP won't tell you when it breaks, or lighttpd won't either... for this, check the logs!

If you go to /var/log there'll be a folder for apache2 or lighttpd, with error and access logs within. PHP errors are typically reported in there, too.

mysql errors could be in /var/log/mysql.err or in the /var/log/mysql/ directory.

read the files with the less command! they're just text.

that's pretty much it.

You now have either a LAMP or LLMP server set up. congratulations! you now have the perfect web development environment to test things out and play!

I have one more tip though, to make things cooler. you really don't need to do the following stuff, the guide for making a simple web server is over.

however, with your server as it's set up right now, you need to access it via IP in a browser. what if you wanted to access it via a cool domain name?

forget your IP / what is DNS

get the IP of the server you just set up. (you probably already have it if you're already SSH'd into it.) you access it via a browser by using that IP.

however, if you want to actually give your server a DNS name that looks a lot cooler, i'll help you out here. well, what is a DNS name? it's a fancy way of saying domain name. is a DNS/domain name. DNS just stands for Domain Name System, and it's a part of the backbone of the internet. all it does is translate into an IP for a server. seriously, that's it!

when you go to Google, the very first thing your computer does is ask: wait, where is Your computer sends out a DNS request for the name

think of it this way: my apartment might be known as "cyle's awesome apartment", but to get there you probably need the actual address. DNS is the same. is just a name, the actual address is an IP, like

try it, actually. in the prompt, type this:

> host

That shows you the actual IPs of the servers of wouldn't it be cool if we could have a domain name for our little web server?

hosts files

DNS is a system that's run on almost every computer, but certain servers on the web are authoritative DNS servers and actually hold most of the keys.

however, on every computer you can override and write your own DNS shortcuts. this is done on the operating system's hosts file.

on Windows, this file is located in this directory: C:\windows\system32\drivers\etc

on Mac, this file is located in this directory: /etc (which you can't get to through Finder, you have to go through Terminal)

You'll notice that the file has no extension, but it's just a plain text file.

on Mac I'd suggest you just use the free app Gas Mask to edit your hosts file.

on PC you'll have to open Notepad as administrator (right-click on notepad and Run As Administrator) and then open the file manually.

Inside the hosts file should be nothing but maybe some commented-out text about what the file does.

At the bottom, make a new line, and put in this:

well, replace with the IP of your server. replace with whatever you want your server's domain name to be. it can be anything.

now you need to flush your DNS cache so that your computer accepts these changes. with Gas Mask on Mac, I think it'll do this automatically.

on Windows, you need to open a command line window (go to Start > Run and type cmd and then enter) and type ipconfig /flushdns then enter into the Windows prompt. give it a second.

Now if you go to a web browser and type in that domain name, your server should pop up.

Please note: this will only work on your computer! you edited your own hosts file. it won't work across computers; you'd need to edit their hosts file, too.

setting up your own DNS server and shit is way more complicated than I'm going to get into.

but doing this on your own computer might make life a little easier if you don't want to have to remember the server's IP.

the end

that's how you set up a simple web server! wow!

I make one like all the damn time to test stuff out on. I like virtual machines because they're easily disposable and I can have a bunch running at once.

You could even get crazier and make it a node.js server, too......