1、编译boa
1.1 设置编译环境
boa 的官方网站是:www.boa.org,下载地址:
https://sourceforge.net/project/showfiles.php?group_id=78,下载的版本为:boa-0.94.13 版。
#tar xvfz boa-0.94.13.tar.gz -C /opt/EmbedSky/
1.2 配置编译条件
配置 boa:
#cd /opt/EmbedSky/boa-0.94.13/src
#./configure
会在 boa-0.94.13/src 目录下面生成 Makefile 文件,修改 Makefile 文件:
#vi Makefile
1)在 31 行和 32 行,找到:CC = gcc 和 CPP = gcc -E,然后改成:CC = mipsel-linux-gcc 和 CPP=mipsel-linux-gcc -E,保存退出。
2)相关文件的修改:
a. 修改compat.h
#define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff
将其修改成
#define TIMEZONE_OFFSET(foo) (foo)->tm_gmtoff
否则会出现错误:
util.c:100:1: error: pasting "t" and "->" does not give a valid preprocessing token make: *** [util.o]
b. 修改log.c
将以下语句注释掉:
if (dup2(error_log, STDERR_FILENO) == -1) {
DIE("unable to dup2 the error log");
}
否则会出现错误:
log.c:73 unable to dup2 the error log:bad file deor
c. 修改boa.c(步骤6)
注释掉下面两句话:
if (passwdbuf == NULL) {
DIE(”getpwuid”);
}
if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) {
DIE(”initgroups”);
}
否则会出现错误:boa.c:211 - getpwuid: No such file or directory
注释掉下面语句:
if (setuid(0) != -1) {
DIE(”icky Linux kernel bug!”);
}
否则会出现问题:boa.c:228 - icky Linux kernel bug!: No such file or directory
生成可执行文件BOA:
首先进入src目录,并执行configure文件,生成Makefile文件
#cd /source/boa-0.94.13/src
# make(步骤9)
对生成文件进程瘦身
#mipsel-linux-strip boa(步骤10)
2、配置 Web 服务器
主要修改 boa.conf 文件
User 0 //行48
Group 0 //行49
//作为哪个用户组运行,即它拥有该用户组的权限,一般都是root,需要在/etc/group文件中有root组。
ErrorLog /dev/console //行62
//错误日志文件。如果没有以/xxx开始,则表示从服务器的根路径开始。如果不需要错误日志,则用
/dev/null。系统启动后看到的boa的打印信息就是由/dev/console得到的。
AccessLog /dev/null //行75
//访问日志文件。如果没有以/xxx开始,则表示从服务器的根路径开始。如果不需要错误日志,则用
/dev/null或直接注释掉。
DocumentRoot /www //行112
//非常重要,这个就是存放HTML文档的主目录。如果没有以/xxx开始,则表示从服务器的根路径开
始。
ScriptAlias /cgi-bin/ /www/cgi-bin/ //行194
//非常重要,指明CGI脚本的虚拟路径对应的实际路径。一般所有的CGI脚本都要放在实际路径里,
用户访问执行时输入站点+虚拟路径+CGI脚本名。前面的/cgi-bin/就是虚拟路径,/web/cgi-bin/就是
实际的路径。
3、测试
(1)先进入开发板的根目录,创建www目录,在进入www目录创建cgi-bin目录,用来放置cgic测试文件。
(2)进入etc目录,创建boa目录。通过nfs将boa 和 boa.conf文件拷到该目录下。
(3)将linux系统下得/etc/mime.types拷贝到开发板根文件系统的/etc下,mime.types的主要功能是控制从网络上发给宿主机端的媒体文件的类型,并且告诉宿主机怎么去处理这些数据。
(4)启动boa :./boa &
(5)CGI 脚本测试
使用 helloweb.c 进行测试。
helloweb.c 源码如下:
#include <stdio.h>
main()
{
printf("Content-type: text/html\n\n");
printf("<html>\n");
printf("<head><title>CGI Output</title></head>\n");
printf("<body>\n");
printf("<hl>Hello, Web Server.</hl>\n");
printf("<body>\n");
printf("</html>\n");
exit(0);
}
编译:
#mipsel-linux-gcc -o helloweb.cgi helloweb.c
#cp /mnt/helloweb.cgi /www/cgi-bin
然后在 PC 的网页浏览器中输入:
http://xx.xx.xx.xx/cgi-bin/helloweb.cgi 即可打开测试页面。
注意:
用cgi程序进行交互时,浏览器端出现了如下的错误信息:502 Bad Gateway
The CGI was not CGI/1.1 compliant
在终端上还会显示这样的提示信息:
cgi_header:unable to find LFIF
这样的错误一般是格式的问题,我写的头是
printf("Content-type: text/html");
正确的应该是:
printf("Content- type: text/html\n\n");
也就是后面有两个空行,这也是默认的格式,必须这样写才行,一定要注意哦。
如果还是错误时,尝试以下方法:
1,修改boa代码cgi.c,在 complete_env 函数中添加环境变量
my_add_cgi_env(req, "LD_LIBRARY_PATH", "/lib"),然后重新make即可;
2,编译时加入一个参数,具体如下:
gcc -static test.c -o test.cgi
3,有时原因可能是出在 req->post_data_fd上,在cgi.c的ini_cgi()中,在進行fork()之後,父行程會將 req->post_data_fd开关必設為零,在別的系统中这不會影响到子行程,不知道为什么在uclinux下就会出问题,试试吧
if (req->method == M_POST)
{
close(req->post_data_fd); /* child closed it too */
req->post_data_fd = 0;
}
裡的req->post_data_fd = 0,註解掉看看;