MINI_httpd移植,构建小型WEB服务器

时间:2023-02-09 13:20:44

一、简介

目的:构建小型WEB站,具备SSL,解析PHP脚本(适用嵌入式环境)。

mini_httpd is a small HTTP server. Its performance is not great, but for low or medium traffic sites it's quite adequate. It implements all the basic features of an HTTP server, including:

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.

It can also be configured to do SSL/HTTPS and IPv6.

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

使用版本:mini_httpd_1.19 b修改版:http://www.hochstrasser.org/wiki/files/mini_httpd-1.19bhoc.tar.gz

php:php.4.4.2

二、步骤说明

A、解压 php.4.4.2


#cd php.4.4.2/#./configure --prefix=/usr/local/php
# make install
安装完成后把/usr/local/src/php-4.4.2/php.ini-dist复制到/usr/local/php/lib/,并重命名为php.ini

B、安装 mini_httpd-1.19

解压 mini_httpd-1.19bhoc.tar.gz 
#cd mini_httpd-1.19bhoc#vi Makefile
修改如下:

# Makefile for mini_httpd

# CONFIGURE: If you are using a SystemV-based operating system, such as

# Solaris, you will need to uncomment this definition.

#SYSV_LIBS = -lnsl -lsocket

# CONFIGURE: Some systems don't need -lcrypt, and indeed they get an

# error if you try to link with it. If you get an error about libcrypt

# not found, try commenting out this definition.

CRYPT_LIB = -lcrypt

# CONFIGURE: If you want to compile in support for https, uncomment these

# definitions. You will need to have already built OpenSSL, available at

# [url]http://www.openssl.org/[/url] Make sure the SSL_TREE definition points to the

# tree with your OpenSSL installation - depending on how you installed it,

# it may be in /usr/local instead of /usr/local/ssl.

SSL_TREE = /usr/share/ssl

SSL_DEFS = -DUSE_SSL

SSL_INC = -I${SSL_TREE}/include

SSL_LIBS = -L${SSL_TREE}/lib -lssl -lcrypto

# CONFIGURE: If you want to compile in support for PHP environment variables

# (namely PHP_AUTH_USER and PHP_AUTH_PW) then uncomment the definition below.

#PHP_ENV = -DPHP_ENV

# CONFIGURE: If you want to omit all IPV6 Support, uncomment the def below.

#IPV6_DEFS = -DNO_IPV6

BINDIR = /usr/local/sbin

MANDIR = /usr/local/man

CC = gcc

CDEFS = ${SSL_DEFS} ${SSL_INC} ${IPV6_DEFS} ${PHP_ENV}

CFLAGS = -O ${CDEFS}

#CFLAGS = -g ${CDEFS}

LDFLAGS = -s -ldl

#LDFLAGS = -g

LDLIBS = ${SSL_LIBS} ${SYSV_LIBS} ${CRYPT_LIB}

all: mini_httpd htpasswd

mini_httpd: mini_httpd.o match.o tdate_parse.o

${CC} ${CFLAGS} ${LDFLAGS} mini_httpd.o match.o tdate_parse.o ${LDLIBS} -o mini_httpd


mini_httpd.o: mini_httpd.c version.h port.h match.h tdate_parse.h mime_encodings.h mime_types.h

${CC} ${CFLAGS} -c mini_httpd.c

match.o: match.c match.h

${CC} ${CFLAGS} -c match.c

tdate_parse.o: tdate_parse.c tdate_parse.h

${CC} ${CFLAGS} -c tdate_parse.c

mime_encodings.h: mime_encodings.txt

rm -f mime_encodings.h

sed mime_encodings.h \

-e 's/#.*//' -e 's/[ ]*$$//' -e '/^$$/d' \

-e 's/[ ][ ]*/", 0, "/' -e 's/^/{ "/' -e 's/$$/", 0 },/'

mime_types.h: mime_types.txt

rm -f mime_types.h

sed mime_types.h \

-e 's/#.*//' -e 's/[ ]*$$//' -e '/^$$/d' \

-e 's/[ ][ ]*/", 0, "/' -e 's/^/{ "/' -e 's/$$/", 0 },/'

htpasswd: htpasswd.o

${CC} ${CFLAGS} ${LDFLAGS} htpasswd.o ${CRYPT_LIB} -o htpasswd

htpasswd.o: htpasswd.c

${CC} ${CFLAGS} -c htpasswd.c

cert: mini_httpd.pem

mini_httpd.pem: mini_httpd.cnf

openssl req -new -x509 -days 3650 -nodes -config mini_httpd.cnf -out mini_httpd.pem -keyout mini_httpd.pem

openssl x509 -subject -dates -fingerprint -noout -in mini_httpd.pem

chmod 600 mini_httpd.pem

install: all

rm -f ${BINDIR}/mini_httpd ${BINDIR}/htpasswd

-mkdir -p ${BINDIR}

cp mini_httpd htpasswd ${BINDIR}

rm -f ${MANDIR}/man8/mini_httpd.8 ${MANDIR}/man1/htpasswd.1
-mkdir -p ${MANDIR}/man8
cp mini_httpd.8 ${MANDIR}/man8

-mkdir -p ${MANDIR}/man1

cp htpasswd.1 ${MANDIR}/man1



clean:

rm -f mini_httpd mime_encodings.h mime_types.h htpasswd mini_httpd.rnd *.o core core.* *.core

tar:

@name=`sed -n -e '/SERVER_SOFTWARE/!d' -e 's,.*mini_httpd/,mini_httpd-,' -e 's, .*,,p' version.h` ; \

rm -rf $$name ; \

mkdir $$name ; \

tar cf - `cat FILES` | ( cd $$name ; tar xfBp - ) ; \

chmod 644 $$name/Makefile $$name/mime_encodings.txt $$name/mime_types.txt ; \

chmod 755 $$name/contrib $$name/contrib/redhat-rpm ; \
tar cf $$name.tar $$name ; \
rm -rf $$name ; \
gzip $$name.tar

修改说明:

编译支持SSL,采用redhat8 自带SSL /usr/share/ssl。
SSL_TREE = /usr/share/ssl
SSL_DEFS = -DUSE_SSL
SSL_INC = -I${SSL_TREE}/include
SSL_LIBS = -L${SSL_TREE}/lib -lssl -lcrypto

修改ssl证书有效期为10年, -days 3650

cert: mini_httpd.pem

mini_httpd.pem: mini_httpd.cnf

openssl req -new -x509 -days 3650 -nodes
#make

#make install

每次重新编译前需要运行(make clean)

--------------------------------------------------------------
生成SSL证书:
#make cert
安提示输入证书信息,即在当前目录下生成证书文件mini_httpd.pem 拷贝该文件到 /etc
#cp ./mini_httpd.pem /etc

修改建立mini_httpd.conf 配置文件

# mini_httpd configuration file

data_dir=/usr/local/www

#user=httpd

port=443

host=0.0.0.0

cgipat=**.php

logfile=/var/log/mini_httpd

pidfile=/var/run/mini_httpd.pid

charset=GB2312

ssl

certfile=/etc/mini_httpd.pem

建立php 测试脚本test.php 如下:
      #!/usr/local/php/bin/php      <?php        phpinfo();      ?>            

拷贝该脚本到,web 数据目录
      #cp ./test.php /usr/local/www
      更改文件为可执行!
      #chmod +x /usr/local/www/test.php
     --------------------------------------------------------------
     配置WEB服务网卡IP
     #ifconfig eth0 192.168.1.1 up
     启动mini_httpd
     #mini_httpd -C /etc/mini_httpd.conf


     客户端测试URL
     https://192.168.1.1/test.php
原文地址: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=711979