欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Linux下嵌入式Web服务器BOA和CGI编程开发

程序员文章站 2022-05-09 11:10:12
**目录**一、环境搭建二、相关配置(部分)三、调试运行四、测试源码参考五、常见错误六、扩展(CCGI,SQLite) # 一、环境搭建操作系统:Ubuntu12.04 LTSboa下载地址(但是我找不到...): http://www.boa.org/我是其他网站找到的资源,但是忘了网址了,所以我 ......


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



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

# 二、相关配置(部分)
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.94 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 2002/03/22 04:33:09 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 80.

# if it is less than 1024, the server must be started as root.
# 端口老是被占用,所以我改掉了
port 88

# listen: the internet address to bind(2) 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:
# 1. 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).
# 2. 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(3), 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 0 
group 0

# 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 0 to disable keepalive processing

keepalivemax 1000

# keepalivetimeout: seconds to wait before keepalive connection times out

keepalivetimeout 10

# 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 *:80>
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 ::1/128
</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
Linux下嵌入式Web服务器BOA和CGI编程开发

 

 Linux下嵌入式Web服务器BOA和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)!=2)
        printf("<p>the input must be numbers");
    else
        printf("<p>%ld and %ld multi= %ld",m,n,m*n);
    return 0;
}

 

Linux下嵌入式Web服务器BOA和CGI编程开发

 

 


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[64];
       char pass[64];
       int i = 0;
       int j = 0;
      
//     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 = 9; i < (int)strlen(input); i++ )
       {
              if ( input[i] == '&' )
              {
                     name[j] = '\0';
                     break;
              }                  
              name[j++] = input[i];
       }
       // 前面9个字符 + "&password="10个字符 + username的字符数
       // 是我们不要的,故省略掉,不拷贝
       for ( i = 19 + strlen(name), j = 0; 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 0;
}
char* getcgidata(file* fp, char* requestmethod)
{
       char* input;
       int len;
       int size = 1024;
       int i = 0;
      
       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 + 1));
             
              if (len == 0)
              {
                     input[0] = '\0';
                     return input;
              }
             
              while(1)
              {
                     input[i] = (char)fgetc(fp);
                     if (i == size)
                     {
                            input[i+1] = '\0';
                            return input;
                     }
                    
                     --len;
                     if (feof(fp) || (!(len)))
                     {
                            i++;
                            input[i] = '\0';
                            return input;
                     }
                     i++;
                    
              }
       }
       return null;
}

 

效果图:
Linux下嵌入式Web服务器BOA和CGI编程开发

 

 

输入数据点击“登录”

Linux下嵌入式Web服务器BOA和CGI编程开发

 

自动跳转到 /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的主站点:
sqlite官网:
配置参考大佬博客:
我在调试中遇到的问题也在大佬的博客下面做了 评论 ,如果大家碰到问题可以参考一下。
补充:编译时会出错,使用

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

 

![涉及文件]

Linux下嵌入式Web服务器BOA和CGI编程开发

 

 


![html效果]

Linux下嵌入式Web服务器BOA和CGI编程开发

 

 


![cgi页面]

Linux下嵌入式Web服务器BOA和CGI编程开发

 

 


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

sqlite3 person.db
.table
select * from person;