Building Your Own Web Server

Need a low-power, small-footprint web server? From hardware to software, Craig shows you how to roll your own server.


October 21, 2008
URL:http://www.drdobbs.com/embedded-systems/building-your-own-web-server/211300170

Craig is a degreed hardware engineer who, until recently, had been writing large-scale Java applications. He can be contacted at [email protected].


To lower my energy consumption and carbon footprint, I decided about a year ago to build a low-power web server for use in hosting my personal website. Previously I had used an old desktop PC as a host, but knew it consumed way more energy (100× even without a monitor) than what I had in mind.

My quest began with the design of a web server based on a 20-MHz 8-bit PIC microcontroller (this design was published in the July 2008 issue of Nuts and Volts (www.nutsvolts.com). The design consumed little power, but wasn't fast enough to be truly practical. This first-generation "Webster" was like the little engine that could. It tried really hard to push out media-rich pages, but it took a long, long time to do so.

The second-generation "Webster" used a faster 16-bit PIC processor with built-in Ethernet hardware also running the Microchip TCPIP stack. But even with an increased clock rate and word length, this design also was quickly dismissed as inadequate.

For generation three, I used a NetBurner (NB) MOD5270 module (www.netburner.com) coupled with custom electronics, which included a power supply and an SD card interface. This design had all of the features and performance I needed and was used successfully for my website for eight months. Unfortunately, this web server was terminally damaged when I tried to change out a part that had failed.

Knowing I had to build yet another web server to replace the broken one and being happy with the NB hardware, I created the fourth generation Webster, which I call "Webster2" because it is the second of the designs published. In this iteration, I did away with custom hardware and based the design on the NB MOD5270LC development kit. With the stock NB hardware, building the web server was a breeze. (For the record, I have no affiliation with NetBurner Inc. other than being a very satisfied customer.)

Webster2's features include:

At present, the Webster2 web server software is configured to serve static HTML pages only and cannot be used for applications that require write access to the underlying filesystem. This was a conscious design decision that could easily be changed if your application requires it.

Webster2 and Me

Besides the energy conservation issue, Webster can be put on the Internet without concern for viruses and spyware. Webster's software is written entirely in C++ with no underlying operating system with exploitable security flaws. Since, by design, there isn't any write access to the underlying filesystem, it is unlikely the website content can be compromised. Finally, since Webster supports basic authentication, access to the website can be controlled with a username and password. Of course like any web server, Webster2 could be brought to its knees by a denial-of-service attack.

Building and maintaining a website with Webster2 is easy. I compose web pages on my PC using the free nvu HTML editor (www.nvu.com). When finished, the HTML pages can be copied to an SD memory card that is subsequently moved to the server, or the pages can be FTPed to an SD memory card already in place on the server. When Webster2's Reset button is pressed, the new or modified website becomes immediately operational.

At the most basic level, any web server is just a device that understands and implements HTTP protocol; a web server doesn't influence website content. A web server's job is to stream requested content to a user's browser, whether that be HTML files, image files, PDF files, MP3s, or whatever. This is important because it is the content of the served up files that may or may not be compatible with different browsers. If the website you serve with Webster2 works with Internet Explorer but not Firefox, it probably isn't Webster2's fault.

The NetBurner Development Kit

The folks at NetBurner have put together a great development kit for their MOD5270 module. The kit includes everything you need to develop and test code including a:

NetBurner's slogan is "Networking in 1 Day!" and it is absolutely true. They provide code for support of most Internet protocols, making application development almost trivial. In addition, they provide excellent customer support and have a public forum of dedicated developers that can and do help with problems.

Networking Acronyms at Work

There are a number of networking protocols that come into play in Webster2's design.

Because Webster2 uses all of these protocols, it may be necessary to configure any firewall software you use. Most importantly, you must allow HTTP requests to pass through the firewall or Webster2 won't be available on the Internet. This feature is called "port forwarding" in some cases.

Using Webster2

Before Webster2 can go online. you must decide if static or dynamic IP addressing is to be used. Using DHCP is always easiest if your network permits it. For my website, I connect Webster2 directly to my wireless DSL router and use DHCP so the router assigns an IP address automatically. This works fine. Once your web server has a public IP address, as mentioned, you must open up the firewall in the router to allow inbound HTTP connections so users outside your network can hit your server. How this is done depends upon your firewall software.

Keep in mind, in trying to host a website on a home DSL connection, the IP address assigned to your DSL router changes from time to time. This is a problem whether you are using Webster2 as your web server or a professional product like Apache. Regardless of how you access your website, when your router's IP address changes, connectivity to your website using an old address will be lost.

A solution that lets you host a DSL-connected website is a program called No-IP (www.no-ip.com). No-IP (the company) offers a free dynamic DNS and web redirection service for just this purpose. No-IP will even provide you the use of a domain name from a list of preexisting domains that you can use for free.

The No-IP client program runs as a service under Windows XP. It continually monitors the public address exposed by your router for your web server and informs No-IP's DNS servers of any changes. So as long as access to your website is via your chosen domain name, you are insulated from changes to your router's IP address. By running No-IP on any PC in your network, access to your website is maintained even if the public IP address of your router/server changes. Pretty slick, huh?

Once you register your web server host with No-IP and your server is operational, you should be able to ping your server:


ping myhost.mydomain.com


Executing this command verifies whether myhost in the domain mydomain.com is operational or not. You would need to change the ping command to check your server in your domain.

Once you have web content in place and you can ping your server, you should be able to access your website with a browser (Internet Explorer, Firefox, or whatever) by specifying a Universal Resource Locator or URL of the form:


http://hostname.domainname


If you don't specify a specific HTML file to access, the file index.html is assumed. Once this works, you are good to go.

Webster2 Hardware

Again, Webster2 is based entirely on the NB MOD5270LC development kit hardware. As such, only two things are required to get Webster2 running. First, you need to insert an SD memory card (64-GB max) into the socket on the development board and second. you need to download the web server code I provide (see www.ddj.com/code/) with this article.

While packaging the NB hardware is not required for its operation, I decided to package the development hardware to protect it from accidental abuse or damage. At my local electronics shop, I found a plastic box made by Serpac that the hardware would easily fit into; see the complete parts list in Table 1.

Designation Value Notes
MOD5270LC NB development kit hardware. Part #: NNDK-MOD5270LC-KIT Available directly from NetBurner, Inc.
Box Serpac Model #271 or #271-I Dimensions: 7"x 4 3/4"x 1 3/4" See www.serpac.com
SPST power switch Any brand off/on switch I used a miniature toggle switch
SPST momentary contact pushbutton switch Any brand pushbutton switch This is the server's reset switch
LED power on indicator Any brand LED I used a green LED for power on
LED access indicator Any brand LED I used a red LED as access indicator
Misc. hardware Screws, nuts and spacers, etc. For mounting NB module
Memory Card SD memory card 64 GB max; 1 GB usually adequate

Table 1: Webster2 parts list.

Because I was going to hide the hardware inside the box, I thought it necessary to mount important controls and indicators on the outside. In the end, I mounted a power switch, power indicator, reset switch, and access indicator on the box; see Figures 1 and 2. The packaging steps I took are as follows:

  1. Lay the NB module into the box with the dip-switch facing the front of the box to visualize how things will fit.
  2. Remove any of the box's molded standoffs that are in the way using a drill bit larger in diameter than the standoff. The idea is that the NB hardware should be supported in the box by the acrylic feet attached to the bottom of the printed circuit board (PCB).
  3. With the PCB held in place, mark the box with the position of two or more of the mounting holes on the NB PCB. Drill these holes for #6 machine screws.
  4. Layout the positions of the two switches and the two LEDs on the bottom front portion of the box. Drill holes of the appropriate sizes for mounting. Glue the LEDs into place and mount the switches.
  5. File two holes in the top rear portion of the box to allow the power and network cables to protrude.
  6. Mount the NB PCB into the box using appropriate screws. The screw heads should be on the outside of the box; nuts on the inside. Tighten the screws but not so tightly as to cause the PCB to bend.
  7. Carefully unsolder surface-mounted LEDs 8 and 9 from the PCB. Unsolder and remove the reset switch as well. Led8 is the furthest right in the row of eight LEDs. Led9 is located to the left of the dip-switches. The reset switch is located above the row of eight LEDs.
  8. Cut the connector off of the supplied power cable. Pay attention to the polarity of the cable. There was a white strip on the + side of the cable on the unit I used. If you cannot tell which is the + side of the cable. use a multimeter to determine it.
  9. You will probably have to extend the power cable by soldering other wires onto it. A six-inch extension will probably do it. Again. be careful of the polarity.
  10. Solder the - end of the power cable to the GND terminal on the PCB. Solder the + end to one side of the power switch.
  11. Solder one side of another wire to the other switch connection and the other side to the "7to18V" terminal on the PCB.
  12. Determine the + side of the power LED by holding the leads to a 9V battery and noting which orientation causes the LED to light. Note which lead of the LED is connected to the + side of the battery.
  13. Solder a wire from the + side of the power LED to the + connection on the PCB for Led9, which was removed earlier. Solder another wire from the other lead of the LED to the other connection for Led9.
  14. If you are feeling lucky, plug in the wall wort and turn the power switch on, if all is well you should see the power LED come on. Turn power back off and unplug the wall wort.
  15. Solder two wires from the reset switch to the two pads on the PCB from which the reset switch was removed. Polarity is not important here.
  16. Determine the + side of the access LED as you did for the power LED.
  17. Solder a wire from the + side of the access LED to the + connection on the PCB for Led8, which was removed earlier. Solder another wire from the other lead of the LED to the other connection for Led8.
  18. Connect an Ethernet cable to the RJ-45 connector on the MOD5270 module and route it and the power supply cable out of the box.
  19. Place the top on the box and screw it to the bottom using the screws provided.

That's it with the hardware. Plug Webster2's power cable in and plug the network cable into a network port. Flip the power switch to on and you are ready to go.

[Click image to view at full size]

Figure 1: Inside the Webster2 chassis.

[Click image to view at full size]

Figure 2: The finished Webster2 Web Server From the left are the power switch, the green LED power on indicator, the pushbutton reset switch, and the red access LED. Holes are filed in the rear of the chassis for power supply and network cables to pass through.

Webster2's Software

You will need some of the tools provided by NB on the CD that comes with the development kit to download the web server software to your server. Install the development kit on your computer. Also, get the ZIP file containing the Webster2 code from the DDJ website (see www.ddj.com/code/).

First, it is necessary to verify your server is running and available via the network. The easiest way to do this is to bring up the NB IPSetup program. If all is well you should see a MOD5270 device listed in the right pane and see the networking parameters your server has adopted in the left. If you don't see your server, make sure the server is plugged in, the power switch on, power indicator lit, and the network port you plugged into is active.

Once you can see your server, bring up the NB AutoUpdate tool. The IP address you saw in the IPSetup program should again be displayed. Click the Browse button and navigate to the directory you unzipped the Webster2 code into. Find the file Webster2_APP.s19 in the release directory. Click the Update button and the web server code will be transferred to your server.

Before Webster2 can be used it must be configured. Webster's operation is controlled by the config2.dat configuration file that must reside in the root directory of the server's flash drive. Webster will blink all eight on-board LEDs and halt operation if the configuration file is not found, or if an error is detected in its format. Example 1 is a typical configuration file. The configuration file is a human-readable text file with each line terminated by carriage-return, linefeed characters. The code (in config.cpp) that parses this file is not very forgiving so it is important to not deviate from the format shown. Most entries in the configuration file are probably self-explanatory.


// Webster2 Configuration File - Version: 1.1
// NOTES:
//   1. Case is significant
//   2. No blank lines allowed; Use // for comment lines and formatting
//   3. Be careful with spaces around usernames and passwords
//   4. Don't use tabs
//
Realm Name: Your website's Name
Hostname: YourHostname
Use DHCP: yes
//
Use Authentication: no
Username: roy
Password: rogers
Pages Requiring Authentication: page1.html,page2.html,page3html
//
Use FTP Security: no
FTPUsername: rogers
FTPPassword: roy
//
Use NTP: yes
NTP Servers: 207.200.81.113,164.67.62.194
Time Zone: -6
Use DST: yes
//
// Static IP Addresses - Only necessary if not using DHCP
//
Static IP Address: 192.168.0.32
Static Subnet Mask: 255.255.255.0
Static Gateway Address: 192.168.0.1
Static DNS Address: 205.171.3.65
//
// Use UDP Logging
//
Use Logging: yes
Logging Hostname: your logging hostname

Example 1: Typical configuration file.

If Use Logging is "yes" and Logging Hostname is specified, UDP packets are sent the specific host. If Use Logging is "yes" and no Logging Hostname is specified, UDP packets with logging information are broadcast.

You have two ways to make configuration changes: You can remove the SD memory card from the server and connect it to your computer via an SD card reader and make changes to the config2.dat file directly; or you can edit a local copy of config2.dat and use FTP to update the server's configuration. Either way, once the configuration changes have been made and the Reset button hit, Webster2 should come back up with the new configuration operational.

Once you have determined your configuration requirements and have them coded in the config2.dat file, you can start working on your website's content. Remember, you should have a file in the root directory named index.html or index.htm as this will be the entry point for your website. Other than the index file, how you arrange your HTML files and other website content is up to you. You have a full FAT filesystem on your web server so filename case is important and long filenames are permitted. Also, you can organize your files into subdirectories if that fits your style.

In this iteration of the web server software, I wanted to have some form of logging so I could see when and by whom my website was accessed. My first inclination was to use a log file in the filesystem, but I rejected this approach as flash memory has a large but finite number of write cycles. Writing to a log file for every website access could damage the flash over time. Instead, I decided to log across the network to another machine. I used NB's SysLog function in the web server code and NB's UDP Terminal Tool on the remote machine to see all accesses to my website. It is pretty cool. System logging uses UDP port 514.

Within the GET processing code in the web server, I turn Webster2's access LED off/on. This causes the LED to flash whenever my website is accessed.

Finally, if you want to modify and/or extend the Webster2's web server software I provide, you need to create a new project within the Eclipse development environment and import all of my code. With this as the starting point, you can make all of the changes you desire.

Conclusion

Webster2 was fun and easy to build and helps me conserve energy. Webster2 is the fourth-generation web server I have designed and built and is fully capable of supporting all my current web server needs. It has the stability and performance necessary for serving pictures, large documents, music, and video files for my family and friends.

Webster2 costs about $130 to build, but you will have the satisfaction of knowing you did it yourself. That can earn you some serious bragging rights with your high-tech buddies.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.