# Setting Up Networking and an HTTP Server on the BeagleBone Black with Buildroot

This is a continuation of my [previous blog](https://blog.billvanleeuwen.ca/creating-a-minimal-linux-system-for-the-beaglebone-black-with-buildroot) where we set up a minimal Linux system for the BeagleBone Black with Buildroot.

Today we're going to set up networking and an HTTP server on the board to serve web pages.

#### Why might we want to do that?

Take for example any IoT device like this [Smart Washing Machine](https://www.geappliances.com/ge/connected-appliances/washer-dryer-laundry.htm), rather than running a cycle by having the user SSH into the Linux board and run scripts, one might run an HTTP server on the board and show a pretty website with nice features that anyone could use.

## Modify the Buildroot Image

We need to add some extra packages to our board, mainly the HTTP Server.

Just like last time, change into your buildroot directory, and run:

```plaintext
make menuconfig
```

You should see the familiar menuconfig page:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676467638475/6e8a79b6-0bb7-4b38-b233-fb8e4d49605f.png align="center")

* In *Target Packages: Networking applications:*
    
    * Enable nginx, make sure http\_server is enabled
        
    * Enable [dropbear](https://matt.ucc.asn.au/dropbear/dropbear.html). This is an SSH client that will be convenient for us.
        
    * Enable ifupdown scripts. These are init scripts courteously written for us to initialize our static network connection on the board.
        

## Set up Networking

To connect our board to a network, we need a connection to our desktop.

Normally, you could do this by plugging your board into your router and letting it assign an IP address, but my router is pretty far from my desk so we'll have to use another method.

**Option 1**

If you have a network switch laying around, like this little Netgear one I have, you could use that:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677943581016/8c1da39a-073f-4cae-bde8-14e7021dd639.png align="center")

**Option 2**

You could use a [Crossover Cable](https://en.wikipedia.org/wiki/Ethernet_crossover_cable). This will allow you to connect your desktop directly to the Beaglebone.

### Setting IP Addresses

Like I spoke about before, your router could just assign the board an IP address. This is because the router is running a [DCHP Server](https://www.infoblox.com/glossary/dhcp-server/) that assigns IP's for every computer on your network.

Since we are connecting our PC directly to our board, this will be a separate network from your router and will require different configuration.

We could run a DCHP server on our Desktop to assign the board an IP, but because we're only connecting one thing to this network, that is certainly overkill.

We'll define a static address for our board in its `/etc/network/interfaces` file.

#### Making an Overlay Directory

Instead of SSH'ing into the board every time we create a new image to modify this file, we can use an overlay file. This is an option in the buildroot menuconfig to allow us to add things into the image each time it builds.

In the buildroot directory, create and overlay directory, and fill it with the directories and files we need:

```bash
mkdir -p overlay/etc/network
touch overlay/etc/network/interfaces
vi overlay/etc/network/interfaces
```

In this vi window, paste:

```plaintext
auto eth0
iface eth0 inet static
    address 192.168.1.100
    gateway 192.168.1.1
    netmask 255.255.255.0
```

Here what we are doing, is setting our ethernet interface, `eth0`, to have a static ip address of `192.168.1.100`, and giving it the gateway of `192.168.1.1`.

Now enter the menuconfig, and in System Configuration, set the Root filesystem overlay directory to `./overlay`

Now run `make` to build this new image, flash it onto your SD card when you're finished.

Plug the board in and boot it holding the USR button as usual. Plug your crossover cable or switch into both the board and your desktop.

In Settings, if you're running the GNOME Desktop Environment, go into network. You won't see a connection to your board yet, because you havent configured the static IP for your desktop.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677945211966/99309519-7a0f-444a-9fc4-8c4fdc4ff532.png align="center")

Add a Wired network, and set your settings as follows:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677945430034/b05e804d-61e1-426a-a07a-043bc8b0beb7.png align="center")

The last set of numbers on your address can be whatever you'd like (except the IP you used for the beaglebone), I just used `192.168.1.45`.

Save this network, and you should now see that you're connected to a wired network!

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677945535864/26a7b6b5-1e03-4f28-b3ce-3caadaa19552.png align="center")

## SSH

Now we should be able to ssh into our board.

```bash
ssh root@192.168.1.100
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677945631864/3eb16164-1762-45f6-b15c-3a9d89209a78.png align="center")

You'll notice ssh is acting very sluggish upon connection. There is something we can disable on the image to fix this.

In the menuconfig, navigate to the dropbear package. Inside this, disable reverse DNS Lookups.

[Reverse DNS Lookup](https://www.cisco.com/c/en/us/support/docs/switches/nexus-5000-series-switches/211983-Delay-Before-Password-Prompt-Appears-whi.pdf) is a security feature, normally used to verify the authenticity of connections. But since our beaglebone isn't connected to any DNS server it will hang here as it screams out into nothing, waiting for a return from a non-existent DNS server.

Build this image and put it onto your card, and SSH access should be fast now!

## Serving Web Pages

Busybox init handles a lot of the work for us here. Because we enabled nginx, buildroot has assumed that we want it to run each time on startup.

With the board booted up, and network cable connected, you should be able to go directly to `192.168.1.100` in your browser and get the nginx configure page:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1678035172629/c4c965ad-bab7-4f36-9a7b-1be09653fd6b.png align="center")

According to

Next, we need to provide some basic configuration to NGINX to serve our custom pages. In the [The NGINX Beginners Guide](https://nginx.org/en/docs/beginners_guide.html), we learn that the configuration file for nginx is in `/etc/nginx/nginx.conf` and has a relatively simple configuration process.

Instead of doing this on our board, where every time we reinstall an image we'd have to repeat this process, lets automate this through the use of the overlay directory.

Back in your buildroot diretory, cd into your overlay directory.

```bash
mkdir -p etc/nginx/
touch etc/nginx/nginx.conf
```

In a text editor, enter the following simple config into `nginx.conf` :

```plaintext
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root  /data/www;
            index  index.html;
        }
    }
}
```

This tells nginx we want to serve out of `/data/www` which is the recommended location in the aforementioned beginner's guide.

Create this directory in the overlay as well:

```bash
mkdir -p data/www
touch data/www/index.html
```

In a text editor, let's add a "Hello world page" to demonstrate a working embedded web server:

```javascript
<!DOCTYPE HTML>
<html>
<body>
  <script>
    alert( 'Hello world from the BeagleBone Black!' );
    document.write('Hello world from the BeagleBone Black!');
  </script>
</body>
</html>
```

After this, run `make` to build the image, flash it to the board, and boot it familiarly.

Navigate to the board's address in the browser, and:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1678040002642/12477826-1356-447a-bb7c-428c9c34c859.png align="center")

Success! We can serve web pages from the beaglebone to your desktop!
