Adding a web server to your Buildroot project - cu-ecen-aeld/buildroot-assignments-base GitHub Wiki

NGINX

This section describes how to set up the NGINX server on Buildroot for Raspberry Pi. A working example for Raspberry Pi can be found at this Pull Request

Introduction

nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev. Wordpress.com and Netflix are the success stories based on nginx. Reference - https://nginx.org/en/

All the documentation related to the nginx server can be found at - https://nginx.org/en/docs/

Implementation Steps:

  1. This example shows working nginx server configure for Raspberry Pi using Buildroot.

  2. For setting up nginx server follow the steps mentioned below, in the Buildroot base repository:

     A. open buildroot directory.
    
     B. open menuconfig using "$ make menuconfig"
    
     C. Go to "> Target packages > Networking applications" and press 'y' on 'nginx --->' to select. Then press 'Enter' 
        key.
    
     D. There are some packages which are selected by default. If you want to add more feature support to your server 
        you can select more modules from the list which is displayed. For example, the 'ngx_http_ssl_module' is 
        selected if the user wants 'HTTPS' functionality, then this module should be selected. (Packages 
        shown below are the packages selected for this example:)
    

    E. Once all the necessary modules are selected, 'Save' and 'Exit' from the 'menuconfig'. Now the 'nginx' server is 
       set up.
  1. When the 'nginx' server is set up successfully, a startup script for the server is generated in /etc/init.d named as 'S50nginx'

  2. The rootfs_overlay folder has the sample template for html web page at /rootfs_overlay/usr/html/. This web page is displayed the server is accessed.

  3. In menuconfig Go to "> System configuration" and add path of your rootfs_overlay to the users table as mentioned below:

rootfs_overlay

  1. run build.sh and load the sdcard.img into sdcard and load this image into your raspberry-pi board.

  2. Connect your Raspberry Pi to your local network. On boot up, the 'eth0' gets an IP address as shown in the image below:

The image shows the IP address assigned to Raspberry pi as 10.0.0.13.

References Used/Leverage code:

Reference used

Demo

Access this address from your local network and the Web page is displayed as shown below:

In this way, the nginx server is set up using Buildroot on Raspberry Pi.

Apache

This section describes using Apache as an alternative to NGINX as your project web server.

Creating a local web server using Apache

We can host a web server by installing and running a server application inside Buildroot. NGINX and Apache are two such open-source server applications. I have used Apache to create the local server. Below are the steps followed:

  • Step 1:

Installing Target packages: Inside build root repository. Add necessary packages with make menuconfig

  • Target packages -> Networking applications -> [x] apache (default settings)
  • Target packages -> Interpreter languages and scripting ->
    • [x] php
    • [x] Apache interface

image
image

  • Step 2:

Now Apache server is set up successfully, we need to add a startup script at rootfs_overlay/etc/init.d/ with the name S50apache
see this for a sample script: link
Make the script executable with chmod +x /etc/init.d/S50apache

  • Step 3:

To Add web files according to your customizations, add them at /rootfs_overlay/usr/htdocs/
This webpage written in .html or .php will be displayed once we access the server.

In order to determine which directory to save html file, always check the httpd.conf file, which is the default setup for the server.
In this section DocumentRoot indicates the location to save the documents

image

  • Step 4:

Add the path of rootfs_overlay directory in menuconfig ->System configuration.

image

  • Step 5:

In order to enable PHP, we need to edit the httpd.conf file at /etc/apache2/httpd.conf
Thus we can add rootfs_overlay/etc/apache2/httpd.conf file so as to configure php. For reference for httpd.conf file: refer to this link from a working repo : httpd.conf

From the default httpd.conf file, I have changed the section inside the file <Directory "/usr/htdocs"> node:
image

Added:

DirectoryIndex index.php<br />
<FilesMatch ".+\.ph(p[3457]?|t|tml)$">
  SetHandler application/x-httpd-php
</FilesMatch>

image

Now add your files with php script at /usr/htdocs/

  • Step 6:

now run ./save_config.sh and build.sh. After that flash the image to SD card. Get the Ip address of R-pi as it boots: In the below image, it is 192.168.43.205.

image

  • Step 7 :

To check if the web server is working. Connect your PC to the same router network. Try accessing the IP address from a browser: 192.168.43.205 and the .html/.php file as given below
image

For a working example, check this repository for the build root configurations: - https://github.com/cu-ecen-aeld/final-project-ayswariya088

References

  1. https://github.com/TOLDOTECHNIK/buildroot-webkit/issues/13
  2. https://www.instructables.com/Connecting-Your-Raspberry-Pi-to-the-Web/
  3. https://docplayer.net/230651206-Building-an-embedded-web-server-with-buildroot.html
⚠️ **GitHub.com Fallback** ⚠️