嵌入式web服务器移植

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wyy626562203/article/details/83536062

文章目录

嵌入式web服务器移植

ubuntu 16.04.4

arm-linux-gnueabihf-gcc 4.7.2

arm linux 3.10

嵌入式web服务器,是web服务器当中的一种,是基于嵌入式系统而实现的web服务器。指的是在嵌入式系统上实现的一个web服务器,可以通过ie等去访问,对硬件要求稍微低一点。

嵌入式WEB服务器常见的有:Lighttpd,Shttpd,Thttpd,Boa,Mini_httpd,Appweb,Goahead

1.移植环境搭建

新建工作目录

mkdir -p ~/Develop/webserver

如果移植Lighttpd,就在/home/wyy/Develop/webserver目录下新建一个Lighttpd目录

mkdir -p ~/Develop/webserver/lighttpd

2.Lighttpd移植

LibHttpd是一个开源轻量级嵌入式Web server,是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的web server环境。具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点。

lighttpd 适合静态资源类的服务,比如图片、资源文件、静态HTML等等的应用,性能应该比较好,同时也适合简单的CGI应用的场合,lighttpd可以很方便的通过fastcgi支持php

官网:http://www.lighttpd.net/

文档:http://redmine.lighttpd.net/projects/lighttpd/wiki

2.1 下载源码并解压

mkdir -p ~/Develop/webserver/lighttpd
cd ~/Develop/webserver/lighttpd
wget https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.51.tar.gz
tar xvf lighttpd-1.4.51.tar.gz

2.2 编译配置

http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs#Documentation-Overview

cd ~/Develop/webserver/lighttpd/lighttpd-1.4.51

./configure --prefix=/home/wyy/Develop/webserver/lighttpd/web --host=arm-linux-gnueabihf --build=i686-pc-linux --disable-FEATURE --enable-shared --disable-static --disable-lfs --disable-ipv6 --without-PACKAGE --without-valgrind --without-openssl --without-kerberos5 --without-pcre --without-zlib --without-bzip2 --without-lua

2.3 编译安装

make -j4
make install

2.4 Lighttpd配置

安装完成后会在安装目录下生成3个文件夹lib,sbin,share,需要手动再创建一些目录

cd ~Develop/webserver/lighttpd/web
mkdir -p cache cgi-bin config log sockets upload vhosts webpages

将源码中doc/config目录下的所有文件复制到安装目录中config文件夹里面

cp -a ~/Develop/webserver/lighttpd/lighttpd-1.4.51/doc/config/* ./config/
#删除里面的makefile文件当然不删除也可以不影响使用
rm -rf ./config/Makefile*

2.4.1 修改lighttpd.conf文件

vi ./config/lighttpd.conf

将16行至20行修改为如下

var.log_root    = "/home/wyy/Develop/webserver/lighttpd/web/log"
var.server_root = "/home/wyy/Develop/webserver/lighttpd/web"
var.state_dir   = "/home/wyy/Develop/webserver/lighttpd/web"
var.home_dir    = "/home/wyy/Develop/webserver/lighttpd/web"
var.conf_dir    = "/home/wyy/Develop/webserver/lighttpd/web/config"

将61行修改为如下

var.cache_dir   = server_root + "/cache"

将93行修改为如下

server.use-ipv6 = "disable"

将104和105行注释掉,如下所示

#server.username  = "lighttpd"
#server.groupname = "lighttpd"

将115行修改为如下

server.document-root = server_root + "/webpages"

将127行注释掉,如下所示

#server.pid-file = state_dir + "/lighttpd.pid"

如果不需要查看错误日志文件,可以将141行注释掉

server.errorlog             = log_root + "/error.log"

将152行注释掉,如下所示

#include "conf.d/access_log.conf"

将158行注释掉,如下所示

#include "conf.d/debug.conf"

将191行注释掉,如下所示

#server.network-backend = "linux-sendfile"

根据系统资源设置207行和254行的数值

#max-fds必须是max-connections两倍
server.max-fds = 256
server.max-connections = 128

将343至345行注释掉,如下所示

#$HTTP["url"] =~ "\.pdf$" {
#  server.range-requests = "disable"
#}

将408行修改为如下

server.upload-dirs = ("/home/wyy/Develop/webserver/lighttpd/web/upload")

2.4.2 修改modules.conf文件

vi ./config/modules.conf

在43后插入**“mod_alias”**

server.modules = (
  "mod_access",
  "mod_alias", #加在这里
#  "mod_alias",
#  "mod_auth",
#  "mod_authn_file",
#  "mod_evasive",
#  "mod_redirect",
#  "mod_rewrite",
#  "mod_setenv",
#  "mod_usertrack",
)

使能CGI模块,将144行的注释符去掉

##
## plain old CGI (mod_cgi)
##
include "conf.d/cgi.conf"

2.4.3 修改conf.d文件夹中的cgi.conf文件

vi ./config/conf.d/cgi.conf

将15至19行这一段配置修改如下

#将以下内容
cgi.assign = ( ".pl"  => "/usr/bin/perl",
			 ".cgi" => "/usr/bin/perl",
			 ".rb"  => "/usr/bin/ruby",
			 ".erb" => "/usr/bin/eruby",
			 ".py"  => "/usr/bin/python" )
#修改为
cgi.assign = (".cgi" => "")
#cgi.assign = ( ".pl"  => "/usr/bin/perl",
#             ".cgi" => "/usr/bin/perl",
#             ".rb"  => "/usr/bin/ruby",
#             ".erb" => "/usr/bin/eruby",
#             ".py"  => "/usr/bin/python" )

将28行的注释符去掉,如下所示

alias.url += ( "/cgi-bin" => server_root + "/cgi-bin" )

2.4.4 修改conf.d文件夹中的cgi.conf文件

vi ./config/conf.d/dirlisting.conf

将24行的注释掉,如下所示

##
## list of regular expressions. Files that match any of the specified
## regular expressions will be excluded from directory listings.
##
#dir-listing.exclude       =  ( "~$" )

由于便是去掉了pcre的支持,所以这里将其注释掉否则会出现以下错误信息

1970-01-01 00:24:24: (server.c.1436) server started (lighttpd/1.4.51) 
1970-01-01 00:24:24: (mod_dirlisting.c.300) pcre support is missing for:  dir-listing.exclude , please install libpcre and the headers 
1970-01-01 00:24:24: (server.c.1444) Configuration of plugins failed. Going down. 

2.5 Lighttpd测试

编写名为index.html的页面

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>lighttpd测试</title>
    </head>
    <body>
        <p>轻量级web服务器lighttpd测试(for arm)</p>
        <hr>
        <p>hello word!</p>
    </body>
</html>

将这个index.html文件拷贝到Lighttpd安装目录下webpages

2.5.1 启动Lighttpd服务器

注意:部署到开发板时目录要和开发环境的一样,可以通过上面的配置修改路径。

cd ~/Develop/webserver/lighttpd/web/sbin
./lighttpd -f ../config/lighttpd.conf

打开浏览器就可以看到输出的页面了

2.5.2 关闭Lighttpd服务器

killall lighttpd

2.6 CGI测试

编写cgi程序

test.c

#include <stdio.h>
int main()
{
    printf("Content-type: text/html\n\n");
    printf("<html>\n");
    printf("<head>\n");
    printf("<title>CGI Output</title>\n");
    printf("</head>\n");

    printf("<body>");
    printf("<h1> Hello, world. </h1>");
    printf("</body>");
    printf("</html>\n");
    return 0;
}

编译cgi,并将test.cgi程序拷贝到开发板的~/Develop/webserver/lighttpd/web/cgi-bin目录下

arm-linux-gnueabihf-gcc test.c -o test.cgi

在浏览器中输入地址192.168.x.x/cgi-bin/test.cgi即可访问

3.Shttpd移植

Shttpd,开源。它是另一个轻量级的web server,具有比thttpd更丰富的功能特性,支持CGI, SSL, cookie, MD5认证, 还能嵌入(embedded)到现有的软件里。最有意思的是不需要配置文件!由于shttpd可以轻松嵌入其他程序里,因此shttpd是较为理想的web server开发原形,开发人员可以基于shttpd开发出自己的webserver,官方网站上称shttpd如果使用uclibc/dielibc(libc的简化子集)则开销将非常非常低。

官网:https://docs.huihoo.com/shttpd/

文档:https://docs.huihoo.com/shttpd/shttpd.1.txt

3.1 下载源码并解压

mkdir -p ~/Develop/webserver/Shttpd
cd ~/Develop/webserver/Shttpd
wget https://nchc.dl.sourceforge.net/project/shttpd/shttpd/1.42/shttpd-1.42.tar.gz
tar xvf shttpd-1.42.tar.gz

3.2 编译配置

cd ~/Develop/webserver/Shttpd/shttpd-1.42

修改src/Makefile,在第29行空白处添加以下4行,如下所示

# 3. start console, go to shttpd-VERSION\src\ directory
# 4. type "nmake msvc"
# 5. go to shttpd-VERSION\examples , type "nmake msvc"
CC=arm-linux-gnueabihf-gcc
AR=arm-linux-gnueabihf-ar
CFLAGS="-DNO_SSL "

all:
        @echo "make (unix|msvc|mingw|rtems)"
        @echo 'Linux: "LIBS=-ldl make unix"'
        @echo 'BSD: "LIBS=-lpthread make unix"'
        @echo 'Solaris: "LIBS="-lpthread -lnsl -lsocket" make unix"'

3.3 编译

LIBS="-lpthread " make unix

成功后,会产生静态库libshttpd.a和可执行文件shttpd,后面会用到这个libshttpd.a文件。

3.4 运行shttpd

#静态页面存放在/home/wyy/Develop/webserver/shttpd/web,可以放到任意你想放的位置,然后通过-root来之指定
./shttpd -root /home/wyy/Develop/webserver/shttpd/web -ports 80

3.5 shttpd测试

假设静态页面和CGI存放在/home/wyy/Develop/webserver/shttpd/web目录下

3.5.1 静态页面测试

将静态页面放在-root指定的/home/wyy/Develop/webserver/shttpd/web目录下,使用浏览器中输入服务器地址即可看到测试页面。

3.5.2 使用官方例子测试shttpd

进到源码目录下examples,修改example.c文件,去掉SSL等功能,修改如下:

将第349,351,374,372,373行注释掉。

ctx = shttpd_init(argc, argv);
//shttpd_set_option(ctx, "ssl_cert", "shttpd.pem");
shttpd_set_option(ctx, "aliases", ALIAS_URI "=" ALIAS_DIR);
//shttpd_set_option(ctx, "ports", "8080,8081s");

/* Register an index page under two URIs */
shttpd_register_uri(ctx, "/", &show_index, (void *) &data);
shttpd_register_uri(ctx, "/abc.html", &show_index, (void *) &data);

/* Register a callback on wildcard URI */
shttpd_register_uri(ctx, "/users/*/", &show_users, NULL);

/* Show how to use password protection */
shttpd_register_uri(ctx, "/secret", &show_secret, NULL);
shttpd_set_option(ctx, "protect", "/secret=passfile");

/* Show how to use stateful big data transfer */
shttpd_register_uri(ctx, "/huge", &show_huge, NULL);

/* Register URI for file upload */
shttpd_register_uri(ctx, "/post", &show_post, NULL);

/* Register SSI callbacks */
//shttpd_register_ssi_func(ctx, "true", ssi_test_true, NULL);
//shttpd_register_ssi_func(ctx, "false", ssi_test_false, NULL);
//shttpd_register_ssi_func(ctx, "print_stuff", ssi_print_stuff, NULL);

shttpd_handle_error(ctx, 404, show_404, NULL);

编译例子

arm-linux-gnueabihf-gcc example.c -I ../src  ../src/libshttpd.a -ldl -lpthread -o test

成功后,执行./test,使用浏览器访问http://192.168.x.x/,可以看到输出的界面。

3.6 CGI测试

将CGI程序放在-root指定的/home/wyy/Develop/webserver/shttpd/web目录下,假设cgi文件名为test.cgi,在流览器中输入服务器地址192.168.1.2/test.cgi,即可访问cgi程序。(这里成CGI程序的后缀是.cgi,但是没有规定一定是.cgi后缀,可以设置成任意的,如test.loveyou)

编写cgi程序

test.c

#include <stdio.h>
int main()
{
    printf("Content-type: text/html\n\n");
    printf("<html>\n");
    printf("<head>\n");
    printf("<title>CGI Output</title>\n");
    printf("</head>\n");

    printf("<body>");
    printf("<h1> Hello, world. </h1>");
    printf("</body>");
    printf("</html>\n");
    return 0;
}

编译cgi,并将test.cgi程序拷贝到开发板的/home/wyy/Develop/webserver/shttpd/web目录下

arm-linux-gnueabihf-gcc test.c -o test.cgi

在浏览器中输入地址192.168.x.x/test.cgi即可访问

4.Thttpd移植

是一款比较精巧的开源Web服务器。它的初衷是提供一款简单、小巧、易移植、快速和安全的HTTP服务器。对于并发请求不使用fork()来派生子进程处理,而是采用多路复用(Multiplex)技术来实现。

thttpd至少和主流的web server一样快,在高负载下更快,因为其资源占用小的缘故。

Thttpd还有一个较为引人注目的特点:基于URL的文件流量限制,这对于下载的流量控制而言是非常方便的。象Apache就必须使用插件实现,效率较thttpd低。Thttp是开源的。是用C语言编写的,使用的很多。

官网:http://www.acme.com/software/thttpd/

4.1 下载源码并解压

mkdir -p ~/Develop/webserver/Thttpd
cd ~/Develop/webserver/Thttpd
wget http://www.acme.com/software/thttpd/thttpd-2.29.tar.gz
tar xvf thttpd-2.29.tar.gz

4.2 编译配置

cd ~/Develop/webserver/Thttpd/thttpd-2.29
./configure

修改Makefile,在第50行将编译器改为arm-linux-gnueabihf-gcc

vi Makefile

修改如下:

# You shouldn't need to edit anything below here.
 
CC =            arm-linux-gnueabihf-gcc #修改这里
CCOPT =         -O2

在cgi-src和extras目录中也有Makefile,修改方法和上面一样。

4.3 编译

make -j4

4.4 Thttpd部署

创建thttpd的工作目录,假设目录是/home/wyy/Develop/webserver/Thttpd/web

#thttpd工作目录
mkdir -p /home/wyy/Develop/webserver/Thttpd/web
#页面
mkdir -p /home/wyy/Develop/webserver/Thttpd/web/www
#配置文件conf
mkdir -p /home/wyy/Develop/webserver/Thttpd/web/conf
#可执行文件sbin
mkdir -p /home/wyy/Develop/webserver/Thttpd/web/sbin
#log文件目录
mkdir -p /home/wyy/Develop/webserver/Thttpd/web/log
#cgi-bin目录
mkdir -p /home/wyy/Develop/webserver/Thttpd/web/www/cgi-bin

mkdir -p /home/wyy/Develop/webserver/Thttpd/web/run

将生成的thttpd复制到/home/wyy/Develop/webserver/Thttpd/web/sbin目录,contrib/redhat-rpm/thttpd.conf复制到/home/wyy/Develop/webserver/Thttpd/web/conf 目录,并修改thttpd为可执行。

使用vi打开thttpd.conf文件,并进行配置,如下配置我们仅将“user=httpd”改为“user=root”,如果不修改会提示不存在httpd用户,解决方法是新建一个httpd用户sudo useradd httpd,这里直接改为root用就无需创建新用户了

# This section overrides defaults
dir=/home/wyy/Develop/webserver/Thttpd/web/www
nochroot # chroot 
user=root# default = nobody  #修改这里
logfile=/home/wyy/Develop/webserver/Thttpd/web/log/thttpd.log
pidfile=/home/wyy/Develop/webserver/Thttpd/web/run/thttpd.pid
# This section _documents_ defaults in effect
# port=80
# nosymlink# default = !chroot
# novhost
# nocgipat
cgipat=/cgi-bin/* 
# nothrottles
# host=0.0.0.0
# charset=iso-8859-1

4.5 Thttpd测试

创建一个名为index.html,并将给文件拷贝到开发板/home/wyy/Develop/webserver/Thttpd/web/www

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Thttpd测试</title>
    </head>
    <body>
        <h1>Hello word!</h1>
        <p>We are coming from one world, welcome here!</p>
    </body>
</html>

开启thttpd,打开浏览器即可访问

./thttpd -D -C ../conf/thttpd.conf

4.6 CGI脚本测试

编写测试程序

test.c

#include <stdio.h>

int main(void)
{
    int i = 10;
    printf("Content-type: text/html\n\n");
    printf("<html>\n");
    printf("<head><title>CGI Output</title></head>\n");
    printf("<body>\n");
    while(i)
        printf("<h1>%d\n</h1>\n",i--);
    printf("<body>\n");
    printf("</html>\n");
    return 0;
}

交叉编译,拷贝cgi程序到开发板目录/home/wyy/Develop/webserver/Thttpd/web/cgi-bin

arm-linux-gnueabihf-gcc -o test.cgi test.c

测试CGI

在浏览器中输入http://192.168.x.x/cgi-bin/test.cgi

5.Boa移植

服务器是一个小巧高效的web服务器,是一个运行于unix或linux下的,支持CGI的、适合于嵌入式系统的单任务的http服务器,源代码开放、性能高。

是一种非常小巧的Web服务器,其可执行代码只有大约60KB左右。作为一种单任务Web服务器,Boa只能依次完成用户的请求,而不会fork出新的进程来处理并发连接请求。但Boa支持CGI,能够为CGI程序fork出一个进程来执行。Boa的设计目标是速度和安全。

官网:http://www.boa.org/

文旦:http://www.boa.org/documentation/

5.1 下载源码并解压

mkdir -p ~/Develop/webserver/Boa
cd ~/Develop/webserver/Boa
wget http://www.boa.org/boa-0.94.13.tar.gz
tar xvf boa-0.94.13.tar.gz

5.2 编译配置

cd ~/Develop/webserver/Boa/boa-0.94.13/src
./configure

修改Makefile

vi Makefile

将第23行修改为

LDFLAGS = -static

将第31,32行修改为

CC = arm-linux-gnueabihf-gcc
CPP = arm-linux-gnueabihf-gcc –E

5.3 修改源码

5.3.1 修改boa.c文件

将第225行代码注释掉,如下

//if ( setuid ( 0 ) != - 1 ) {
//    DIE ( "icky Linux kernel bug!" );
//}

5.3.2 修改compat.h文件

修改第120行,如下

#define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff

修改成

#define TIMEZONE_OFFSET(foo) (foo)->tm_gmtoff

5.3.3 修改config.c文件

将第266到286行代码注释掉,不注释会报错:gethostbyname:: Resource temporarily unavailable

/*
if(!server_name)
{
 	struct hostent *he;
 	char temp_name[100];
 	
	......
	
	perror("strdup:");
	exit(1);
	}
}*/

5.3.4 修改log.c文件

注释掉第72到74,不注释会报错:unable to dup2 the error log:bad file descriptor

/*if (dup2(error_log, STDERR_FILENO) == -1) {
	DIE("unable to dup2 the error log");
}*/

5.4 编译

make -j4

如果编译出现报错,那么应该安装一下两个工具

sudo apt-get install bison
sudo apt-get install flex

去除调试信息

arm-linux-gnueabihf-strip boa

修改执行权限

chmod 777 boa

5.5 Boa服务器配置

5.5.1 Boa配置文件解析

# 指定boa工作目录,从该目录加载boa.conf
# boa -c /usr/local/boa

# 监听的端口
Port 80

# 服务器绑定的IP地址,注释掉表示绑定到INADDR_ANY,适配服务器所有的IP
# Listen 192.68.0.5

# 服务器运行的用户和组
# User o
User 0

# Group o
Group 0

# 当服务器发生问题时发送报警的email地址
# ServerAdmin root@localhost

# 错误日志文件
ErrorLog /var/log/boa/error_log

# 访问日志文件
AccessLog /var/log/boa/access_log

# 是否使用本地时间。如果没注释掉,则使用本地时间。注释掉则使用UTC时间
# UseLocaltime

# 是否记录CGI运行信息
# VerboseCGILogs

# 服务器名字
ServerName www.hello.com

# 是否启动虚拟主机功能,即设备可以有多个网络接口,每个接口都可以拥有一个虚拟的Web服务器
# VirtualHost

# 非常重要,HTML文档的主目录
DocumentRoot /var/www

# 如果收到一个用户请求的话,在用户主目录后再增加的目录名
UserDir public_html

# HTML目录索引的文件名
DirectoryIndex index.html

# 指定用于生成目录的程序,注释此变量将不允许列目录
# 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


# 一个连接所允许的HTTP持续作用请求最大数目
KeepAliveMax 1000

# HTTP持续作用中服务器在两次请求之间等待的时间数,以秒为单位,超时将关闭连接
KeepAliveTimeout 10

# 指明mime.types文件位置
MimeTypes /etc/mime.types

# 文件扩展名没有或未知的话,使用的缺省MIME类型
DefaultType text/plain

# Uncomment the next line if you want .cgi files to execute from anywhere
# AddType application/x-httpd-cgi cgi

# 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

# CGIPath: The value of the $PATH environment variable given to CGI progs.
CGIPath /bin:/usr/bin:/usr/local/bin

# 为路径加上别名
Alias /doc /usr/doc

# 指明CGI脚本的虚拟路径对应的实际路径
ScriptAlias /cgi-bin/ /var/www/cgi-bin/

5.5.2 服务器部署

从linux的etc目录拷贝mime.types、passwd、group文件到开发板系统的etc目录(一般已经有passwd和group,只需拷贝mime.types),将boa拷贝到开发板的/bin目录下,在开发板/etc目录下建boa目录,拷贝boa.conf到板子的/etc/boa目录。

开发版:

mkdir -p /home/wyy/Develop/webserver/Boa
mkdir /home/wyy/Develop/webserver/Boa/www
mkdir /home/wyy/Develop/webserver/Boa/cgi-bin
mkdir /home/wyy/Develop/webserver/Boa/boa
mkdir /home/wyy/Develop/webserver/Boa/bin
mkdir /home/wyy/Develop/webserver/Boa/log
mkdir /home/wyy/Develop/webserver/Boa/doc
mkdir /home/wyy/Develop/webserver/Boa/indexer

将宿主机中的mime.types拷贝到开发板的/home/wyy/Develop/webserver/Boa/boa中,将源码目录中的boa.conf拷贝到/home/wyy/Develop/webserver/Boa/boa中。将boa可执行文件拷贝到/home/wyy/Develop/webserver/Boa/bin

修改boa.conf

vi /etc/boa/boa.conf
# 1.User的修改
User nobody
# 修改为
User 0

# 2.Group的修改
Group nogroup
# 修改为
Group 0

# 3.错误日志路径
ErrorLog /var/log/error_log
# 修改为
ErrorLog /home/wyy/Develop/webserver/Boa/log/error_log  

# 4.ServerName的设置 
# ServerName www.your.org.here
# 修改为
ServerName www.your.org.here
#否则会出现错误“gethostbyname::No such file or directory”

# 5.DoucmentRoot的修改
DoucmentRoot /var/www
# 修改为
DoucmentRoot /home/wyy/Develop/webserver/Boa/www

# 6.指定资源类型文件
MimeTypes /etc/mime.types 
# 修改为
MimeTypes /home/wyy/Develop/webserver/Boa/boa/mime.types

# 7.修改生成目录
DirectoryMaker /usr/lib/boa/boa_indexer
# 修改为
DirectoryMaker /home/wyy/Develop/webserver/Boa/indexer


# 8.修改文档目录
Alias /doc /usr/doc
# 修改为
Alias /doc /home/wyy/Develop/webserver/Boa/doc

# 9.ScriptAlias的修改
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 
# 修改为
ScriptAlias /cgi-bin/ /home/wyy/Develop/webserver/Boa/cgi-bin/

5.6 Boa测试

在开发板上编写一个名为index.html的静态页面,将该页面拷贝到/www目录下,使用浏览器输入开发板ip即可访问。

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Boa 静态网页测试</title>
    </head>

    <body>
        <h1>  Welcome to Boa sever! </h1>
    </body>
</html>

5.7 CGI测试

编写cgi程序

test.c

#include <stdio.h>
int main()
{
    printf("Content-type: text/html\n\n");
    printf("<html>\n");
    printf("<head>\n");
    printf("<title>CGI Output</title>\n");
    printf("</head>\n");

    printf("<body>");
    printf("<h1> Hello, world. </h1>");
    printf("</body>");
    printf("</html>\n");
    return 0;
}

编译cgi,并将test.cgi程序拷贝到开发板的/home/wyy/Develop/webserver/Boa/cgi-bin目录下

arm-linux-gnueabihf-gcc test.c -o test.cgi

在浏览器中输入地址192.168.x.x/cgi-bin/test.cgi即可访问

6.Mini_httpd移植

Mini_httpd是一个小型的HTTP服务器。开源,它的性能不强,但是它非常适合于中小访问量的站点。Mini_httpd和thttpd都是ACME Labs 开发的软件,功能没有thttpd强。支持php。

官网:http://www.acme.com/software/mini_httpd/

6.1 下载源码并解压

mkdir -p ~/Develop/webserver/Mini_httpd
cd ~/Develop/webserver/Mini_httpd
wget http://www.acme.com/software/mini_httpd/mini_httpd-1.29.tar.gz
tar xvf mini_httpd-1.29.tar.gz

6.2 编译配置

cd ~/Develop/webserver/Mini_httpd/mini_httpd-1.29
vi Makefile

修改Makefile第25行

CC =            arm-linux-gnueabihf-gcc #cc

6.3 编译

默认是不支持ssl

make

6.4 Mini_httpd服务器配置

开发板创建目录

mkdir -p /home/wyy/Develop/webserver/Mini_httpd
mkdir /home/wyy/Develop/webserver/Mini_httpd/bin
mkdir /home/wyy/Develop/webserver/Mini_httpd/www
mkdir /home/wyy/Develop/webserver/Mini_httpd/conf
mkdir /home/wyy/Develop/webserver/Mini_httpd/www/cgi-bin
mkdir /home/wyy/Develop/webserver/Mini_httpd/log
mkdir /home/wyy/Develop/webserver/Mini_httpd/run
mkdir /home/wyy/Develop/webserver/Mini_httpd/cert

创建mini_httpd.conf

# mini_httpd configuration file
#存放网页的目录
data_dir=/home/wyy/Develop/webserver/Mini_httpd/www

nochroot

#用户的使用权限
user=root #httpd

#使用的端口
port=80 #443 https

#host=0.0.0.0

#存放cgi的目录
cgipat=/cgi-bin/*

#存放log的文件
logfile=/home/wyy/Develop/webserver/Mini_httpd/log/mini_httpd.log

pidfile=/home/wyy/Develop/webserver/Mini_httpd/run/mini_httpd.pid

#设置的格式
charset=GB2312

#支持ssl
#ssl

#存放ssl证书的目录
#certfile=/etc/mini_httpd.pem

部署mini_httpd服务器,将mini_httpd.conf拷贝到开发板/home/wyy/Develop/webserver/Mini_httpd/conf目录下,mini_httpd拷贝到开发板/home/wyy/Develop/webserver/Mini_httpd/bin目录下。

6.5 Mini_httpd测试

编写一个index.html页面拷贝到/www

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Mini_httpd 静态网页测试</title>
    </head>

    <body>
        <h1>  Welcome to Mini_httpd sever! </h1>
    </body>
</html>

启动服务器,在浏览器里打开ip即可访问

./mini_httpd -C ../conf/mini_httpd.conf

6.6 Mini_httpd支持ssl

要支持ssl需要在上面基础上做些修改

6.6.1 修改Makefile

将17到20行注释去掉,设置openssl目录

SSL_TREE =     /usr/local/ssl
SSL_DEFS =     -DUSE_SSL
SSL_INC =      -I$(SSL_TREE)/include
SSL_LIBS =     -L$(SSL_TREE)/lib -lssl -lcrypto

6.6.2 制作证书

根据提示输入对应的信息,生成mini_httpd.pem文件

make cert

6.6.3 编译支持SSL的Mini_httpd

make

6.6.4 修改配置文件mini_httpd.cnf

# mini_httpd configuration file
#存放网页的目录
data_dir=/home/wyy/Develop/webserver/Mini_httpd/www

nochroot

#用户的使用权限
user=root #httpd

#使用的端口
port=443

#host=0.0.0.0

#存放cgi的目录
cgipat=/cgi-bin/*

#存放log的文件
logfile=/home/wyy/Develop/webserver/Mini_httpd/log/mini_httpd.log

pidfile=/home/wyy/Develop/webserver/Mini_httpd/run/mini_httpd.pid

#设置的格式
charset=GB2312

#支持ssl
ssl

#存放ssl证书的目录
certfile=/home/wyy/Develop/webserver/Mini_httpd/cert/mini_httpd.pem

6.6.5 部署

将证拷贝到开发板的/home/wyy/Develop/webserver/Mini_httpd/cert

6.7 CGI测试

编写cgi程序

test.c

#include <stdio.h>
int main()
{
    printf("Content-type: text/html\n\n");
    printf("<html>\n");
    printf("<head>\n");
    printf("<title>CGI Output</title>\n");
    printf("</head>\n");

    printf("<body>");
    printf("<h1> Hello, world. </h1>");
    printf("</body>");
    printf("</html>\n");
    return 0;
}

编译cgi,并将test.cgi程序拷贝到开发板的/home/wyy/Develop/webserver/Mini_httpd/cgi-bin目录下

arm-linux-gnueabihf-gcc test.c -o test.cgi

在浏览器中输入地址192.168.x.x/cgi-bin/test.cgi即可访问

7.Appweb

appweb 是下一代嵌入式web服务器,它天生是为嵌入式开发的,它的最初设计理念就是安全。Appweb是一个快速、低内存使用量、标准库、方便的服务器。与其它嵌入式web服务器相比,appweb最大特点就是功能多和高度的安全保障。Appweb简单、方便、开源。

官网:https://www.embedthis.com/

文档:https://www.embedthis.com/appweb/doc/

7.1 下载源码并解压

下载地址:https://download.csdn.net/download/wyy626562203/10746781

GitHub:https://github.com/embedthis/appweb

mkdir -p ~/Develop/webserver/Appweb
cd ~/Develop/webserver/Appweb
wget https://s3.amazonaws.com/embedthis.software/appweb-7.0.3-src.tgz
tar xvf appweb-7.0.3-src.tgz

7.2 编译

参考文档:https://www.embedthis.com/makeme/doc/source/make.html

查看编译配置选项使用./configure -h

cd ~/Develop/webserver/Appweb/appweb-7.0.3
make -f projects/appweb-linux-default.mk ARCH=arm PROFILE=default CC=arm-linux-gnueabihf-gcc ME_COM_CGI=1

7.3 配置文件

参考官网:https://www.embedthis.com/appweb/doc/users/configuration.html

https://www.embedthis.com/appweb/doc/users/directives.html

源码里有个测试用例test,可以参考里面配置,但是不要直接使用。可以使用samples/typical-serversamples/tiny-server里的配置文件,根据需要稍做修改。

7.4 Appweb测试

Appweb支持ssl,这里就不测ssl功能了。

在开发板创建工作目录,将build/linux-arm-default/下的bin拷贝到/home/wyy/Develop/webserver/Appweb

mkdir -p /home/wyy/Develop/webserver/Appweb
mkdir /home/wyy/Develop/webserver/Appweb/www
mkdir /home/wyy/Develop/webserver/Appweb/bin
mkdir /home/wyy/Develop/webserver/Appweb/conf
mkdir /home/wyy/Develop/webserver/Appweb/cgi-bin

/home/wyy/Develop/webserver/Appweb/conf下新建一个配置文件,内容如下

#日志
ErrorLog "error.log" size=10MB level=2 backup=5 anew 
TraceLog "trace.log" level=0 size=10MB backup=5 anew   
Trace request=1 error=2 result=2 context=3 form=4 body=5 debug=5 content=10K    

#Chroot "."

#
#Enable emitting debug error messages back to the client. Defaults to "off".         
#WARNING: this may disclose unwanted information. Do not enable in production eleases. 
# 
ShowErrors off 

#监听的端口
Listen 80 

#页面存在的目录
Documents /home/wyy/Develop/webserver/Appweb/www

#用户权限
GroupAccount root  
UserAccount root

# CanonicalName http://yourname.com  

<if CGI_MODULE>
    AddHandler cgiHandler exe cgi cgi-nph out bat cmd pl py
    #配置cgi的路径
    ScriptAlias /cgi-bin/ "/home/wyy/Develop/webserver/Appweb/cgi-bin"
    
    Action application/x-perl /usr/bin/perl                                        
    Action application/x-python /usr/bin/python
    Action application/x-lua /usr/bin/lua
    Action application/x-ruby /usr/bin/ruby 
    CgiPrefix CGI_ 
    CgiEscape on
    LimitProcesses 10
</if>

<Route ^/action/>
    SetHandler actionHandler
</Route>

<if ESP_MODULE>
    AddHandler espHandler esp
</if>

AddHandler fileHandler
Cache client=1day extensions="html,gif,jpeg,jpg,png,pdf,ico,js"

#缩短超时时间
InactivityTimeout 30sec
RequestParseTimeout 5sec
RequestTimeout 1min
SessionTimeout 5mins

#工作线程数  
LimitWorkers 3

#管道队列的最大缓冲区大小   
LimitBuffer 16K
 
#最大并发客户端系统数。 设置为零无限制。
LimitClients 5

#最大客户端连接数
LimitConnections 10

#每个客户端的最大并发请求数。 这有助于防止拒绝服务攻击
LimitRequestsPerClient 10

#在单个TCP / IP连接上接受的HTTP请求数
#减少此数字以最小化DoS攻击的可能性。
LimitKeepAlive 50

#请求内容总体的最大大小(包括标题)  
LimitRequestBody 50K

#请求表单的最大大小
LimitRequestForm 16K
 
#请求标头的最大大小  
LimitRequestHeader 4K

#请求标题行的最大数量
LimitRequestHeaderLines 16
  
#最大请求URI大小
LimitUri 512


#最大应用程序内存 如果超出则调用MemoryPolicy
LimitMemory 3MB
MemoryPolicy restart

启动服务器,在浏览器中输入开发板ip即可访问。记得在www目录中放入测试页面。

./appweb --config ../conf/appweb.conf

7.5 CGI测试

编写cgi程序

test.c

#include <stdio.h>
int main()
{
    printf("Content-type: text/html\n\n");
    printf("<html>\n");
    printf("<head>\n");
    printf("<title>CGI Output</title>\n");
    printf("</head>\n");

    printf("<body>");
    printf("<h1> Hello, world. </h1>");
    printf("</body>");
    printf("</html>\n");
    return 0;
}

编译cgi,并将test.cgi程序拷贝到开发板的/home/wyy/Develop/webserver/Appweb/cgi-bin目录下

arm-linux-gnueabihf-gcc test.c -o test.cgi

在浏览器中输入地址192.168.x.x/cgi-bin/test.cgi即可访问

8.GoAhead

GoAhead Webserver是为嵌入式实时操作系统(RTOS)量身定制的Web服务器。GoAhead Webserver是跨平台的服务器软件,可以稳定地运行在Windows,Linux和Mac OS X操作系统之上。GoAhead Webserver是开放源代码的,这意味着你可以随意修改Web服务器的功能。这款WEB服务器非常小巧,它的WIN CE版本编译后的大小还不到60k,它的输出通常也是面向一些小屏幕设备。在性能方面,使用一颗24MH z的68040处理器,它的响应速度为20次/秒,使用266MHz的Pentium处理器可以达到50次/秒的响应速度。

官网:https://www.embedthis.com/goahead/

文档:https://www.embedthis.com/goahead/doc/

8.1 下载源码并解压

下载地址:https://download.csdn.net/download/wyy626562203/10746788

Github:https://github.com/embedthis/goahead

mkdir -p ~/Develop/webserver/GoAhead
cd ~/Develop/webserver/GoAhead
wget https://s3.amazonaws.com/embedthis.software/goahead-4.0.2-src.tgz
tar xvf goahead-4.0.2-src.tgz

8.2 修复域名解析问题

需要修改的代码有两处,这两处的代码都有相同,在第2370和2396

if ((hp = gethostbyname(host)) == NULL) {
    error("Can't get host address for host %s: errno %d", host, errno);
    return -1;
}
memcpy((char*) &intaddr, (char *) hp->h_addr_list[0], (size_t) hp->h_length);
ddr = inet_ntoa(intaddr);
websSetIpAddr(ipaddr);
websSetHost(ipaddr);

修改成

#if 0
    if ((hp = gethostbyname(host)) == NULL) {
        error("Can't get host address for host %s: errno %d", host, errno);
        return -1;
    }
    memcpy((char*) &intaddr, (char *) hp->h_addr[0], (size_t) hp->h_length);
    ipaddr = inet_ntoa(intaddr);
#else
    int sockfd;
    struct sockaddr_in sin;
    struct ifreq ifr;
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1)
    {   
        return -1;
    }   
    strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
    ifr.ifr_name[IFNAMSIZ - 1] = 0;
    if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0)
    {   
        return -1;
    }   
    memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
    ipaddr=inet_ntoa(sin.sin_addr);
#endif
    websSetIpAddr(ipaddr);
    websSetHost(ipaddr);

8.2 编译

查看编译配置选项使用./configure -h

cd ~/Develop/webserver/GoAhead/goahead-4.0.2
make -f projects/goahead-linux-default.mk ARCH=arm PROFILE=default CC=arm-linux-gnueabihf-gcc ME_GOAHEAD_CGI=1 ME_GOAHEAD_SSL=0

8.3 配置文件

配置文件在源码目录src中“route.txt”,修改如下:

#
#   route.txt - Route and authorization configuration
#
#   Schema
#       route uri=URI protocol=PROTOCOL methods=METHODS handler=HANDLER redirect=STATUS@URI \
#           extensions=EXTENSIONS abilities=ABILITIES
#
#   Routes may require authentication and that users possess certain abilities.
#   The abilities, extensions, methods and redirect keywords use comma separated tokens to express a set of
#       required options, or use "|" separated tokens for a set of alternative options. This implements AND/OR.
#   The protocol keyword may be set to http or https. The redirect status may be "*" to match all HTTP status codes.
#   Multiple redirect fields are permissable
#
#   Examples:
#
#   Universally redirect http to https for secure communications
#       route uri=/ protocol=http redirect=*@https handler=redirect
#
#   Form based login pattern.
#       route uri=/pub/
#       route uri=/action/login methods=POST handler=action redirect=200@/ redirect=401@/pub/login.html
#       route uri=/action/logout methods=POST handler=action redirect=200@/pub/login.html
#       route uri=/ auth=form handler=continue redirect=401@/pub/login.html
#
#   Sample basic or digest authentication for user "joshua"
#       route uri=/auth/basic/ auth=basic abilities=manage
#       route uri=/auth/digest/ auth=digest abilities=manage
#
#   Eanable the PUT or DELETE methods (only) for the BIT_GOAHEAD_PUT_DIR directory
#       route uri=/put/ methods=PUT|DELETE

route uri=/old-alias/ redirect=/alias/atest.html handler=redirect

#这些目录所需的基本和数字身份验证。
#要求只有“joshua”才有的“manage”能力。
route uri=/auth/basic/admin/  auth=basic abilities=manage
route uri=/auth/digest/admin/ auth=digest abilities=manage
route uri=/auth/basic/  auth=basic abilities=view
route uri=/auth/digest/ auth=digest abilities=view

#/auth/form下内容的基于表单的身份验证
#登录表单为/auth/form/login.html。 登录时显示的页面是/auth/form/index.html
#/auth/form下的其他所有内容都是安全的,需要“manage”功能
route uri=/auth/form/login.html

route uri=/action/login methods=POST handler=action redirect=200@/auth/form/index.html redirect=401@/auth/form/login.html
route uri=/action/logout methods=GET|POST handler=action redirect=200@/auth/form/login.html
route uri=/auth/form/ auth=form handler=continue abilities=manage redirect=401@/auth/form/login.html

#仅支持BIT_GOAHEAD_PUT_DIR目录的PUT和DELETE方法
route uri=/tmp/ methods=PUT|DELETE

#要求TLS访问/secure下的任何内容
route uri=/secure/ protocol=http redirect=https handler=redirect

# 标准路由
route uri=/cgi-bin dir=/home/wyy/Develop/webserver/GoAhead/ handler=cgi
route uri=/action handler=action
route uri=/ methods=OPTIONS|TRACE handler=options
route uri=/ extensions=jst,asp handler=jst

#Catch-all路由无需对所有其他URI进行身份验证
route uri=/

配置文件在源码目录src中“auth.txt”,修改如下:

#
#auth.txt - Authorization data
#
#Schema
#  user name=NAME password=PASSWORD roles=ROLES
#  role name=NAME abilities=ABILITIES
#
#Routes (see route.txt) may require authentication and that users possess certain     abilities.
#
#Examples:
#   Define roles
#     role name=manager abilities=view,edit,delete
#
#   Define a user
#     user name=joshua password=2fd6e47ff9bb70c0465fd2f5c8e5305e roles=manager,pur    chaser
#

role name=person abilities=breathe
role name=user abilities=view,person
role name=administrator abilities=user,manage

user name=julie password=9d8873a123eb506e7f8e84d1f2a26916 roles=user
user name=joshua password=2fd6e47ff9bb70c0465fd2f5c8e5305e roles=administrator,purch   ase
user name=mary password=5b90553bea8ba3686f4239d62801f0f3 roles=user
user name=peter password=7cdba57892649fd95a540683fdf8fba6 roles=user

8.4 部署

新建一些目录

mkdir -p /home/wyy/Develop/webserver/GoAhead
mkdir /home/wyy/Develop/webserver/GoAhead/www
mkdir /home/wyy/Develop/webserver/GoAhead/bin
mkdir /home/wyy/Develop/webserver/GoAhead/conf
mkdir /home/wyy/Develop/webserver/GoAhead/cgi-bin

将上面的两个配置文件放到/home/wyy/Develop/webserver/GoAhead/conf里,将源码目录中的build/linux-arm-default下的bin目录中的所有文件拷贝到开发板对应目录下/home/wyy/Develop/webserver/GoAhead/bin,将要测试的静态页面放入到/home/wyy/Develop/webserver/GoAhead/www中。

8.4 GoAhead测试

编写测试页面放入/home/wyy/Develop/webserver/GoAhead/www

cd /home/wyy/Develop/webserver/GoAhead/bin

./goahead --route /home/wyy/Develop/webserver/GoAhead/conf/route.txt --auth /home/wyy/Develop/webserver/GoAhead/conf/auth.txt   /home/wyy/Develop/webserver/GoAhead/www 192.168.9.234:80

8.5 CGI测试

编写cgi程序

test.c

#include <stdio.h>
int main()
{
    printf("Content-type: text/html\n\n");
    printf("<html>\n");
    printf("<head>\n");
    printf("<title>CGI Output</title>\n");
    printf("</head>\n");

    printf("<body>");
    printf("<h1> Hello, world. </h1>");
    printf("</body>");
    printf("</html>\n");
    return 0;
}

编译cgi,并将test.cgi程序拷贝到开发板的/home/wyy/Develop/webserver/GoAhead/cgi-bin目录下

arm-linux-gnueabihf-gcc test.c -o test.cgi

在浏览器中输入地址192.168.x.x/cgi-bin/test.cgi即可访问

猜你喜欢

转载自blog.csdn.net/wyy626562203/article/details/83536062