本测试在ubuntu1304下测试,具体步骤如下:
1下载源码:www.boa.org,可在ubuntu下自带的火狐浏览器下载,也可在window下下载,然后再移到ubuntu下;
2打开终端,将boa解压到某目录并进入当前源码目录
tar xvzf boa-*
cd /boa-0.94.13/src
3配置 ./configure
4编译
make
出现:yacc -d boa_grammar.y
make: yacc: Command not found
make: *** [y.tab.c] Error 127
分析:yacc是一个文法分析器的生成器,bison即是yacc的GNU版本.Lex和YACC是用于构造词法分析机和语法解释器的工具,利用Lex和
YACC你可以轻松的构造一个语法解释器。
解决:apt-get install bison
再make
出现:lex boa_lexer.l
make: lex: Command not found
make: *** [lex.yy.c] Error 127
解决:apt-get install flex
再make
出现:util.c:100:1: error: pasting "t" and "->" does not give a valid preprocessing token
make: *** [util.o] Error 1
解决: 修改 src/compat.h
找到 #define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff
修改成 #define TIMEZONE_OFFSET(foo) (foo)->tm_gmtoff
再make,编译通过,在当前目录生成一boa可执行程序
5 复制boa.conf到/etc/boa目录下,如果没有这个目录,自己手动创建 : sudo mkdir /etc/boa
因为在defines.h文件中,
当然也可修改此设置,在此选择默认。
6 执行./boa
出现: log.c:73 unable to dup2 the error log: Bad file descriptor
解决:注释掉:
if(dup2(error_log, STDERR_FILENO) == -1)
{
DIE("unable to dup2 the error log");
}
执行make,
再次执行./boa
出现:Cannot open /var/log/boa/access_log for logging: logfile
open: No such file or directory,
或者:Cannot open/var/log/boa/error_log for logging: logfile
open: No such file or directory
解决:
哪个出现,你就在boa.conf里把它给注释掉(不要忘了/etc/boa/目录下的boa.conf)
即改成:
#AccessLog /var/log/boa/access_log
再次执行./boa
此时boa服务器就已经启动!
测试1:
将index.html文件存放在/var/www/下(如果没有,则创建)
打开浏览器,输入ubuntu下的IP地址,则显示如下结果:
实例代码1:
index.html
<html>
<head><title>boa web test page</title></head>
<body bgcolor=#666666>
<p><hr/></p>
<p><center>my boa main page</center></p)
<p><hr/></p>
<p>Boa is a tiny web server that also offers extremely high performance.
It is specifically designed to run on UNIX-like systems, which includes Linux, as well as the *BSD systems. To get all the legal stuff out of the way, Boa is available for free and is covered by the GNU Public License (GPL).The source code for Boa, as well as documentation, can be found at http://www.boa.org/.</p>
<p><hr/></p> </body>
</html>
测试2:
将自己制作的网页(hello.html)放到/var/www/目录中,将cgi程序放到/var/www/cgi-bin目录下,在浏览器中输入http://192.168.182.153/hello.html,这里的IP地址就是Linux的IP地址。则出现如下结果:
实例代码2:
hello.html
<html>
<head>
<title>this is my first HTML page</title>
</head> <body>
<center><p><h1>this is is a simple test of <I>HTML</I></h1></p></center>
<center><img src=../../"img/logo.jpg" alt="CST studio" />
hello from <a href=../../"http://www.latelee.org"><b>Late Lee</b></a></center> <br /> <center><p>you can visit my website at <a href = "http://www.latelee.org">www.latelee.org</a></p><center>
<hr /> <!-- a test -->
<form name="form1" action="/cgi-bin/hello.cgi" method="post">
<table align="center">
<tr><td align="center" colspan="2"></td></tr>
<tr>
<td align="center">user name</td>
<td><input type="text" name="Username"></td>
</tr> <tr>
<td align="center">password</td>
<td><input type="password" name="Password"></td>
</tr> <tr>
<td><input type="submit" value="login"></td>
<td><input type="reset" value="cancel"></td>
</tr>
</table>
</form>
</body> </html>
hello.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h> char* get_cgi_data(FILE* fp, char* method)
{
char* input;
int len;
int size=;
int i=; if (strcmp(method, "GET") == ) /**< GET method */
{
input = getenv("QUERY_STRING");
return input;
} else if (strcmp(method, "POST") == ) /**< POST method */
{
len = atoi(getenv("CONTENT_LENGTH"));
input = (char*)malloc(sizeof(char) * (size+)); if (len == )
{
input[] = ' ';
return input;
} while ()
{
input[i] = (char)fgetc(fp);
if (i == size)
{
input[i+] = ' ';
return input;
}
--len; if (feof(fp) || (!(len)))
{
i++;
input[i] = ' ';
return input;
}
i++;
}
}
return NULL;
} int main(void)
{
char* input;
char* method;
char name[];
char passwd[];
int i=;
int j=; printf("Content-type:text/htmlnn");
printf("The following is query result:");
method = getenv("REQUEST_METHOD");
input = get_cgi_data(stdin, method); printf("string is: %s", input);
int len = strlen(input);
/* sizeof("username=") = 9 */
for (i=; i<len; i++)
{
if (input[i] == '&')
{
name[j]=' ';
break;
} name[j++]=input[i];
} /* sizeof("username=")+sizeof(&password=) = 19 */
for (i=+strlen(name),j=; i<(int)strlen(input); i++)
{
passwd[j++] = input[i];
}
passwd[j]=' '; printf("your username is %s your password is %sn", name, passwd); return ;
}
参考文献: