**目录**
一、环境搭建
二、相关配置(部分)
三、调试运行
四、测试源码参考
五、常见错误
六、扩展(CCGI,SQLite)


# 一、环境搭建
操作系统:Ubuntu12.04 LTS
boa下载地址(但是我找不到...): http://www.boa.org/
我是其他网站找到的资源,但是忘了网址了,所以我直接上云盘资源
链接: https://pan.baidu.com/s/1_SpR9MDcmSB8jpDm76fw6A 提取码: hb1e
可以参考:Ubuntu下boa服务器的配置与搭建
cgi:直接终端安装 sudo apt-get install apache2
可以参考: ubuntu 下搭建cgi环境

# 二、相关配置(部分)
boa我的配置:/etc/boa$ sudo vi boa.conf

# 下面几个都是关键点,基本就错这几个点上
# cumentRoot /var/www
#将cgi保存的实际位置和网站地址做个对应
# ScriptAlias /cgi-bin/ /var/www/cgi-bin/
#cgi脚本运行时能看到的$PATH(可选)
# CGIPath /bin:/usr/bin:/usr/local/bin
#如果想在任何位置都能运行cgi,要添加这个(可选)
AddType application/x-httpd-cgi cgi
# Boa v0. configuration file
# File format has not changed from 0.93
# File format has changed little from 0.92
# version changes are noted in the comments
#
# The Boa configuration file is parsed with a lex/yacc or flex/bison
# generated parser. If it reports an error, the line number will be
# provided; it should be easy to spot. The syntax of each of these
# rules is very simple, and they can occur in any order. Where possible
# these directives mimic those of NCSA httpd 1.3; I saw no reason to
# introduce gratuitous differences. # $Id: boa.conf,v 1.25 // :: jnelson Exp $
# The "ServerRoot" is not in this configuration file. It can be compiled
# into the server (see defines.h) or specified on the command line with
# the -c option, for example:
#
# boa -c /usr/local/boa # Port: The port Boa runs on. The default port for http servers is . # If it is less than , the server must be started as root.
# 端口老是被占用,所以我改掉了
Port # Listen: the Internet address to bind() to. If you leave it out,
# it takes the behavior before 0.93.17.2, which is to bind to all
# addresses (INADDR_ANY). You only get one "Listen" directive,
# if you want service on multiple IP addresses, you have three choices:
# . Run boa without a "Listen" directive
# a. All addresses are treated the same; makes sense if the addresses
# are localhost, ppp, and eth0.
# b. Use the VirtualHost directive below to point requests to different
# files. Should be good for a very large number of addresses (web
# hosting clients).
# . Run one copy of boa per IP address, each has its own configuration
# with a "Listen" directive. No big deal up to a few tens of addresses.
# Nice separation between clients.
# The name you provide gets run through inet_aton(), so you have to use dotted
# quad notation. This configuration is too important to trust some DNS. #Listen 192.68.0.5
# User: The name or UID the server should run as.
# Group: The group name or GID the server should run as. User
Group # ServerAdmin: The email address where server problems should be sent.
# Note: this is not currently used, except as an environment variable
# for CGIs.
#ServerAdmin root@localhost
# ErrorLog: The location of the error log file. If this does not start
# with /, it is considered relative to the server root.
# Set to /dev/null if you don't want errors logged.
# If unset, defaults to /dev/stderr ErrorLog /var/log/boa/error_log # Please NOTE: Sending the logs to a pipe ('|'), as shown below,
# is somewhat experimental and might fail under heavy load.
# "Usual libc implementations of printf will stall the whole
# process if the receiving end of a pipe stops reading."
#ErrorLog "|/usr/sbin/cronolog --symlink=/var/log/boa/error_log /var/log/boa/error-%Y%m%d.log" # AccessLog: The location of the access log file. If this does not
# start with /, it is considered relative to the server root.
# Comment out or set to /dev/null (less effective) to disable
# Access logging.
# AccessLog /var/log/boa/access_log
# Please NOTE: Sending the logs to a pipe ('|'), as shown below,
# is somewhat experimental and might fail under heavy load.
# "Usual libc implementations of printf will stall the whole
# process if the receiving end of a pipe stops reading."
#AccessLog "|/usr/sbin/cronolog --symlink=/var/log/boa/access_log /var/log/boa/access-%Y%m%d.log" # UseLocaltime: Logical switch. Uncomment to use localtime
# instead of UTC time
#UseLocaltime # VerboseCGILogs: this is just a logical switch.
# It simply notes the start and stop times of cgis in the error log
# Comment out to disable. #VerboseCGILogs # ServerName: the name of this server that should be sent back to
# clients if different than that returned by gethostname + gethostbyname ServerName www.your.org.here # VirtualHost: a logical switch.
# Comment out to disable.
# Given DocumentRoot /var/www, requests on interface 'A' or IP 'IP-A'
# become /var/www/IP-A.
# Example: http://localhost/ becomes /var/www/127.0.0.1
#
# Not used until version 0.93.17.2. This "feature" also breaks commonlog
# output rules, it prepends the interface number to each access_log line.
# You are expected to fix that problem with a postprocessing script. #VirtualHost # DocumentRoot: The root directory of the HTML documents.
# Comment out to disable server non user files. DocumentRoot /var/www # UserDir: The name of the directory which is appended onto a user's home
# directory if a ~user request is recieved. UserDir public_html # DirectoryIndex: Name of the file to use as a pre-written HTML
# directory index. Please MAKE AND USE THESE FILES. On the
# fly creation of directory indexes can be _slow_.
# Comment out to always use DirectoryMaker DirectoryIndex index.html # DirectoryMaker: Name of program used to create a directory listing.
# Comment out to disable directory listings. If both this and
# DirectoryIndex are commented out, accessing a directory will give
# an error (though accessing files in the directory are still ok). DirectoryMaker /usr/lib/boa/boa_indexer # DirectoryCache: If DirectoryIndex doesn't exist, and DirectoryMaker
# has been commented out, the the on-the-fly indexing of Boa can be used
# to generate indexes of directories. Be warned that the output is
# extremely minimal and can cause delays when slow disks are used.
# Note: The DirectoryCache must be writable by the same user/group that
# Boa runs as. # DirectoryCache /var/spool/boa/dircache # KeepAliveMax: Number of KeepAlive requests to allow per connection
# Comment out, or set to to disable keepalive processing KeepAliveMax # KeepAliveTimeout: seconds to wait before keepalive connection times out KeepAliveTimeout # MimeTypes: This is the file that is used to generate mime type pairs
# and Content-Type fields for boa.
# Set to /dev/null if you do not want to load a mime types file.
# Do *not* comment out (better use AddType!) MimeTypes /etc/mime.types # DefaultType: MIME type used if the file extension is unknown, or there
# is no file extension. DefaultType text/plain # CGIPath: The value of the $PATH environment variable given to CGI progs. CGIPath /bin:/usr/bin:/usr/local/bin # SinglePostLimit: The maximum allowable number of bytes in
# a single POST. Default is normally 1MB. # AddType: adds types without editing mime.types
# Example: AddType type extension [extension ...] # Uncomment the next line if you want .cgi files to execute from anywhere
#AddType application/x-httpd-cgi cgi # Redirect, Alias, and ScriptAlias all have the same semantics -- they
# match the beginning of a request and take appropriate action. Use
# Redirect for other servers, Alias for the same server, and ScriptAlias
# to enable directories for script execution. # Redirect allows you to tell clients about documents which used to exist in
# your server's namespace, but do not anymore. This allows you to tell the
# clients where to look for the relocated document.
# Example: Redirect /bar http://elsewhere/feh/bar # Aliases: Aliases one path to another.
# Example: Alias /path1/bar /path2/foo Alias /doc /usr/doc # ScriptAlias: Maps a virtual path to a directory for serving scripts
# Example: ScriptAlias /htbin/ /www/htbin/ ScriptAlias /cgi-bin/ /var/www/cgi-bin/

cgi我的配置:sudo vi /etc/apache2/sites-enabled/000-default

ServerName 127.0.0.1
<VirtualHost *:>
ServerAdmin webmaster@localhost DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::/
</Directory> </VirtualHost>

# 三、调试运行
    都配好后运行 sudo ./boa
    访问 http://127.0.0.1:端口号
    我的就是 http://127.0.0.1:88
    gcc -o test.cgi test.c 编译生成 test.cgi
    把cgi文件拷贝到 cgi-bin 下
    网址就是 http://localhost:88/cgi-bin/test.cgi

# 四、测试源码参考


下面提供几个测试代码,转自其他大佬,不过我找不到网址了。
1、

#include <stdio.h>
#include <stdlib.h> int main(void)
{
char *data;
long m,n;
printf("Content-type:text/html\n\n");
printf("<HTML>");
printf("<HEAD>");
printf("<TITLE>multi</TITLE>");
printf("</HEAD>");
printf("<BODY>");
printf("<H2 ALIGN=\"center\">multi control</H2>");
printf("<FORM METHOD=\"GET\" ACTION=\"test1.cgi\">");
printf("<P>Direction:<INPUT TYPE=\"text\" NAME=\"m\" VALUE=\"\" size=\"18\">");
printf("<P>Step Number:<INPUT TYPE=\"text\" NAME=\"n\" VALUE=\"\" size=\"17\">");
printf("<P ALIGN=\"left\">");
printf("<INPUT TYPE=\"SUBMIT\"  VALUE=\"Submit\">");
printf("<INPUT TYPE=\"RESET\"    VALUE=\"Reset\">");
printf("</P>");
printf("</FORM>");
printf("</BODY>");
printf("</HTML>"); data=getenv("QUERY_STRING");
if(!data)
printf("<P>get no datas and it's wrong.");
else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=)
printf("<P>the input must be numbers");
else
printf("<P>%ld and %ld multi= %ld",m,n,m*n);
return ;
}

2、
/var/www 下的pass.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登陆验证</title>
</head>
<body>
<form name="form1" action="/cgi-bin/pass.cgi" method="POST">
<table align="center">
<tr><td align="center" colspan="2"></td></tr>
<tr>
<td align="right">用户名</td>
<td><input type="text" name="Username"></td>
</tr>
<tr>
<td align="right">密 码</td>
<td><input type="password" name="Password"></td>
</tr>
<tr>
<td><input type="submit" value="登 录"></td>
<td><input type="reset" value="取 消"></td>
</tr>
</table>
</form>
</body>
</html>

/var/www/cgi-bin 下的pass.c

/*=====================================================================
cgi例子
=====================================================================*/
//pass.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getcgidata(FILE* fp, char* requestmethod);
int main()
{
char *input;
char *req_method;
char name[];
char pass[];
int i = ;
int j = ; // printf("Content-type: text/plain; charset=iso-8859-1\n\n");
printf("Content-type: text/html\n\n");
printf("The following is query reuslt:<br><br>");
req_method = getenv("REQUEST_METHOD");
input = getcgidata(stdin, req_method);
// 我们获取的input字符串可能像如下的形式
// Username="admin"&Password="aaaaa"
// 其中"Username="和"&Password="都是固定的
// 而"admin"和"aaaaa"都是变化的,也是我们要获取的 // 前面9个字符是UserName=
// 在"UserName="和"&"之间的是我们要取出来的用户名
for ( i = ; i < (int)strlen(input); i++ )
{
if ( input[i] == '&' )
{
name[j] = '\0';
break;
}
name[j++] = input[i];
}
// 前面9个字符 + "&Password="10个字符 + Username的字符数
// 是我们不要的,故省略掉,不拷贝
for ( i = + strlen(name), j = ; i < (int)strlen(input); i++ )
{
pass[j++] = input[i];
}
pass[j] = '\0';
printf("Your Username is %s<br>Your Password is %s<br> \n", name, pass); return ;
}
char* getcgidata(FILE* fp, char* requestmethod)
{
char* input;
int len;
int size = ;
int i = ; if (!strcmp(requestmethod, "GET"))
{
input = getenv("QUERY_STRING");
return input;
}
else if (!strcmp(requestmethod, "POST"))
{
len = atoi(getenv("CONTENT_LENGTH"));
input = (char*)malloc(sizeof(char)*(size + )); if (len == )
{
input[] = '\0';
return input;
} while()
{
input[i] = (char)fgetc(fp);
if (i == size)
{
input[i+] = '\0';
return input;
} --len;
if (feof(fp) || (!(len)))
{
i++;
input[i] = '\0';
return input;
}
i++; }
}
return NULL;
}

效果图:

输入数据点击“登录”

自动跳转到 /cgi-bin/pass.cgi ,获取到数据并打印

# 五、常见错误
你的配置会影响网址,502什么错误也是配置或权限有问题导致的。
如:
502 Bad Gateway
The CGI was not CGI/1.1 compliant.
cgi_header: unable to find LFLF
1.可能是网址打错了(路径是否和配置文件对应)
2.配置有问题
3.权限没给足 chmod 777 test.cgi

# 六、扩展(CCGI,SQLite)
CGIC的主站点: http://www.boutell.com/cgic/
SQLite官网:http://www.sqlite.org/
配置参考大佬博客:项目实战
我在调试中遇到的问题也在大佬的博客下面做了 评论 ,如果大家碰到问题可以参考一下。
补充:编译时会出错,使用

gcc -o config.cgi config.c sqlite3.c cgic.c -lsqlite3 -lpthread -ldl

![涉及文件]

![html效果]

![cgi页面]

数据已写入数据库,可使用以下命令

sqlite3 person.db
.table
select * from person;

Linux下嵌入式Web服务器BOA和CGI编程开发的更多相关文章

  1. 嵌入式web服务器BOA+CGI+HTML+MySQL项目实战——Linux

    准备环境操作系统: Ubuntu12.04 LTS环境搭建: 需要 BOA,Apache,CCGI,MySQL,GCC[Linux下嵌入式Web服务器BOA和CGI编程开发][数据库的相关知识——学习 ...

  2. 嵌入式web服务器BOA的移植及应用

    嵌入式web服务器子系统 一.嵌入式web服务器的控制流程 如下图所示,嵌入式web服务器可实现通过网络远程控制嵌入式开发板,便捷实用. 控制流程:浏览器 --->>>嵌入式开发板 ...

  3. Linux下查看Web服务器当前的并发连接数和TCP连接状态

    对于web服务器(Nginx.Apache等)来说,并发连接数是一个比较重要的参数,下面就通过netstat命令和awk来查看web服务器的并发连接数以及TCP连接状态. $ netstat -n | ...

  4. Linux下进行Web服务器压力(并发)测试工具http_load、webbench、ab、Siege、autobench简单使用教程(转)

    一.http_load 程序非常小,解压后也不到100K http_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载.但是它不同于大多数压力测试工 具,它可以以一个单一的进程运行,一般 ...

  5. 嵌入式Web服务器boa在ARM平台的移植步骤

    1.下载http://www.boa.org/ 2.解压tar xzf boa-0.94.13.tar.gz 3.编译cd boa-0.94.13/src./configure 生成了makefile ...

  6. Linux下实现web服务器

    说明:暂时只是实现了静态网页的响应 #include <stdio.h> #include <sys/types.h> /* See NOTES */ #include < ...

  7. Linux系统下LNMP一键搭建Linux、PHP、MySQL环境(适合新手搭建linux下的web生成环境)

    一. 首先要解释一下,什么是LNMP,LNMP起源于LAMP,LAMP是Linux+Apache+Mysql/MariaDB+Perl/PHP/Python的缩写,这里将Web服务端的Apache替换 ...

  8. 三种嵌入式web服务器(Boa / lighttpd / shttpd)的 linux移植笔记

    一:移植Boa(web服务器)到嵌入式Linux系统 一.Boa程序的移植 1.下载Boa源码    下载地址: http://www.boa.org/    目前最新发行版本: 0.94.13   ...

  9. [置顶] ARM-Linux下WEB服务器Boa的移植、配置和运行测试

    Linux下使用的轻量级WEB服务器主要有:lighttpd.thttpd.shttpd和boa等等,而Boa是使用最为广泛的轻量级WEB服务器之一(当然,阿帕奇是世界使用排名第一的Web服务器软件) ...

随机推荐

  1. Singer 修改tap-s3-csv 支持minio 连接

    singer 团队官方处了一个tap-s3-csv 的tap,对于没有使用aws 的人来说并不是很方便了,所以简单修改了 下源码,可以支持通用的s3 csv 文件的处理,同时发布到了官方pip 仓库中 ...

  2. 14-网页,网站,微信公众号基础入门(网页版MQTT,小试牛刀)

    https://www.cnblogs.com/yangfengwu/p/11192639.html 抱歉哈...最近由于做板子,,教程的进度落下了... 这些天总共做了还几块板子 首先对当前这个教程 ...

  3. 【题解】洛谷 P1449 后缀表达式

    目录 题目 思路 \(Code\) 题目 P1449 后缀表达式 思路 栈.题目说的不是很清楚,没说包含什么操作.除法用整数除法就行. 先string读入字符串,然后从前往后看如果是个数字就入栈,如果 ...

  4. C++ STL(标准模板库)的学习了解

    C++ STL(标准模板库)是一套功能强大的 C++ 模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量.链表.队列.栈. C++ 标准模板库的核心包括以 ...

  5. 分布式系统 与 Google

    google 论文 http://duanple.com/?p=170 google 论文与开源 http://duanple.com/?p=1096 分布式系统论文集 https://github. ...

  6. jquery实现select数据回显

    [html] view plain copy     <select class="div_select_re" id="edit_technicalGrade&q ...

  7. fastjson WriteClassName,Double类型不打3.3D

    方式一: public class SerializeConfigX extends SerializeConfig { public SerializeConfigX() { put(Double. ...

  8. mysql中的正则操作 匹配手机号,匹配中文,替换

    mysql中的正则操作 匹配手机号,匹配中文,替换 正则匹配hy_user表内tel字段的电话号码: SELECT * FROM hy_user WHERE tel REGEXP "[1][ ...

  9. arcpy地理处理工具案例教程-生成范围-自动画框-深度学习样本提取-人工智能-AI

    arcpy地理处理工具案例教程-生成范围-自动画框-深度学习样本提取-人工智能-AI 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 目的:对面. ...

  10. 同一个交换机 局域网内 内网IP ping不通为什么 没关闭windows防火墙

    同一个交换机 局域网内 内网IP  ping不通为什么  没关闭windows防火墙