我的openwrt学习笔记(二十九)webserver之 thttpd

举报
aiot_bigbear 发表于 2022/09/25 01:11:36 2022/09/25
【摘要】 我的openwrt学习笔记(二十八)webserver之thttpd   thttpd WEBSERVER安装 thttpd - tiny/turbo/throttling HTTP server  thttpd中是一个简单,小型,轻便,快速和安全的http服务器: 简单:它能够支持HTTP/1.1协议标准,...

我的openwrt学习笔记(二十八)webserver之thttpd

 

thttpd WEBSERVER安装

thttpd - tiny/turbo/throttling HTTP server 
thttpd中是一个简单,小型,轻便,快速和安全的http服务器:
简单:它能够支持HTTP/1.1协议标准,或者超过了最低水平
小巧:它具有非常少的运行时间,因为它不fork子进程来接受新请求,并且非常谨慎的分配内存(性能对比表:http://www.acme.com/software/thttpd/benchmarks.html)
便携:它能够在大部分的类Unix系统上运行,包括FreeBSD, SunOS 4, Solaris 2, BSD/OS, Linux, OSF等等
快速:它的速度要超过主流的Web服务器(Apache, NCSA, Netscape),在高负载情况下,它要快的多
安全:它努力的保护主机不受到攻击,不中断服务器

thttpd 类似于lighttpd,对于并发请求不使用fork()来派生子进程处理,而是采用多路复用(Multiplex)技术来实现。因此效能很好。同时它还有一个特点就是基于URL的文件流量限制,这对于下载的流量控制而言是非常方便的。象Apache就必须使用插件实现,效率较thttpd低。
thttpd跟lighttpd类似,适合静态资源类的服务,比如图片、资源文件、静态HTML等等的应用,性能应该比较好,同时也适合简单的CGI应用的场合。

官方地址:http://www.acme.com/software/thttpd
下载地址:http://www.acme.com/software/thttpd/thttpd-2.25b.tar.gz

一.安装

# wget http://www.acme.com/software/thttpd/thttpd-2.25b.tar.gz

# tar zxvf thttpd-2.25b.tar.gz

# cd thttpd-2.25b

 

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ ./configure --version

configure generated by autoconf version 2.13

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ ./configure --prefix=/usr/local/thttpd

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ make

….

htpasswd.c:52:12: error: conflicting types for ‘getline’

/usr/include/stdio.h:675:20: note: previous declaration of ‘getline’ was here

htpasswd.c: In function ‘main’:

htpasswd.c:216:11: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]

htpasswd.c: In function ‘add_password’:

htpasswd.c:117:2: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result [-Wunused-result]

make[1]: *** [htpasswd.o] Error 1

make[1]: Leaving directory `/home/linux/linux_c/thttpd/thttpd-2.25b/extras'

make: *** [subdirs] Error 2

 

修改方法:

htpasswd.c中的名称与stdio.h标准库中的getline的名称一样,所以导致了冲突。

修改的方法是到htpasswd.c中,修改getlinehtpasswd.cgetline是一个static类型的函数,函数只会出现在本文件中。依次查找getline,并修改成get_line



htpasswd.c 52行定义

static int getline(char *s, int n, FILE *f) {

htpasswd.c 192行调用

while(!(getline(line,MAX_STRING_LEN,f))) {

重新make

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ make

E_ATOLL=1 -DHAVE_UNISTD_H=1 -DHAVE_GETPAGESIZE=1 -DHAVE_MMAP=1 -DHAVE_SELECT=1 -DHAVE_POLL=1 -DHAVE_TM_GMTOFF=1 -DHAVE_INT64T=1 -DHAVE_SOCKLENT=1  -I..-DWEBDIR=\"/usr/local/thttpd/www\" -c htpasswd.c

htpasswd.c: In function ‘main’:

htpasswd.c:216:11: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]

htpasswd.c: In function ‘add_password’:

htpasswd.c:117:2: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result [-Wunused-result]

gcc  -static htpasswd.o -o htpasswd -lcrypt

make[1]: Leaving directory `/home/linux/linux_c/thttpd/thttpd-2.25b/extras'

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ file thttpd

thttpd: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x4be9c679d8707115a3c4c9bba60dea3b7bffac2b, not stripped

 

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ make install

mkdir -p /usr/local/thttpd/sbin

mkdir: cannot create directory `/usr/local/thttpd': Permission denied

make: [installthis] Error 1 (ignored)

/usr/bin/install -c -m 555 -o bin -g bin thttpd /usr/local/thttpd/sbin

/usr/bin/install: cannot create regular file `/usr/local/thttpd/sbin': No such file or directory

make: *** [installthis] Error 1

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ sudo make install

[sudo] password for linux:

mkdir -p /usr/local/thttpd/sbin

/usr/bin/install -c -m 555 -o bin -g bin thttpd /usr/local/thttpd/sbin

mkdir -p /usr/local/thttpd/man/man8

/usr/bin/install -c -m 444 -o bin -g bin thttpd.8 /usr/local/thttpd/man/man8

for i in cgi-src extras ; do ( \

             cd $i ; \

             pwd ; \

             make  \

                   WEBDIR=/usr/local/thttpd/www \

                   CGIBINDIR=/usr/local/thttpd/www/cgi-bin \

                   MANDIR=/usr/local/thttpd/man \

                   WEBGROUP=www \

                   install \

         ) ; done

/home/linux/linux_c/thttpd/thttpd-2.25b/cgi-src

make[1]: Entering directory `/home/linux/linux_c/thttpd/thttpd-2.25b/cgi-src'

mkdir -p /usr/local/thttpd/www/cgi-bin

rm -f /usr/local/thttpd/www/cgi-bin/redirect

cp redirect /usr/local/thttpd/www/cgi-bin/redirect

rm -f /usr/local/thttpd/man/man8/redirect.8

cp redirect.8 /usr/local/thttpd/man/man8/redirect.8

rm -f /usr/local/thttpd/www/cgi-bin/ssi

cp ssi /usr/local/thttpd/www/cgi-bin/ssi

rm -f /usr/local/thttpd/man/man8/ssi.8

cp ssi.8 /usr/local/thttpd/man/man8/ssi.8

rm -f /usr/local/thttpd/www/cgi-bin/phf

cp phf /usr/local/thttpd/www/cgi-bin/phf

make[1]: Leaving directory `/home/linux/linux_c/thttpd/thttpd-2.25b/cgi-src'

/home/linux/linux_c/thttpd/thttpd-2.25b/extras

make[1]: Entering directory `/home/linux/linux_c/thttpd/thttpd-2.25b/extras'

rm -f /usr/local/thttpd/sbin/makeweb /usr/local/thttpd/sbin/htpasswd /usr/local/thttpd/sbin/syslogtocern

cp makeweb /usr/local/thttpd/sbin/makeweb

chgrp www /usr/local/thttpd/sbin/makeweb

chgrp: invalid group: `www'

make[1]: *** [install] Error 1

make[1]: Leaving directory `/home/linux/linux_c/thttpd/thttpd-2.25b/extras'

make: *** [installsubdirs] Error 2

 

增加组


linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ cat /etc/group

root:x:0:

daemon:x:1:

bin:x:2:

sys:x:3:

adm:x:4:linux

tty:x:5:

disk:x:6:

lp:x:7:

mail:x:8:

news:x:9:

uucp:x:10:

man:x:12:

proxy:x:13:

kmem:x:15:

dialout:x:20:

fax:x:21:

voice:x:22:

cdrom:x:24:linux

floppy:x:25:

tape:x:26:

sudo:x:27:linux

audio:x:29:pulse

dip:x:30:linux

www-data:x:33:

backup:x:34:

operator:x:37:

list:x:38:

irc:x:39:

src:x:40:

gnats:x:41:

shadow:x:42:

utmp:x:43:

video:x:44:

sasl:x:45:

plugdev:x:46:linux

staff:x:50:

games:x:60:

users:x:100:

nogroup:x:65534:

libuuid:x:101:

crontab:x:102:

syslog:x:103:

fuse:x:104:

messagebus:x:105:

bluetooth:x:106:

scanner:x:107:

colord:x:108:

lpadmin:x:109:linux

ssl-cert:x:110:

lightdm:x:111:

nopasswdlogin:x:112:

netdev:x:113:

whoopsie:x:114:

mlocate:x:115:

ssh:x:116:

avahi-autoipd:x:117:

avahi:x:118:

pulse:x:119:

pulse-access:x:120:

utempter:x:121:

rtkit:x:122:

saned:x:123:

linux:x:1000:

sambashare:x:124:linux

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ addgroup www

addgroup: Only root may add a user or group to the system.

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ which addgroup

/usr/sbin/addgroup

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ sudo addgroup www

Adding group `www' (GID 1001) ...

Done.

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ cat /etc/group

root:x:0:

daemon:x:1:

bin:x:2:

sys:x:3:

adm:x:4:linux

tty:x:5:

disk:x:6:

lp:x:7:

mail:x:8:

news:x:9:

uucp:x:10:

man:x:12:

proxy:x:13:

kmem:x:15:

dialout:x:20:

fax:x:21:

voice:x:22:

cdrom:x:24:linux

floppy:x:25:

tape:x:26:

sudo:x:27:linux

audio:x:29:pulse

dip:x:30:linux

www-data:x:33:

backup:x:34:

operator:x:37:

list:x:38:

irc:x:39:

src:x:40:

gnats:x:41:

shadow:x:42:

utmp:x:43:

video:x:44:

sasl:x:45:

plugdev:x:46:linux

staff:x:50:

games:x:60:

users:x:100:

nogroup:x:65534:

libuuid:x:101:

crontab:x:102:

syslog:x:103:

fuse:x:104:

messagebus:x:105:

bluetooth:x:106:

scanner:x:107:

colord:x:108:

lpadmin:x:109:linux

ssl-cert:x:110:

lightdm:x:111:

nopasswdlogin:x:112:

netdev:x:113:

whoopsie:x:114:

mlocate:x:115:

ssh:x:116:

avahi-autoipd:x:117:

avahi:x:118:

pulse:x:119:

pulse-access:x:120:

utempter:x:121:

rtkit:x:122:

saned:x:123:

linux:x:1000:

sambashare:x:124:linux

www:x:1001:

 

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ cd /usr/local/thttpd/man/

linux@ubuntu:/usr/local/thttpd/man$

linux@ubuntu:/usr/local/thttpd/man$ ls -l

total 4

drwxr-xr-x 2 root root 4096 Sep 10 17:31 man8

linux@ubuntu:/usr/local/thttpd/man$ sudo mkdir -p man1

linux@ubuntu:/usr/local/thttpd/man$ ls -l

total 8

drwxr-xr-x 2 root root 4096 Sep 10 17:32 man1

drwxr-xr-x 2 root root 4096 Sep 10 17:31 man8

linux@ubuntu:/usr/local/thttpd/man$ cd -

/home/linux/linux_c/thttpd/thttpd-2.25b

linux@ubuntu:~/linux_c/thttpd/thttpd-2.25b$ sudo make install

mkdir -p /usr/local/thttpd/sbin

/usr/bin/install -c -m 555 -o bin -g bin thttpd /usr/local/thttpd/sbin

mkdir -p /usr/local/thttpd/man/man8

/usr/bin/install -c -m 444 -o bin -g bin thttpd.8 /usr/local/thttpd/man/man8

for i in cgi-src extras ; do ( \

             cd $i ; \

             pwd ; \

             make  \

                   WEBDIR=/usr/local/thttpd/www \

                   CGIBINDIR=/usr/local/thttpd/www/cgi-bin \

                   MANDIR=/usr/local/thttpd/man \

                   WEBGROUP=www \

                   install \

         ) ; done

/home/linux/linux_c/thttpd/thttpd-2.25b/cgi-src

make[1]: Entering directory `/home/linux/linux_c/thttpd/thttpd-2.25b/cgi-src'

mkdir -p /usr/local/thttpd/www/cgi-bin

rm -f /usr/local/thttpd/www/cgi-bin/redirect

cp redirect /usr/local/thttpd/www/cgi-bin/redirect

rm -f /usr/local/thttpd/man/man8/redirect.8

cp redirect.8 /usr/local/thttpd/man/man8/redirect.8

rm -f /usr/local/thttpd/www/cgi-bin/ssi

cp ssi /usr/local/thttpd/www/cgi-bin/ssi

rm -f /usr/local/thttpd/man/man8/ssi.8

cp ssi.8 /usr/local/thttpd/man/man8/ssi.8

rm -f /usr/local/thttpd/www/cgi-bin/phf

cp phf /usr/local/thttpd/www/cgi-bin/phf

make[1]: Leaving directory `/home/linux/linux_c/thttpd/thttpd-2.25b/cgi-src'

/home/linux/linux_c/thttpd/thttpd-2.25b/extras

make[1]: Entering directory `/home/linux/linux_c/thttpd/thttpd-2.25b/extras'

rm -f /usr/local/thttpd/sbin/makeweb /usr/local/thttpd/sbin/htpasswd /usr/local/thttpd/sbin/syslogtocern

cp makeweb /usr/local/thttpd/sbin/makeweb

chgrp www /usr/local/thttpd/sbin/makeweb

chmod 2755 /usr/local/thttpd/sbin/makeweb

cp htpasswd /usr/local/thttpd/sbin/htpasswd

cp syslogtocern /usr/local/thttpd/sbin/syslogtocern

rm -f /usr/local/thttpd/man/man1/makeweb.1

cp makeweb.1 /usr/local/thttpd/man/man1/makeweb.1

rm -f /usr/local/thttpd/man/man1/htpasswd.1

cp htpasswd.1 /usr/local/thttpd/man/man1/htpasswd.1

rm -f /usr/local/thttpd/man/man8/syslogtocern.8

cp syslogtocern.8 /usr/local/thttpd/man/man8/syslogtocern.8

make[1]: Leaving directory `/home/linux/linux_c/thttpd/thttpd-2.25b/extras'

 

OK 终于没有报错了

拷贝thttpd配置文件

contrib/redhat-rpm/thttpd.conf/

启动thttpd

/usr/local/thttpd/sbin/thttpd -C /usr/local/thttpd/conf/thttpd.conf

thttpd服务后出现/usr/sbin/thttpd: unknown user - 'httpd'错误提示

 

linux@ubuntu:/usr/local/thttpd$ ls -l

total 16

drwxr-xr-x 2 root root 4096 Sep 10 17:58 conf

drwxr-xr-x 4 root root 4096 Sep 10 17:32 man

drwxr-xr-x 2 root root 4096 Sep 10 17:33 sbin

drwxr-xr-x 3 root root 4096 Sep 10 17:26 www

linux@ubuntu:/usr/local/thttpd$ pwd

/usr/local/thttpd

linux@ubuntu:/usr/local/thttpd$ cd conf/

linux@ubuntu:/usr/local/thttpd/conf$ pwd

/usr/local/thttpd/conf

linux@ubuntu:/usr/local/thttpd/conf$ ls -l

total 4

-r--r--r-- 1 root root 305 Sep 10 17:59 thttpd.conf

linux@ubuntu:/usr/local/thttpd/conf$ cd ../

linux@ubuntu:/usr/local/thttpd$ chown -R linux:linux conf/

chown: changing ownership of `conf/thttpd.conf': Operation not permitted

chown: changing ownership of `conf/': Operation not permitted

linux@ubuntu:/usr/local/thttpd$ sudo chown -R linux:linux conf/

linux@ubuntu:/usr/local/thttpd$ ls -l

total 16

drwxr-xr-x 2 linux linux 4096 Sep 10 17:59 conf

drwxr-xr-x 4 root  root  4096 Sep 10 17:32 man

drwxr-xr-x 2 root  root  4096 Sep 10 17:33 sbin

drwxr-xr-x 3 root  root  4096 Sep 10 17:26 www

linux@ubuntu:/usr/local/thttpd$ cd conf/

linux@ubuntu:/usr/local/thttpd/conf$ ls

thttpd.conf

linux@ubuntu:/usr/local/thttpd/conf$ ls -l

total 4

-r--r--r-- 1 linux linux 305 Sep 10 17:59 thttpd.conf

linux@ubuntu:/usr/local/thttpd/conf$ chmod 777 thttpd.conf

linux@ubuntu:/usr/local/thttpd/conf$ ls -l

total 4

-rwxrwxrwx 1 linux linux 305 Sep 10 17:59 thttpd.conf

linux@ubuntu:/usr/local/thttpd/conf$ vi thttpd.conf

linux@ubuntu:~/linux_c/thttpd$ cat /usr/local/thttpd/conf/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

 port=81

# nosymlink# default = !chroot

# novhost

# nocgipat

# nothrottles

# host=0.0.0.0

# charset=iso-8859-1

 

linux@ubuntu:~/linux_c/thttpd$ cat in.sh

#!/bin/sh

 

echo "start thhtp conf"

mkdir -p /var/log 

mkdir -p /var/run 

sudo touch /var/log/thttpd.log 

sudo touch /var/run/thttpd.pid

 

linux@ubuntu:~/linux_c/thttpd$ chmod +x in.sh

linux@ubuntu:~/linux_c/thttpd$ ./in.sh

start thhtp conf

linux@ubuntu:~/linux_c/thttpd$ /usr/local/thttpd/sbin/thttpd -C /usr/local/thttpd/conf/thttpd.conf

/var/log/thttpd.log: Permission denied

linux@ubuntu:~/linux_c/thttpd$ sudo /usr/local/thttpd/sbin/thttpd -C /usr/local/thttpd/conf/thttpd.conf

linux@ubuntu:~/linux_c/thttpd$ ps -aux|grep thttpd

Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html

httpd     3191  0.0  0.1   3020  1084 ?        Ss   18:10   0:00 /usr/local/thttpd/sbin/thttpd -C /usr/local/thttpd/conf/thttpd.conf

linux     3194  0.0  0.0   4368   828 pts/1    S+   18:10   0:00 grep --color=auto thttpd

thttpdWeb Server

linux@ubuntu:/home/httpd/html$ tree

.

├── cgi-bin

│   ├── hello.c

│   ├── hello.cgi

│   ├── mul.html

│   ├── mult.c

│   └── mult.cgi

├── index.html

├── index.lighttpd.html

├── mul.html

└── test.html

 

1 directory, 9 files

linux@ubuntu:/home/httpd/html$ cat hello.html

<html>

<head>

<meta charset="UTF-8">

<title> 主标题 | 副标题</title>

</head>

<body>

<p>hello world</p>

</body>

</html>

浏览器输入,就可以看到网页界面了。

http://localhost:81/index.html

 

http://192.168.11.126:81/hello.html

 

OK 终于没有报错了

 

 

CGI HTML测试

修改thttpd.conf

linux@ubuntu:~/linux_c/thttpd$ cat /usr/local/thttpd/conf/thttpd.conf

# This section overrides defaults

dir=/home/httpd/html

#chroot

#chroot

#屏蔽chroot是为了运行动态编译的CGI

user=httpd# default = nobody

logfile=/var/log/thttpd.log

pidfile=/var/run/thttpd.pid

# This section _documents_ defaults in effect

# port=80

 port=81

# nosymlink# default = !chroot

# novhost

# nocgipati

#cgipat=**.cgi|**.pl

cgipat=/cgi-bin/**.cgi

# nothrottles

# host=0.0.0.0

# charset=iso-8859-1

到了这里,当我输入目标板ip时,问题出现了:

403 Forbidden

The requested URL '/mul.html' resolves to a file which is marked executable but is not a CGI file; retrieving it is forbidden.

thttpd/2.25b 29dec2003


原因:由于linux文件系统的权限问题,非cgi的文件请使用chmod -x去掉其可执行权限,否则web服务器会认为它是cgi ( 比如index.html这样的文件 )

thttpd web对目录和文件的访问权限要求非常严格,在测试的时候,已经心有体会。

 

在目标板的/var目录下添加两个文件:

[plain] view plaincopyprint?

1.       $ mkdir -p /var/log  

2.       $ mkdir -p /var/run  

3.       $ sudo touch /var/log/thttpd.log  

4.       $ sudo touch /var/run/thttpd.pid  

再次重启目标板,待进入shell界面,从主机的浏览器中输入:

[plain] view plaincopyprint?

1.       目标板IP:port/html文件名  

在本人所用的调试环境下,浏览器地址栏输入:

[plain] view plaincopyprint?

1.       192.168.37.244:81/a.html  

 

 

 

文章来源: blog.csdn.net,作者:悟空胆好小,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/xushx_bigbear/article/details/48379893

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。