写在最前面

华清远见教育集团
15年专注高端IT培训
做良心教育,做专业教育,做受人尊敬的职业教育

创客学院官网:http://www.makeru.com.cn/
华清远见创客学院嵌入式课程链接:http://www.makeru.com.cn/emb
华清远见创客学院物联网课程链接:http://www.makeru.com.cn/iot

 

 

概述

BOA是一种非常小巧的web服务器,用来负责处理客户端或者是浏览器端的http请求,因为其特性小巧,性能优秀,故而适合应用于嵌入式系统。

 

如图,用户通过网页客户端(浏览器)实现对服务端(嵌入式系统)的查询访问和下发数据命令。

其中,A9开发板+CGI+BOA构成服务端,浏览器或者应用程序够成客户端。这有点类似与web开发中的B/S架构设计思想。。

 

BOA简介


    其可执行代码只有大约60KB左右,Boa是一个单任务的HTTP服务器,Boa只能依次完成用户的请求,而不会fork出新的进程来处理并发连接请求。Boa支持CGI。
Boa的设计目标是速度和安全。(CGI只是一个进程,用来提供接口),自动目录生成和自动文件枪支进行拼接。
Boa的主要设计目标是速度和安全性。安全性在“不能被恶意用户破坏”的意义上,不是“细粒度访问控制和加密通信”。

特点:可靠性和可移植性,Boa不是作为功能强大的服务器。
开发平台:GNU / Linux是目前的开发平台。
 

 

在linux中查看进程 - 根据进程PID号终止进程

boa在linux系统中一守护进程的方式存在,如果要终止boa进程,通过以下方法

root@linux:src# ps -axj | grep "boa"
     2102 24862 24860 24255 pts/18   24255 S    65534   0:00 ./boa
     2102 25735 25733 24255 pts/18   24255 S    65534   0:00 ./boa
    25753 25793 25792 25753 pts/4    25792 S+    1000   0:00 grep 

root@linux:src# kill -9 24862
root@linux:src# kill -9 25733

 

 

BOA移植过程

 

在Linux(pc端)上进行移植

1.下载boa源码

http://www.boa.org/

2.解压文件,并进入./boa-0.94.13/src目录

tar -xvf boa-0.94.13.tar.gz
cd ./boa-0.94.13/src/

3. 执行./configure配置编译环境

root@linux:src# ./configure

4. make编译源码

root@linux:src# make 

出现错误:
        compat.h:120:30: note: in definition of macro ‘TIMEZONE_OFFSET’
         #define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff
                                      ^
        make: *** [util.o] Error 1
解决方案:

        root@linux:src# vim compat.h +120
        #define TIMEZONE_OFFSET(foo) (foo)->tm_gmtoff        //修改成这个
        //#define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff        //注释掉

        root@linux:src# vim boa.c +225
        225     #if 0     //注释掉
        226         if (setuid(0) != -1) {
        227             DIE("icky Linux kernel bug!");
        228         }
        229     #endif   


        root@linux:src# make clean
        root@linux:src# make 

5.创建boa安装目录 /boa

root@linux:src# mkdir -p /boa /boa/www /boa/cgi-bin /boa/log

6.修改defines.h文件中的SERVER_ROOT,使其指向改动后的配置文件路径

root@linux:src# cp boa /boa/
root@linux:boa-0.94.13# vim src/defines.h +30
         30 #define SERVER_ROOT "/boa"
         31 //#define SERVER_ROOT "/etc/boa"

7.复制必要的文件到安装目录

root@linux:boa-0.94.13# cp boa.conf /boa/
root@linux:src# cd ./src
root@linux:src# cp boa /boa/
root@linux:src# cp boa_indexer /boa/
root@linux:src# cp /etc/mime.types /boa/

8.修改boa配置文件

root@linux:src# cd /boa
root@linux:boa# ls
boa  boa.conf  boa_indexer  cgi-bin  log  mime.types  www
root@linux:boa# cp boa.conf boa.conf.back
root@linux:boa# cat boa.conf.back | grep -v "#" | grep -v "^$" > boa.conf
root@linux:boa# vim boa.conf


修改成:

        Port  80  
        User  0  
        Group 0  
        #Listen 192.68.0.5 
        ErrorLog /boa/log/error_log  
        AccessLog /boa/log/access_log    
        DocumentRoot /boa/www  
        UserDir public_html  
        DirectoryIndex index.html    
        DirectoryMaker /boa/boa_indexer
        KeepAliveMax 1000  
        KeepAliveTimeout 10  
        MimeTypes /boa/mime.types 
        DefaultType text/plain    
        CGIPath /bin:/usr/bin:/usr/local/bin   
        Alias /doc /usr/doc   
        ScriptAlias /cgi-bin/ /boa/cgi-bin/

9.实现HTML页面文件


(1)index.html

root@linux:boa# cd www
root@linux:www# touch index.html
root@linux:www# vim index.html 

<html>
        <body>
                <h3>this is a test!</h3><br/>
                        <img src="image.jpg"/>
                <h3>tree picture</h3><br/>

                <a href="/cgi-bin/test.cgi">to cgi page</a>
        </body>
</html>

(2)image.jpg  随便找一张图片,放在当前目录

root@linux:www# cp image.jpg /boa/www

test.cgi

root@linux:www# cd ..
root@linux:boa# ls
boa  boa.conf  boa.conf.back  boa_indexer  cgi-bin  log  mime.types  www
root@linux:boa# cd cgi-bin/
root@linux:cgi-bin# touch test.c
root@linux:cgi-bin# vim test.c 
#include <stdio.h>  
int main()  
{  
  printf("Content-type:text/html\n\n"); //这句一定要加上  
  printf("<html><body>");  
  printf("<font style=\"color:red; font-size:30px;\">Hello, CGI!</font><br/>");  
  printf("<a href=\"/index.html\">return index.html</a>");  
  printf("</body></html>");  
  return 0;  
} 
root@linux:cgi-bin# gcc -o test.cgi test.c
root@linux:cgi-bin# ls
test.c  test.cgi

10.验证


(1)查看目录

root@linux:boa# cd /boa/
root@linux:boa# tree
.
├── boa
├── boa.conf
├── boa.conf.back
├── boa_indexer
├── cgi-bin
│   └── test.cgi
├── log
├── mime.types
└── www
    └── index.html
3 directories, 7 files
root@linux:boa# 

查看效果

root@linux:boa# ./boa
root@linux:boa# firefox


可以查看到boa服务器给我们(浏览器)所展示的页面了。女神~~~

 

出现错误:
        root@linux:boa# ./boa
        Could not chdir to "/etc/boa": aborting
        root@linux:boa# 
解决方案:
        重新编译并将boa 等文件复制一下
        root@linux:src# cp boa /boa/
        root@linux:src# cp boa_indexer /boa

出现错误:
        root@linux:boa# ./boa
        gethostbyname:: Success
        root@linux:boa# 
解决方案:
        root@linux:boa# vim boa.conf
        添加如下行
        ServerName www.mydomain.com
出现错误:
        root@linux:log# cat /boa/log/error_log 
        [03/Dec/2018:08:44:10 +0000] boa.c:226 - icky Linux kernel bug!: Success
        [03/Dec/2018:08:46:04 +0000] boa.c:226 - icky Linux kernel bug!: Success
解决方案:
        root@linux:src# vim boa.c +225
        225     #if 0     //注释掉
        226         if (setuid(0) != -1) {
        227             DIE("icky Linux kernel bug!");
        228         }
        229     #endif  

        root@linux:src# make 
        重新编译并将boa 等文件复制一下
        root@linux:src# cp boa /boa/
        root@linux:src# cp boa_indexer /boa

 

 

 

移植到嵌入式开发板(FS4412)中

 

1.在源码编译的时候,指定交叉编译工具链


查看交叉编译工具链的类型
    <1>进入gcc安装目录

    root@linux:soft# cd gcc-4.6.4/
    root@linux:gcc-4.6.4# cd bin

    <2>查看选中的工具链(被标注了颜色)

    arm-none-linux-gnueabi-gdb

(2)Makefile中指定编译链

root@linux:src# pwd
/root/boa_test/boa-0.94.13/src
root@linux:src# vim Makefile

CC = arm-none-linux-gnueabi-gcc

2.编译目标文件  并cp到安装目录

root@linux:src# make clean
root@linux:src# make 
root@linux:src# cp boa /boa/
root@linux:src# cp boa_indexer /boa

同时,通过交叉编译工具将test.c进行编译

root@linux:cgi-bin# arm-none-linux-gnueabi-gcc -o test.cgi test.c
root@linux:cgi-bin# ls
test.c  test.cgi

3.将整个/boa目录复制到,nfs共享跟目录下面

root@linux:/# cp -rf boa/ /nfs/rootfs

4.在上位机中测试结果


浏览器访问目标机所在的IP

 

出现错误:
        root@linux:log# cat /boa/log/error_log 
        [01/Jan/1970:00:04:04 +0000] boa.c:211 - getpwuid: No such file or directory

解决方案:
        root@linux:src# vim boa.c +211
        210 #if 0
        211         if (passwdbuf == NULL) {
        212             DIE("getpwuid");
        213         }
        214         if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) {
        215             DIE("initgroups");
        216         }
        217 #endif


        root@linux:src# make clean
        root@linux:src# make 
        重新编译并将boa 等文件复制一下
        root@linux:src# cp boa /nfs/rootfs/boa/
        root@linux:src# cp boa_indexer /nfs/rootfs/boa

 

 

全部移植过程。呼~~   终于搞定了!