• Blog
07 Feb 2016

Setting Up A Multiple-Raspberry Pi Web Server: Part 4 (Create a working web server)

Congrats on finishing Parts 1, 2, and 3! Hopefully at this point you have one or more working raspberry pis, and each of them has its own static IP address.

At this point, we're ready to actually make things happen:

By the end of this post, you should know how to set up a basic web server on your raspberry pi(s) and query that webserver from any other computer on your network.


Step 4.1: Start a web server on your raspberry pi

This part is pretty straightforward--we're going to get a basic web server up and running on your raspberry pis.

Step 4.1.1: Get the code for the web server

The easiest way to get a working web server is to go to http://howtonode.org/hello-node and copy the example "hello world" web server available there. Just to make it even easier, I've copied the code below, also:

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {  
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");  

Now, just to make this and future tutorials in this series a bit easier, I'm going to modify the return value for all incoming requests to ALSO mention the static IP address of the web server serving the request. So I'd recommend changing the code to look like this:

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {  
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello from raspberry pi #1 at 192.168.1.200!\n");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");  

Take this code, and put it in a file on your raspberry pi called: basic_node_webserver.js

Step 4.1.2: Start the web server

Once the JavaScript file from Step 4.1.1 is on your raspberry pi, starting it up is as easy as running node basic_node_webserver.js from inside a terminal. After doing this, your terminal should look something like this:

$ node basic_node_webserver.js 
Server running at http://127.0.0.1:8000/  

Now, any requests to port 8000 on that raspberry pi will be served by the web server. Leave the server running for now, and switch to another computer on your local network (eg. your laptop or desktop computer).

Step 4.2 Query the webserver from another computer on your network

This part is even more straightforward than step 4.1. You already have the web server running on your raspberry pi, now all you need to do is query the web server.

Assuming that the static IP address of your raspberry pi is 192.168.1.200, here are 2 ways you can query the web server that's now running on your raspberry pi:

  1. curl 192.168.1.200:8000 from the terminal of your other computer.
  2. Open up a browser and navigate to 192.168.1.200:8000.

In either case, you should see the response of the web server that's running on your raspberry pi:

Hello from raspberry pi #1 at 192.168.1.200!


And that's it! Your web server is running on your raspberry pi, but is queryable by any computer on your local network (assuming they use the appropriate static IP address).

Some Notes

  • This tutorial does not teach you how to make your raspberry pi web server available to the wider internet. Such tutorials do exist, however, and most of the things we discuss here will be valid there, also.
  • We used port 8000 for the web servers in this tutorial. As a result, we had to specify the port number at the end of the static IP address.
    • If you'd rather NOT have to specify the port at the end of the IP address, then you need to serve requests at port 80 .
    • Since port 80 is the default for HTTP requests, you can leave off the port number, and the computer will assume you mean port 80.
      • This is what's happening any time you go to a website online. http://www.gregtrowbridge.com is actually the same as http://www.gregtrowbridge.com:80
    • In reality, running a local web server at port 80 is kind of a pain, but I'll show you how to forward requests from port 80 to port 8000 (or any other port) using HAProxy in Part 5.
  • If you followed this tutorial with multiple raspberry pis, you've probably realized that:
    • We don't yet have a multi-raspberry pi web server that works together to serve requests from the same address.
    • Instead, we just know how to set up one or more individual web servers that respond to requests at different addresses
      • eg. You may have one raspberry pi webserver that serves requests at 192.168.1.200, one that serves requests at 192.168.1.201, one at 192.168.1.202, etc.
    • Good News: I explain how to get all of the raspberry pis working together in Part 5.
Tag

Greg Trowbridge

High Frequency Trader -> Entrepreneur -> Software Engineer

Share

Twitter Facebook Google+
Back to all post
comments powered by Disqus
Subscribe!
All content copyright Greg Trowbridge © 2025 • All rights reserved.
Proudly published with Ghost