Buildroot basics: create a web server


Publicerad av Kenneth Andersson
Buildroot supports building in a different directory. We will use this now that we will have multiple configurations we want to keep.

We start out with our buildroot environment from the last session and again will use qemu as a test platform.
The output we will put in a separate directory called myWebserver that is located along side the directory myLinux that contains the buildroot source tree.
When calling "make" we add the parameters "O=${PWD} -C ../myLinux" to inform make where to find the buildroot source tree and where to store generated files such as image and configurations.
Our directory "myWebserver" will now contain the same output as was previously found under the directory "myLinux/output"

To create a web server we need to add network capability and a web server software with some additional tools.
Type 'make O=${PWD} -C ../myLinux qemu_x86_defconfig' to set a predefined qemu configuration and then ' make O=${PWD} -C ../myLinux menuconfig' to begin the configuration.

Add the same settings as in the previous sessions to get the rootfs in a correct format.

Under 'Package selection for the target' we will find a number of categorise such as audio, graphic, network and even games.
Click in 'Show packages thar are also provided by busybox'.
Then we add the following packages:
'Toolchain'
    'Enable large file' - sshfs needs it.
    'Enable WCHAR support' - serveral packes need it.
'Interpreter languages and scripting'
    'php' - will let us do some more advanced web-services.
    'PHP extensions'
        'sqlite' - add database support for sqlite to php.
'Libraries'
    'Compression and decompression'
        'zlib' - might come in handy.
    'Database'
        'sqlite' - a simple, local and good enough database. We wont use it this time but include it anyway.
            'command line editing' requires ncurses and readline
    'Javascript'
        'JQuery' - will give us some fancy moves when designing web pages.
'Network applications'
    'dropbear' - ssh server which we later on can use to remotely mount directorys onto our host.
        'optimized for size'
    'lighthttpd' - the web server
    'wget' -  allows us to download from the terminal.
'Text editors and viewers'
    'uemacs' - just an editor. If you prefere vi or other, pick that one instead.

Save the configuration and run 'make O=${PWD} -C ../myLinux ' in the root directory of buildroot.
Have a coffe or take a break.
Buildroot will download necessary packages.
If the build ends with an error you can try doing a 'make O=${PWD} -C ../myLinux clean' before the 'make O=${PWD} -C ../myLinux '
This will clear out any previous builds but also set you back on the build time.

Once the build is done, take a look in the output/images directory.
The images ha increased since the previous session.

Again we will use Qemu to try out our build and we need to add network capability.
We set it up to redirect port 22 (ssh) to our hosts local port 8022 and the same thing is done for port 80 (http) to local host port 8080.
The root filesystem is used as an init ram disk as before.
'kvm -kernel images/bzImage -initrd images/rootfs.cpio.bz2 -net nic -net user -redir tcp:8022::22 -redir tcp:8080::80'
Note that the search path has changed since we are now compiling while standing in "myWebserver" and we've told make to use that directory as output destination.

Dropbear will create a new certificate for ssh.
Since we are running this as a init ram disk we don't save anything and a new certificate needs to be created each time we restart.
We will make the certificate permanent later.

Log on to the machine as 'root' with no password.

Typing 'ifconfig' we can see that there are no network interfaces configured yet.
To activate it we need to activate eth0 and start a dhcp client:
ifconfig eth0 up
udhcpc eth0



To make this configuration automatic we add the following line to the file <buildroot>/output/target/etc/init.d/S40network under the 'start)' section.
'ifconfig eth0 up'
'udhcpc eth0'
The next time we rebuild our root filesystem this modification will be included and run at start up of the OS.

To be able to log on using ssh we need to set a password for root also:
passwd
Since this is just an experiment, set a short and simple password.

The web server Lighttpd comes with a configuration file which will suit us just fine for now.
We start it up:
lighttpd /etc/lighttpd/lighttpd.cfg

Check what ports are now open: netstat -ntul
# netstat -ntul
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN


We can see that port 22 (ssh) and port 80 (http) are open.

It is now possible to reach the target through ssh from our host:
ssh -p 8022 root@localhost

The webserver is configured to service files from a directory specified in the configuration file.
I this case it is /var/www.
We create the file /var/www/index.html and write the following to it:
<html>
<body>
Hello world!!
</body>
</html>

This file can also be added in our target directory on our host, myWebserver/target/var/www/.

We test the server with a web client (lynx for instance) to see if it works:
lynx http://localhost:8080


Now you can hack away on your own web server.