Using the Boa Server in Embedded Linux to implement cgi/HTML Web Access)

Source: Internet
Author: User
Tags small web server

 

Introduction to CGI/HTML Web access through the Boa Server in Embedded Linux:

Step 1: port the boa Program

1. Download Boa Source Code

2. Generate the MAKEFILE file

3. Modify the MAKEFILE file

4. Compile

Step 2: Configure Boa

Step 3: Boa execution

1. Mount the root file system ramdisk to the target directory of Mount-o loop ramdisk on the Computer Host.

2. Test operations

3. repackage and compress the ramdisk file and download it to the Embedded Board.

4. cgi script Test

Introduction

With the rise of internet technology, in the management and interaction of embedded devices, Web-based applications have become the mainstream. This program structure is the B/S structure that everyone is very familiar, that is, running a web server supporting scripts or CGI on an embedded device can generate dynamic pages. On the user end, you only need to use a web browser to manage and monitor embedded devices, it is very convenient and practical. This section describes the development and Transplantation of such applications.

Embedded Web Server transplantation because embedded device resources are generally limited and do not need to be able to process requests from many users at the same time, it does not use the most common Linux servers such as Apache, some Web servers specially designed for embedded devices are required. These web servers are suitable for embedded applications in the storage space and the memory space occupied by the operation.

Typical embedded web servers include BOA (http://www.boa.org/) and thttpd (http://www.acme.com/software/thttpd/). The main difference between them and high-performance Web servers such as Apache is that they are generally single-process servers, only after a user request is completed can the request of another user be responded without concurrent response, but this is sufficient for embedded devices.

Boa is a very small Web server with only about 60 kb of executable code. It is a single-task web server that can only complete user requests in sequence, rather than fork new processes to process concurrent connection requests. However, BOA supports CGI and can execute a process for the CGI program fork. BOA's goal is speed and security. In performance tests published on its site, BOA's performance is better than that of Apache servers.

Note: The following transplantation and operation are performed on the at91rm9200 chip linux2.4.27.

 

Step 1: port the boa Program

This step is completed on the Computer Host.

For uClinux-Dist, make menuconfig is in the Application menu, select BOA under network application, and then execute make compilation in the uClinux-Dist directory.

For platforms with MMU, such as armlinux and ppclinux, you can download a boa tar package from the Internet and find a mainstream available version, either the latest or the oldest version. Download it to the RedHat host, decompress it to any directory, and modify the compiler in Boa/src/makefile by adding two or three lines. perform the following steps:

1. Download Boa Source Code

: Http://www.boa.org/, or http://sourceforge.net/

Latest release: 0.94.13

Download the boa-0.94.13.tar.gz, note: From the boa download is boa-0.94.13.tar.tar, the decompression method is the same

Unzip: # tar xzf boa-0.94.13.tar.gz

2. Generate the MAKEFILE file

The boa-0.94.13.tar.gz decompress and generate the BOA-0.94.13 directory to directly run the./Computer Host directory/BOA-0.94.13/src/configure file

3. Modify the MAKEFILE file

A. Modify cc = gcc

Cc =/usr/local/ARM/2.95.3/bin/ARM-Linux-GCC (depending on the compiler location of your computer host)

B. Modify CPP = gcc-e

For CPP =/usr/local/ARM/2.95.3/bin/ARM-Linux-gcc-e

Note: Boa-0.94.13 fails to be compiled using the 3.3.2 and 3.4.1 compilers. No specific research.

4. Compile

Go to:/Computer Host directory/BOA-0.94.13/src directory

# Make

#/Usr/local/ARM/2.95.3/bin/ARM-Linux-strip BOA (this is to remove the compilation information generated in Boa and reduce the file size)

Step 2: Configure Boa

This step is also done on the Computer Host.

There is an example boa. conf In The Boa Source Code BOA-0.94.13 directory on the Computer Host, which can be modified on the basis. As follows:

1. Group Modification

Modify group nogroup

Group 0

Since there is no nogroup group in the/etc/group file, set it to 0. In addition, there is a nobody user in/etc/passwd, so the user nobody does not need to be modified.

2. Modify ScriptAlias

Modify ScriptAlias/cgi-bin // usr/lib/cgi-bin/

It is ScriptAlias/cgi-bin/var/www/cgi-bin/

3. servername settings

Modify servername www.linuxidc.com

Servername www.linuxidc.com

Note: This option is not enabled by default. If boa is executed, it exits abnormally and the message "gethostbyname: no such file or directory" is displayed. Therefore, this option must be enabled. Other default settings.

Note: boa. conf explains the meaning of the file below:

# The listening port number. The default value is 80, which generally does not need to be modified.

Port 80

# The IP address called by BIND, which is usually commented out, indicates that it is bound to inaddr_any and is assigned to all the IP addresses of the server.

# Listen 192.68.0.5

# Which user is running, that is, it has the permissions of this user. Generally, it is nobody and must have the permissions in/etc/passwd.

# Nobody user

User nobody

# Which user group is used for running? That is, it has the permissions of this user group. Generally, it is a nogroup and must be in the/etc/group file.

# There are nogroup groups in the parts

Group nogroup

# The email address that sends an alarm when an error occurs on the server. It is not used currently and is commented out.

# Serveradmin root @ localhost

# Error log file. If it does not start with "/", it indicates starting from the root path of the server. If no error log is required,

#/Dev/null is used. Note that the/var/log/BOA directory must be created in the following settings.

Errorlog/var/log/BOA/error_log

# Access log files. If it does not start with "/", it indicates starting from the root path of the server. If no error log is required,

Use #/dev/null or comment out directly. Note that the/var/log/BOA directory must be created in the following settings.

# Accesslog/var/log/BOA/access_log

# Whether to use the local time. If not commented out, the local time is used. The UTC time is used for comment-out.

# Uselocaltime

# Whether to record CGI running information. If it is not commented out, it is recorded. If it is commented out, it is not recorded.

# Verbosecgilogs

# Server name

Servername http://www.linuxidc.com

# Whether to enable the VM function, that is, the device can have multiple network interfaces, and each interface can have a virtual Web Server

# Server. Usually comment out, that is, you do not need to start

# Virtualhost

# The main directory of the HTML document. If it does not start with "/", it indicates starting from the root path of the server.

DocumentRoot/var/WWW

# If you receive a user request, add the directory name after the user's main directory

Userdir public_html

# The file name of the HTML Directory Index, that is, the file name returned when no user only specifies the Access Directory

Directoryindex index.html

# When the HTML directory does not have an index file, BOA will call this program to generate an index file and

# Return to the user, because the process is slow, it is best not to execute, you can comment out or add the # file specified by directoryindex to each HTML directory

# Directorymaker/usr/lib/BOA/boa_indexer

# If directoryindex does not exist and directorymaker is annotated, use the built-in BOA index.

# Generate a program to generate the index file of the Directory and output it to the following directory. The directory must be read and written by BOA.

# Directorycache/var/spool/BOA/dircache

# The maximum number of HTTP sustained requests allowed by a connection. If it is commented out or set to 0, HTTP sustained effect is disabled.

Keepalivemax 1000

# The number of waiting times of the server between two requests during HTTP persistence. In seconds, the connection will be closed when the request times out.

Keepalivetimeout 10

# Specify the location of the mime. types file. If it does not start with "/", it indicates starting from the root path of the server. Comment out

# Avoid using the mime. types file. In this case, use addtype to specify

Mimetypes/etc/mime. Types

# The default MIME type used when there is no or unknown File Extension

Defaulttype text/plain

# Provide the PATH environment variable value of the CGI program

Cgipath/bin:/usr/local/bin

# Associate the file extension with the MIME type, which is the same as the mime. types file. If mime. types is used

# File. If the mime. types file is not used, you must use

# Addtype application/X-httpd-CGI

# Specify the document redirection path

# Redirect/bar http: // elsewhere/Feh/Bar

# Add an alias to a path

Alias/doc/usr/doc

# It is very important to specify the actual path corresponding to the virtual path of the CGI script. Generally, all CGI scripts must be placed in the actual path.

# Enter the site + virtual path + CGI script name when the user accesses the execution

ScriptAlias/cgi-bin // var/www/cgi-bin/

 

Step 3: Boa execution

This step is also done on the Computer Host. However, the operated directory is related to the directory on the embedded board.

1. Mount the root file system ramdisk to the target directory of Mount-o loop ramdisk on the Computer Host.

Go to the ramdisk root folder after expansion, and then execute the following:

# Mkdir/var/log

# Mkdir/var/log/BOA (create a log file directory)

# Mkdir/var/WWW (create the main directory of the HTML document)

# Mkdir/etc/Boa

Copy the modified boa. conf file from another directory on the computer host to the related directory of ramdsik.

# Cp/Computer Host directory/BOA-0.94.13/boa. CONF/etc/BOA/

Copy the mime. types file to the/etc directory of the embedded board ramdsik. Generally, mime. types can be copied directly from the/etc directory of the Linux host.

# Cp/etc/mime. Type/etc/

Copy the boa executable file compiled in the first step to the bin directory of ramdsik.

# Cp/Computer Host directory/BOA-0.94.13/src/BOA/bin/

2. Test operations

You can set the index.html under the host/usr/share/doc/html/directory (this is a static html webpage display file, which is the welcome homepage of redhat9. When you enter http: // The default static webpage is displayed when the IP address of the embedded board is displayed. copy the file and IMG directory to the/var/WWW directory of the embedded ramdsik.

# Cp/usr/share/doc/html/index.html/var/www/

# Cp/usr/share/doc/html/img/var/www/

# Cp/usr/share/doc/html/index-ZH_CH.html/var/www/

3. repackage and compress the ramdisk file and download it to the Embedded Board.

After downloading the ramdisk package and downloading it to the sub-board, enter./BOA in the root directory of the sub-terminal to check whether BOA can be executed.

An error may be prompted, such as libso.0.6 glib2.2. This is generally because the boa needs to dynamically share the library during execution. The solution is to compile BOA in static mode, that is

Go to the/Computer Host directory/BOA-0.94.13/src directory and modify the MAKEFILE file:

Ldflags =-G

Change it to ldflags =-static.

When running BOA, other errors may occur. You can find the cause in the/var/log/BOA/error_log file of the Development Board. Most of them are related to the correctness of the BOA. conf configuration file. For example:

Problem: An error occurred while executing./BOA: log. C: 73 unable to dup2 the Error Log: Bad file descriptor.

Solution: comment out the if statement in log. C.

Another problem occurs: Unable to dup2 the Error Log: Bad file descriptor

Solution: Comment out # accesslog/var/log/BOA/access_log in Boa. conf.

4. cgi script Test

Write the following test. C test program,

# Include <stdio. h>

# Include <string. h>

Int main (void)

{

Printf ("contenttype: text/html \ n ");

Printf ("<HTML> \ n ");

Printf ("

Printf ("<body> <br> \ n ");

Printf ("<H2> This is an HTML page generated from with in a CGI program... </H2> \ n ");

Printf ("<HR> <p> \ n ");

Printf ("<a href = \" output.html # Two \ "> <B> go back to out put.html page </B> </a> \ n ");

Printf ("</body> \ n ");

Printf ("

Fflush (stdout );

}

Generate Test. cgi

#/Usr/local/ARM/2.95.3/bin/ARM-Linux-gcc-O test. cgi test. c

Set test. copy CGI to the/var/www/cgi-bin/directory of the embedded board. When you enter http: // ip address of the embedded board/cgi-bin/test on IE of the lan pc. CGI

The web page of test. cgi is displayed.

You can learn about CGI by using C language.

Note: When the code above is executed, an error message may appear:

502 Bad Gateway

The CGI was not cgi/1.1 compliant.

Possible causes:

1. test whether the static web page can be viewed to ensure that the Network is correct.

In the above "Step 3 2. after the test operation, enter the http: // Embedded Board IP address on the IE of the lan pc. If the redhat9 welcome page is displayed, the static access is correct.

2. After copying the. cgi file to the target board, you must change its permissions.

Chmod 755 test. cgi

Otherwise, the upper computer will prompt the above error during browsing

3. The most likely problem is the CGI program itself.

We have seen that the print of test. c starts with the MIME header information "content type: text/html \ n" to output the HTML source code to the web server. Note that there must be an empty line after any MIME header information, that is, \ n,

Therefore, this must be ensured.

4. The original file of test. C is edited in text mode and cannot be executed in Linux.

Can be changed: dos2unix test. C is modified before compilation.

5. another possibility is test. CGI is compiled into a dynamic form. However, the dynamic library file cannot be found in the embedded system during program execution, so-static is added during program compilation and compiled into a static form, however, this method is not suitable for embedded systems because the compiled files are large.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.