How to run 65535 web servers on a single laptop

OK, so here's what crazy computer geeks come up with when they're bored of just sitting in the subway and staring out of the window.

Web servers usually run on port 80. TCP/UDP ports range from 1 to 65535 (port 0 is reserved). You can run multiple web servers on different ports at the same time... Do you think what I think?

Well, first you need a web server (duh). I decided to use lighttpd, as it's said to be small and memory-efficient (which sounded pretty important given what I was trying to do). Apache would probably not be a good choice here. Mind you, I have not done any benchmarks at all, I'm just guessing...

  $ apt-get install lighttpd

Then, I wrote a little shell script containing a loop, which invoked lighttpd on port 1, 2, 3, 4, ..., 65535. That's it ;)

  #!/bin/bash
  TMPDIR=/tmp
  CONFFILE="server.document-root       = \"/var/www/\"
           index-file.names           = ( \"index.html\" )"
  for ((i = 1; i < 300; i = i + 1)) do
    echo "+++ Starting web server on port $i"
    echo $CONFFILE > $TMPDIR/lighttpd.conf
    echo "server.port = $i" >> $TMPDIR/lighttpd.conf
    /usr/sbin/lighttpd -f $TMPDIR/lighttpd.conf
    rm -f $TMPDIR/lighttpd.conf
  done 

I'm sure this can be optimized a lot, but it's sufficient for now, and it works.

You can test any of the web servers by running "wget http://localhost:3556/" (for example). You can kill them all with killall lighttpd. If you already run some other daemons on some ports, you cannot start a lighttpd on the same port, so you'll end up with fewer than 65535 servers.

In case you try this on your hardware, make sure you have lots of RAM and lots of swap. Don't run this on production hardware. Feel free to post your experiences in the comments.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

OpenVZ

You could get a lot more servers running probably with OpenVZ instances. They would all have different IP addresses, but they would all be running on port 80. Or, if you really wanted to get sadistic, you could have 256 instances, each bound to all 65,535 ports. That's 16,776,960 web servers. I think you'll need more RAM. :)

>65535 servers?

If you want more then 65535 servers (lets face it - who doesn't) try running additional servers on other ip addresses. There are lots of those especially when using IPv6. :-)

The steps are left as an exercise to the reader.

bashism

s,/bin/sh,/bin/bash,
The arithmetic syntax you are using is not required by SUSv3, it's a bashism.

bash

Fixed, thanks.

OK, now how well do they

OK, now how well do they stand up to apachebench? ;)

benchmarks

That's left as an excercise for the reader :-)

Howto visit a site on

Howto visit a site on localhost with no TCP Ports available?

the loop can much shorter

the loop can much shorter :)

for i in {1..300}; do

just use vhosting

Would be a lot more efficient to bind a single lighttpd instance to all the ports and use vhosting ...

But in that's not the point

If I understood correctly, the idea was to run 65536 webservers at the same time, and using just one and vhosts takes all the fun about the idea :-)
Probably it wasn't efficient, but i'm sure it was "fun" (or something)