shuixingoo 发表于 2011-6-26 17:10:09

Boa Webserver

工作环境:ubuntu in virtualbox on Win7
目的:在Boa上实验CGI程序(一个用户意见簿用perl编写)
Boa 配置boa.conf:


# 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 custom 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.3.2.6 2003/02/02 05:02:22 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 80

# 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

#Listen 10.0.2.15

#User: The name or UID the server should run as.
# Group: The group name or GID the server should run as.

#User www-data
#Group www-data

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

# PidFile: where to put the pid of the process.
# Comment out to write no pid file.
# Note: Because Boa drops privileges at startup, and the
# pid file is written by the UID/GID before doing so, Boa
# does not attempt removal of the pid file.
# PidFile /var/run/boa.pid

# 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
# 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"

ErrorLog /var/log/boa/error_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.
# Useful to set to /dev/stdout for use with daemontools.
# Access logging.
# 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"

AccessLog /var/log/boa/access_log

# CGILog /var/log/boa/cgi_log
# CGILog: The location of the CGI stderr log file. If this does not
# start with /, it is considered relative to the server root.
# The log file would contain any contents send to /dev/stderr
# by the CGI. If this is commented out, it defaults to whatever
# ErrorLog points.Set to /dev/null to disable CGI stderr logging.
# 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."
#CGILog"|/usr/sbin/cronolog --symlink=/var/log/boa/cgi_log /var/log/boa/cgi-%Y%m%d.log"

# CGIumask 027 (no mask for user, read-only for group, and nothing for user)
# CGIumask 027
# The CGIumask is set immediately before execution of the CGI.

# 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


# VHostRoot: the root location for all virtually hosted data
# Comment out to disable.
# Incompatible with 'Virtualhost' and 'DocumentRoot'!!
# Given VHostRoot /var/www, requests to host foo.bar.com,
# where foo.bar.com is ip a.b.c.d,
# become /var/www/a.b.c.d/foo.bar.com
# Hostnames are "cleaned", and must conform to the rules
# specified in rfc1034, which are be summarized here:
#
# Hostnames must start with a letter, end with a letter or digit,
# and have as interior characters only letters, digits, and hyphen.
# Hostnames must not exceed 63 characters in length.

#VHostRoot /var/www

# DefaultVHost
# Define this in order to have a default hostname when the client does not
# specify one, if using VirtualHostName. If not specified, the word
# "default" will be used for compatibility with older clients.

#DefaultVHost foo.bar.com

# DocumentRoot: The root directory of the HTML documents.
# Comment out to disable server non user files.


#DocumentRoot /var/www

DocumentRoot /home/spring/www


# UserDir: The name of the directory which is appended onto a user's home
# directory if a ~user request is received.

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

# Uncomment the next line if you want .cgi files to execute from anywhere

AddType application/x-httpd-cgi .cgi .pl .sh .py

# 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/share/doc

# ScriptAlias: Maps a virtual path to a directory for serving scripts
# Example: ScriptAlias /htbin/ /www/htbin/

#ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

ScriptAlias /cgi-bin/ /home/spring/www/cgi-bin/



测试页面HTML:
<HTML>
<HEAD>
<TITLE>User's Advices</TITLE>
</HEAD>
<BODY>

<H1>CGI DEMO:User's Advices</H1>
<BR>
<FORM METHOD = POST ACTION = /cgi-bin/UserAdvice.pl>
<B>your name:      </B><INPUT SIZE = 50 NAME = "yourname"><BR>
<B>your E-mail:    </B><INPUT SIZE = 50 NAME = "youremail"><BR>
<B>Your AdviceS: </B>
<TEXTAREA ROWS =5 COLS = 50 NAME = "yourcomments">
</TEXTAREA>
<BR><BR>
<INPUT TYPE = "SUBMIT" VALUE = "Submit" NAME = "Submit">
</FORM>
</BODY>
</HTML>

测试CGI程序:


#!/usr/bin/perl
#enverment pre

$advicefile = "/cgi-bin/UserAdvice.txt";

$method = $ENV{'REQUEST_METHOD'};

if ($method eq 'POST'){
        read(STDIN,$input,$ENV{'COMTENT_LENGTH'});
}

if ($method eq 'GET' || $method eq 'HEAD'){
        $input = $ENV{'QUERY_STRING'};
}

@form_inputs = split(/&/,$input);

for each $pair(form_inputs){
        ($html_name, $html_value) = split(/=/,$pair);
        $html_value = ~ tr/+//;
        $html_value = ~ s/%()/pack("C",hex($1))/eg;
        $form_data{$html_name} = $html_value;
}

$ yourname = $ form_data{"yourname"};
$ youremail = $ form_data{"youremail"};
$ yourcomments = $ form_data{"yourcomments"};

$ yourcomments = ~tr/\n\r/\n/;
$ yourcomments = ~tr/\r/\n/;

if ($ yourname eq "" || $ yourcomments eq ""){
        &html_header("用户意见薄----响应","输入错误");
        print"<H2>your name and your advices!</H2>\n";
        &html_foot;
        exit;
}


&print_file($yourname, $youremail, $yourcomments);
&html_header("用户意见薄----响应","输入正确");
        print"<H2>Thanks for your advices!</H2>\n";
        &html_foot;

sub html_header{
        local($headtitle, $bodytitle) = @_;
        print"Content-type: text/html\n\n";
        print"<HTML>\n";
        print"<HEAD>\n";
        print"<TITLE>$headtitle</TITLE>\n";
        print"</HEAD>\n";
        print"<BODY>\n";
        print"<H1>$bodytitle</H1>\n";
        print"<BR>\n";
}

sub html_foot{
        print"</BODY>\n";
        print"</HTML>\n";
}

sub print_file{
        local($name,$email,$comments) = @_;
        unless(open(FILE, ">>$advicefile")){
                &html_header("UserAdvice--respest","error");
                print"<H2>cannot open $advicefile, try again.</H2>\n";
                &html_foot;
                exit;
        }
        print FILE "Name : $name \n";
        print FILE "E-mail:$email \n";
        print FILE "Advice: \n";
        print FILE "$comments \n";
        print FILE "**************************************************\n";
        close FILE;
}

测试结果:
刚开始由于CGI程序没有执行权限,浏览器报错:403 Forbidden
Your client does not have permission to get URL /cgi-bin/UserAdvice.pl from this server.
后来加了可执行权限,报错:502 Bad Gateway
The CGI was not CGI/1.1 compliant.
由于是初次接触CGI这问题的解决就止步到了这里,先把Error_log贴出来希望高手指点给出些意见!

Error_log :
127.0.0.1 - - request "POST /cgi-bin/UserAdvice.pl HTTP/1.1" ("./UserAdvice.pl"): Unable to execve/execl pathname: "./UserAdvice.pl"Exec format error
127.0.0.1 - - request "POST /cgi-bin/UserAdvice.pl HTTP/1.1" ("/home/spring/www/cgi-bin/UserAdvice.pl"): cgi_header: unable to find LFLF
syntax error at ./UserAdvice.pl line 18, near "for each"
syntax error at ./UserAdvice.pl line 23, near "}"
Unrecognized character \xEF in column 42 at ./UserAdvice.pl line 33.
127.0.0.1 - - request "POST /cgi-bin/UserAdvice.pl HTTP/1.1" ("/home/spring/www/cgi-bin/UserAdvice.pl"): cgi_header: unable to find LFLF
syntax error at ./UserAdvice.pl line 18, near "for each"
syntax error at ./UserAdvice.pl line 23, near "}"
Unrecognized character \xEF in column 42 at ./UserAdvice.pl line 33.
127.0.0.1 - - request "POST /cgi-bin/UserAdvice.pl HTTP/1.1" ("/home/spring/www/cgi-bin/UserAdvice.pl"): cgi_header: unable to find LFLF
caught SIGHUP, restarting
re-reading configuration files
successful restart
syntax error at ./UserAdvice.pl line 18, near "for each"
syntax error at ./UserAdvice.pl line 23, near "}"
Unrecognized character \xEF in column 42 at ./UserAdvice.pl line 33.
127.0.0.1 - - request "POST /cgi-bin/UserAdvice.pl HTTP/1.1" ("/home/spring/www/cgi-bin/UserAdvice.pl"): cgi_header: unable to find LFLF
syntax error at ./UserAdvice.pl line 18, near "for each"
syntax error at ./UserAdvice.pl line 23, near "}"
Unrecognized character \xEF in column 42 at ./UserAdvice.pl line 33.
127.0.0.1 - - request "POST /cgi-bin/UserAdvice.pl HTTP/1.1" ("/home/spring/www/cgi-bin/UserAdvice.pl"): cgi_header: unable to find LFLF
syntax error at ./UserAdvice.pl line 18, near "for each"
syntax error at ./UserAdvice.pl line 23, near "}"
Unrecognized character \xEF in column 42 at ./UserAdvice.pl line 33.
127.0.0.1 - - request "POST /cgi-bin/UserAdvice.pl HTTP/1.1" ("/home/spring/www/cgi-bin/UserAdvice.pl"): cgi_header: unable to find LFLF
syntax error at ./UserAdvice.pl line 18, near "for each"
syntax error at ./UserAdvice.pl line 23, near "}"
Unrecognized character \xEF in column 42 at ./UserAdvice.pl line 33.
127.0.0.1 - - request "POST /cgi-bin/UserAdvice.pl HTTP/1.1" ("/home/spring/www/cgi-bin/UserAdvice.pl"): cgi_header: unable to find LFLF
日志提示CGI程序在:line 18和line 23处有错误。纠结!

huayuliang 发表于 2011-6-26 17:38:58

“unable to find LFLF”,应该是你发送的header有问题,这是cgi_header.c 里的:

        c = strstr(buf, "\n\r\n");
        if (c == NULL) {
                c = strstr(buf, "\n\n");
                if (c == NULL) {
                        log_error_time();
                        fputs("cgi_header: unable to find LFLF\n", stderr);
#ifdef FASCIST_LOGGING
                        log_error_time();
                        fprintf(stderr, "\"%s\"\n", buf);
#endif
                        send_r_error(req);
                        req->status = DONE;
                        return 1;
                }
        }

shuixingoo 发表于 2011-6-26 17:49:10

回复【1楼】huayuliang花生
“unable to find lflf”,应该是你发送的header有问题,这是cgi_header.c 里的:
        c = strstr(buf, "\n\r\n");
        if (c == null) {
                c = strstr(buf, "\n\n");
                if (c == null) {
                        log_error_time();
                        fputs("cgi_header: unable to find lflf\n", stderr);
#ifdef fascist_logging
                        log_error_time();
                        fprintf(stderr, "\"%s\"\n", buf);
#endif
                        send_r_error(req);
                        req-&gt;status = done;
                        return 1;
                }
        }

-----------------------------------------------------------------------

谢谢 huayuliang花生 的回复!我能力有限!有没有那位老兄有时间和经历把上面的程序调试一下!error_log
给出的提示是语法错误?

qllaoda 发表于 2011-9-21 10:25:11

建议用goahead来做,goahead可以支持cgi,也支持ASP的方式来实现动态网页。
asp编写网页的时候,不需要专门写一个CGI文件,直接就用html编写网页就可以了,需要动态变化的地方,用个asp的标记即可。服务器解释的时候,可以运行asp标记里指定的函数,用这个函数的输出来代替这个标记,然后送给客户端浏览器。这样的好处是网页可以用通用的网页编辑器来编辑,而不必用cgi文件一句一句地输出,网页的整体效果在编辑的时候就可以看到了,所见即所得。
页: [1]
查看完整版本: Boa Webserver