嵌入式WebServer搭建

Boa Web Server

介绍

Boa是一个运行在类Unix环境的单任务HTTP服务器,这意味着,它不会为每个连接(会话)创建单独的进程,也不会为了处理并发连接创建额外的进程。它通过内部复用来满足并发http连接,但是会为每一个连接创建单独的CGI进程(CGI的机制决定)、自动创建目录、文件自动压缩。初步测试显示,Boa每秒钟可以响应几千次点击。

Boa的设计目标是快速和安全。

Boa项目最初由Paul Phillips于1991年开发,目前由Larry Doolittle和Jon Nelson维护。

安装和使用

编译

linux$ tar -zxvf boa-0.94.tar.gz
linux$ cd src
修改src/defines.h中的SERVER_ROOT宏定义
linux$ ./configure
linux$ make

编译时报错:

util.c: In function ‘get_commonlog_time’:
util.c:100:39: error: pasting "t" and "->" does not give a valid preprocessing token
         time_offset = TIMEZONE_OFFSET(t);
                                       ^
compat.h:120:30: note: in definition of macro ‘TIMEZONE_OFFSET’
 #define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff
                              ^~~
<builtin>: recipe for target 'util.o' failed
make: *** [util.o] Error 1

解决方法,src/compat.h:

#ifdef HAVE_TM_GMTOFF
-#define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff
+#define TIMEZONE_OFFSET(foo) foo->tm_gmtoff
#else
#define TIMEZONE_OFFSET(foo) timezone
#endif

配置

1、选择一个用户和服务器端口号,通常使用80,如果没有root权限,建议使用1024以上的端口号;

2、选择SERVER_ROOT;

3、选择日志文件目录、CGI程序目录、URL路径根目录;

4、选择mime.type文件路径;

上述配置都在conf/boa.conf配置文件中。

运行

如果没有指定正确的SERVER_ROOT,可以通过-c选项指定

linux$ ./boa -c /usr/local/boa

boa.conf是Boa的配置文件(实际部署时,放置在/etc/boa/boa.conf路径),mime.types(部署在/etc/mime.types路径)定义了HTTP/1.0及以上版本中的Content-Type。

运行时报错:

[28/Feb/2023:14:38:13 +0000] log.c:73 - unable to dup2 the error log: Bad file descriptor

这是因为/etc/boa/boa.conf中指定的log文件夹没有创建。

打开网页,显示如上图,这是因为没有创建Web文件。

编写index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Justin's Web Server</title>
</head>
<body>
    <p>Welcome to Justin's Web Server</p> 
</body>
</html>

放置在/var/www/index.html路径,刷新网页。

 

mini_httpd

mini_httpd实现了http服务器的所有基本功能,包括:

  • GET, HEAD, and POST methods.
  • CGI.
  • Basic authentication.
  • Security against ".." filename snooping.
  • The common MIME types.
  • Trailing-slash redirection.
  • index.html, index.htm, index.cgi
  • Directory listings.
  • Multihoming / virtual hosting.
  • Standard logging.
  • Custom error pages.

它还没有配置支持SSL/HTTPS和IPv6。

编译

tar -zxvf mini_httpd-1.30.tar.gz
cd mini_httpd-1.30
make all

安装

将mini_httpd和htpasswd拷贝到/usr/local/sbin目录。

mini_httpd.conf配置文件

## do not leave empty lines in here!
#host=www.example.org
port=80
user=nobody
dir=/var/www
nochroot
pidfile=/var/www/mini_httpd.pid
logfile=/var/www/mini_httpd.log
charset=UTF-8
cgipat=**.cgi

运行

linux$ mini_httpd
usage:  mini_httpd [-C configfile] [-D] [-p port] [-d dir] [-dd data_dir] [-c cgipat] [-u user] [-h hostname] [-r] [-v] [-l logfile] [-i pidfile] [-T charset] [-P P3P] [-M maxage]
linux$ mini_httpd -C /etc/mini_httpd/mini_httpd.conf -T UTF-8
mini_httpd: started as root without requesting chroot(), warning only

 

posted on 2023-02-28 21:49  者旨於陽  阅读(464)  评论(0编辑  收藏  举报

导航