其他分享
首页 > 其他分享> > 小型WebServer的使用

小型WebServer的使用

作者:互联网

目录

1 thttpd

1.1 安装与基本使用

1.2 使用说明

1.3 用户验证

2 boa

2.1 参考网址

2.2 安装与基本使用

3 goAhead

3.1 参考网址

3.2 安装与使用

3.3 用户验证

4 在thttpd、boa和goAhead都能使用的样例

4.1 参考网址

4.2 基本使用


1 thttpd

可在官网下载最新版本或者从百度网盘下载作者使用的版本包。

链接:https://pan.baidu.com/s/1MDIN3UsW8yA_d0Vr1tvhHg

提取码:2g7y

1.1 安装与基本使用

tar xzf thttpd-2.29.tar.gz

cd thttpd-2.29

./configure

需要修改两个源文件,否则可能会遇到如下编译警告和运行时错误:

修改调用crypt的源文件libhttpd.c,extras/htpasswd.c,在#include <unistd.h>之前添加“#define _XOPEN_SOURCE”,在#include <unistd.h>之后添加char *crypt(const char *key, const char *salt);

make

下面是安装包里面contrib/redhat-rpm/thttpd.conf的内容,

# This section overrides defaults

dir=/home/httpd/html

chroot

user=httpd# default = nobody

logfile=/var/log/thttpd.log

pidfile=/var/run/thttpd.pid

# This section _documents_ defaults in effect

# port=80

# nosymlink# default = !chroot

# novhost

# nocgipat

# nothrottles

# host=0.0.0.0

# charset=iso-8859-1

dir字段指定WebServer的服务根目录;使用时一般注释chroot行;user字段的值为操作系统的某个用户名(比如root或者其他账户);xxxfile为日志文件,如果不想生成日志则将该字段的值改为/dev/null即可;charset字段指定字符集;

例如以下是更改后的配置文件(文件路径没变),

# This section overrides defaults

dir=/home/lijun/thttpd/html

#chroot

user=root# default = nobody

logfile=/var/log/thttpd.log

pidfile=/var/run/thttpd.pid

# This section _documents_ defaults in effect

# port=80

# nosymlink# default = !chroot

# novhost

# nocgipat

# nothrottles

# host=0.0.0.0

# charset=iso-8859-1

charset=GB2312

cgipat=/cgi-bin/*

下问提到的文件可从网盘获取,创建dir字段指定的目录,将index.html放入该目录。

gcc hellocgi.c -o hellocgi

在dir字段指定的目录之下创建cgi-bin目录并将hellocgi放入该目录中。

./thttpd -D -C contrib/redhat-rpm/thttpd.conf

可以执行./thttpd -h查看简单的使用说明,

在PC浏览器端输入服务器主机地址即可看到index.html的内容,如http://192.168.86.129;输入http://192.168.86.129/cgi-bin/hellocgi即可看到hellocgi的执行结果。

1.2 使用说明

dir字段指定的目录下没有index.html也没关系,此时会列出该目录下的所有文件。此时将aaa.txt(包含中文)放入该目录下在浏览器端也能正确显示内容(charset=GB2312起作用),刷新页面即可看到aaa.txt文件。

放入/cgi-bin/目录下的文件需要有755的访问权限,否则可能出现一些错误,比如

501 Not Implemented

The requested method 'POST' is not implemented by this server.

官网给的提示为:目录用655的权限,cgi程序用755权限,不知道cgi程序的可以自行百度一下CGI。

1.3 用户验证

thttpd还有基本的用户验证功能(Basic authentication)。需要使用htpasswd命令(位于安装包的extras目录)。

./htpasswd --help

Usage: htpasswd [-c] passwordfile username

The -c flag creates a new file.

./htpasswd -c .htpasswd testuser1,用户可以不是系统用户

Adding password for testuser1.

New password:

Re-type new password:

将.htpasswd放入配置文件dir字段指定的目录下,此时通过http://192.168.86.129访问时需要填写用户名和密码。

注意:在测试用户验证时要适时清除浏览器的历史记录和cookie。Basic authentication传输过程中不加密账户信息(此时可用WireShark抓包看到明文密码)。

2 boa

2.1 参考网址

http://www.boa.org/

https://www.linuxidc.com/Linux/2011-03/33485.htm

可在官网下载最新版本或者从百度网盘下载作者使用的版本包。

链接:https://pan.baidu.com/s/1MDIN3UsW8yA_d0Vr1tvhHg

提取码:2g7y

2.2 安装与基本使用

tar xzf boa-0.94.13.tar.gz

cd boa-0.94.13/src

./configure

make

可能出现错误 make: yacc: Command not found,则执行apt-get install flex,apt-get install bison

出现如下错误

compat.h:120:30: note: in definition of macro ‘TIMEZONE_OFFSET’

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

vim compat.h +120,将改行改为#define TIMEZONE_OFFSET(foo) foo##->tm_gmtoff(去掉两个’#’)

make clean

make

mkdir -m655 /etc/boa

cp ../boa.conf /etc/boa/

vim /etc/boa/boa.conf

将“Group nogroup”改为“Group 0”,“ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/”改为“ScriptAlias /cgi-bin/ /var/www/cgi-bin/”

mkdir -m655 /var/log/boa/

可使用 cat /etc/boa/boa.conf | grep -v '^#' 查看更改前和更改后的配置文件

下问提到的文件可从网盘获取,mkdir /var/www/,将index.html放入/var/www/

gcc hellocgi.c -o hello.cgi,chmod 777 hello.cgi

mkdir -p /var/www/cgi-bin/,chmod 777 /var/www/cgi-bin/

将hello.cgi放入/var/www/cgi-bin/

进入安装包的src,./boa,此时boa进程在后台执行,可使用ps aux | grep boa查看,

在PC浏览器端输入服务器主机地址即可看到index.html的内容,如http://192.168.86.129;输入http://192.168.86.129/cgi-bin/hello.cgi即可看到hello.cgi的执行结果。

可使用cat /var/log/boa/access_log、cat /var/log/boa/error_log查看boa运行过程中的日志。

注意:放入/cgi-bin/目录下的文件需要有755的权限,否则可能出现一些错误,比如

502 Bad Gateway

The CGI was not CGI/1.1 compliant.

3 goAhead

3.1 参考网址

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

https://blog.csdn.net/richu123/article/details/78112941

可在官网下载最新版本或者从百度网盘下载作者使用的版本包。

链接:https://pan.baidu.com/s/1MDIN3UsW8yA_d0Vr1tvhHg

提取码:2g7y

3.2 安装与使用

tar xzf goahead-4.1.0-src.tgz

cd goahead-4.1.0

make

cd build/linux-x64-default/bin/,(x64还是x86根据CPU选定)

cp ../../../src/route.txt .

cp ../../../src/auth.txt .

更改route.txt的行route uri=/cgi-bin dir=cgi-bin handler=cgi为route uri=/cgi-bin dir=$PWD/web handler=cgi,$PWD为shell命令pwd的输出,比如/home/lijun/goahead-4.1.0/build/linux-x64-default/bin

mkdir -m655 web

mkdir -m655 web/cgi-bin

下文提到的文件hellocgi.c、index.html可从网盘获取,

cd /home/lijun/goahead-4.1.0/build/linux-x64-default/bin

gcc hellocgi.c -o hello.cgi

chmod 755 hello.cgi

cp hello.cgi web/cgi-bin/

cp index.html web/

./goahead

在PC浏览器端输入服务器主机地址即可看到index.html的内容,如http://192.168.86.129;输入http://192.168.86.129/cgi-bin/hello.cgi即可看到hello.cgi的执行结果。

3.3 用户验证

goAhead支持Form Authentication、Basic Authentication(传输过程中不加密账户信息)、Digest Authentication,下面使用Basic Authentication,需要用到的工具为gopass

root@bin# ./gopass --help

goahead: 0: usage: gopass [--cipher cipher] [--file path] [--password password] realm user roles...

Options:

    --cipher md5|blowfish Select the encryption cipher. Defaults to md5

    --file filename       Modify the password file

--password password   Use the specified password

root@bin# ./gopass --cipher md5 --file ./auth.txt --password 12345 "example.com" testuser1 operator

在auth.txt加入下面一行

role name=operator abilities=stop,start,

更改route.txt中行route uri=/为route uri=/ auth=basic abilities=start,stop

网盘中可以找到更改完成之后的文件,

此时通过http://192.168.86.129或者其子路径访问时需要填写用户名(testuser1)和密码(12345)。

注意:在测试用户验证时要适时清除浏览器的历史记录和cookie。

4 在thttpd、boa和goAhead都能使用的样例

4.1 参考网址

https://www.linuxidc.com/Linux/2011-03/33485p2.htm

4.2 基本使用

下文提到的文件hello.c、hello.html可从网盘获取,

gcc hello.c -o hello.cgi

将hello.cgi和hello.html放入适当的路径下,在浏览器端输入http://192.168.86.129/hello.html即可使用(hello.cgi放入cig-bin目录,hello.html放入index.html同级目录)。

标签:cgi,bin,小型,WebServer,html,使用,boa,hello,thttpd
来源: https://blog.csdn.net/li_huaqingfeng/article/details/118422057