今天给大家分享一款适合嵌入式系统的轻量级Web服务器:BOA,并手把手教大家移植。
关于BOA
BOA是一款适合嵌入式系统的轻量级Web服务器,它支持开放源代码、具备高性能,并且兼容CGI(通用网关接口)技术。BOA的核心功能是促进嵌入式设备之间的信息交流,实现对这些设备的网络监控,并自动将数据反馈给主控设备。
这个服务器基于HTTP(超文本传输协议),其中Web页面是传输信息的基本单位。在客户端/服务器模型的基础上,BOA作为服务器与客户端的Web浏览器交互。浏览器首先与BOA服务器建立连接,打开一个套接字,这标志着SOCKET连接已成功建立。随后,浏览器通过这个套接字使用GET或POST方法向服务器发送请求,这些请求通过HTTP协议传递。
服务器收到请求后,会根据请求类型进行处理,可能是返回HTML文件,或者通过CGI调用外部程序来处理请求并返回结果。CGI允许服务器与外部应用程序和脚本进行交互,服务器会收集浏览器提供的信息,并将其传递给相应的CGI程序进行处理。处理完毕后,服务器会分析结果并将其发送回客户端,以便在浏览器中显示。这样,BOA服务器就能够有效地在嵌入式设备和控制系统之间传递和显示信息。
BOA下载与修改
下面展示如何移植Boa轻量级Web服务器到开发板端。
进入Boa官方网址获取Boa源码:
下载压缩包后,将其传入Ubuntu中,用于交叉编译至开发板端运行。
1. 解压Boa压缩包。
tar -xvf boa-0.94.13.tar.gz
cd boa-0.94.13/
2. 安装依赖包。
sudo apt-get update
sudo apt-get install byacc
sudo apt-get install flex
3. 进入src源码目录配置Mafile。
ubuntu@ubuntu2004:~/boa-0.94.13$ cd src/ubuntu@ubuntu2004:~/boa-0.94.13/src$ ./configure
4. 修改源码。
修改 compat.h:
vi osrc/compat.h +120
将源码中的
#define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff
修改为:
define TIMEZONE_OFFSET(foo) (foo)->tm_gmtoff
修改boa.c:
vi osrc/boa.c +255
修改源码为:
#if 0
if (setuid(0) != -1) {
DIE("icky Linux kernel bug!");
}
#endif
修改log.c:
vi osrc/log.c +73
将原来的程序
/* redirect stderr to error_log */
if (dup2(error_log, STDERR_FILENO) == -1) {
DIE("unable to dup2 the error log");
}
注释或者使用if 0不执行改程序,如下所示:
#if 0
/* redirect stderr to error_log */
if (dup2(error_log, STDERR_FILENO) == -1) {
DIE("unable to dup2 the error log");
}
#endif
5. 编译源码。
在src源码目录下执行编译make:
ubuntu@ubuntu2004:~/boa-0.94.13/src$ make
编译完成后会得到可执行程序boa和boa\_indexer。
修改安装目录
1. 进入源码目录中。
ubuntu@ubuntu2004:~/boa-0.94.13$ cd srcubuntu@ubuntu2004:~/boa-0.94.13/src$
2. 在源码src目录下,创建boa安装目录。
sudo mkdir -p /boa /boa/www /boa/cgi-bin /boa/log
3. 修改defines.h文件中的SERVER\_ROOT,使其指向改动后的配置文件路径。
vim defines.h +30
修改源码为:
#define SERVER_ROOT "/boa"
4. 修改boa配置文件。
在boa-0.94.13目录下修改:
ubuntu@ubuntu2004:~/boa-0.94.13$ vim boa.conf
将其中的内容修改为:
Port 80
User 0
Group 0
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/
5. 复制必要的文件到安装目录。
在boa-0.94.13目录下boa.conf配置文件至/boa/目录下。
ubuntu@ubuntu2004:~/boa-0.94.13$ sudo cp boa.conf /boa/
在源码目录下复制boa\_indexer至/boa/目录下。
ubuntu@ubuntu2004:~/boa-0.94.13/src$ sudo cp boa_indexer /boa/
复制/etc/mime.types至/boa/目录下。
ubuntu@ubuntu2004:~/boa-0.94.13/src$ sudo cp /etc/mime.types /boa/
在源码目录下复制boa至/boa/目录下。
ubuntu@ubuntu2004:~/boa-0.94.13/src$ sudo cp boa /boa/
复制完成后可以在/boa/看到对应的可执行程序和配置文件。
实现HTML页面文件
1. 在www目录下创建html文件。
ubuntu@ubuntu2004:~$ cd /boa/www/
ubuntu@ubuntu2004:/boa/www$ sudo touch index.html
2. 编辑index.html文件。
ubuntu@ubuntu2004:/boa/www$ sudo vim index.html
将下面的内容填入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>
这个程序会读取当前目录下的image.jpg。
3. 我们需要找一张JPG图片放在/boa/www目录下,如下所示:
ubuntu@ubuntu2004:/boa/www$ ls
image.jpg index.html
4. 编写测试程序,进入/boa/cgi-bin/目录下创建test.c测试程序。
ubuntu@ubuntu2004:/boa/cgi-bin$ sudo touch test.c
ubuntu@ubuntu2004:/boa/cgi-bin$ sudo 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;
}
5. 编译测试程序。
ubuntu@ubuntu2004:/boa/cgi-bin$ sudo gcc -o test.cgi test.c
编译完成之后会得到可执行程序test.cgi。
6. 执行/boa/目录下boa可执行程序。
在执行程序前,需要修改boa.conf配置文件,将下面程序注释掉。
# AccessLog: The location of the access log file. If this does not
运行boa程序。
sudo ./boa
7. 使用浏览器打开服务器。
返回用户空间中的home目录,使用火狐浏览器打开127.0.0.1网址,即可看到加载的图片。
ubuntu@ubuntu2004:/boa/cgi-bin$ cd
ubuntu@ubuntu2004:~$ firefox
上机实验
1. 在boa-0.94.13/src目录下执行程序清理。
make clean
清理完成后可以看到,可执行文件被删除。
2. 修改Makefile使用交叉编译工具链进行编译。
vi Makefile
注释掉原来的gcc工具链,新增CPP工具链。
CPP = $(CC) -E -static
使用静态链接编译:
$(CC) -o $@ $^ $(LDFLAGS) $(LIBS) -static
$(CC) -o $@ $^ $(LDFLAGS) $(LIBS) -static
修改完成后如下所示:
3. 编译:
① 在boa-0.94.13/src目录下配置环境并编译可执程序。
source /opt/remi-sdk/environment-setup-aarch64-poky-linux
make
编译完成后可查看对应的可执行程序的可用的平台。
file boa
file boa_indexer
② 将可执行文件拷贝到安装目录。
在拷贝前线杀掉后台运行boa进行,可查看前面运行boa、成功后的打印信息,运行成功后会返回PID值,假设PID值为49153:
sudo kill -9 49153
拷贝两个可执行程序到/boa目录下。
ubuntu@ubuntu2004:~/boa-0.94.13/src$ sudo cp boa /boa/
ubuntu@ubuntu2004:~/boa-0.94.13/src$ sudo cp boa_indexer /boa/
③ 编译测试程序。
由于无法在根目录下进行交叉编译,所以需要将test.c拷贝到用户空间编译。拷贝test.c到home目录下:
ubuntu@ubuntu2004:/boa/cgi-bin$ sudo cp test.c ~/
ubuntu@ubuntu2004:/boa/cgi-bin$ cd ~
在编译前需要配置环境变量,配置完成后:
ubuntu@ubuntu2004:~$ source /opt/remi-sdk/environment-setup-aarch64-poky-linux
ubuntu@ubuntu2004:~$ $CC test.c -o test.cgi
编译完成后可得到交叉编译后的可执行程序,将可执行程序拷贝回安装目录。
ubuntu@ubuntu2004:~$ sudo cp test.cgi /boa/cgi-bin/
④ 将根目录中的整个/boa文件夹上传到开发板端。
假设设置开发板的IP为:192.168.5.9,上传程序到开发板上。
sudo scp -r /boa/ root@192.168.5.9:/mnt/
测试:
进入/mnt目录运行程序:
root@myir-remi-1g:~# cd /mnt/
root@myir-remi-1g:/mnt# ls
boa/
将/mnt/目录下的boa文件夹拷贝到开发板根目录下。
root@myir-remi-1g:/mnt# cp boa/ -rf /
修改boa.conf配置文件中的端口号。
root@myir-remi-1g:/mnt# vi /boa/boa.conf
修改文件中的端口号Port为8080,如下所示:
Port 8080
执行boa程序运行Web服务器:
root@myir-remi-1g:/mnt# cd /boa
root@myir-remi-1g:/boa# ./boa
[01/Jan/2066:02:07:10 +0000] boa: server version Boa/0.94.13
[01/Jan/2066:02:07:10 +0000] boa: server built Apr 30 2024 at 02:17:37.
[01/Jan/2066:02:07:10 +0000] boa: starting server pid=610, port 8080
运行成功后,可用在Ubuntu端使用浏览器打开的网址,假设开发板的IP为192.168.5.9,那么Ubuntu端打开的网址为:
http://192.168.5.9:8080
注意:在访问web服务器前,请确保开发板使用网线连接到电脑端/路由器,并成功设置IP。
END
来源:strongerHuang
推荐阅读
欢迎大家点赞留言,更多 Arm 技术文章动态请关注极术社区嵌入式客栈专栏欢迎添加极术小姐姐微信(id:aijishu20)加入技术交流群,请备注研究方向。