mini-httpd移植到ARM Linux及如何支持https
mini-httpd是拿来干嘛的?
可以用于嵌入式Linux终端设备的http服务器,以跟cgi交互,可以部署网页访问终端。
主页 https://acme.com/software/mini_httpd/
下载:
mini_httpd-1.30.tar.gz
解压命令:
tar -xvf mini_httpd-1.30.tar.gz
移植:mini-httpd并没有使用类似其他开源代码那样使用configure,
因为他的移植编译需要我们修改他的Makefile文件;
cat 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
# http://www.openssl.org/ 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/local/ssl
#SSL_DEFS = -DUSE_SSL
#SSL_INC = -I$(SSL_TREE)/include
#SSL_LIBS = -L$(SSL_TREE)/lib -lssl -lcryptoBINDIR = /usr/local/sbin
MANDIR = /usr/local/man
CC = cc
CDEFS = $(SSL_DEFS) $(SSL_INC)
CFLAGS = -O $(CDEFS) -ansi -pedantic -U__STRICT_ANSI__ -Wall -Wpointer-arith -Wshadow -Wcast-qual -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wno-long-long
LDFLAGS = -s
LDLIBS = $(CRYPT_LIB) $(SSL_LIBS) $(SYSV_LIBS)all: mini_httpd htpasswdmini_httpd: mini_httpd.o match.o tdate_parse.o$(CC) $(LDFLAGS) mini_httpd.o match.o tdate_parse.o $(LDLIBS) -o mini_httpdmini_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.cmatch.o: match.c match.h$(CC) $(CFLAGS) -c match.ctdate_parse.o: tdate_parse.c tdate_parse.h$(CC) $(CFLAGS) -c tdate_parse.cmime_encodings.h: mime_encodings.txtrm -f mime_encodings.hsed < mime_encodings.txt > mime_encodings.h \-e 's/#.*//' -e 's/[ ]*$$//' -e '/^$$/d' \-e 's/[ ][ ]*/", 0, "/' -e 's/^/{ "/' -e 's/$$/", 0 },/'mime_types.h: mime_types.txtrm -f mime_types.hsed < mime_types.txt > mime_types.h \-e 's/#.*//' -e 's/[ ]*$$//' -e '/^$$/d' \-e 's/[ ][ ]*/", 0, "/' -e 's/^/{ "/' -e 's/$$/", 0 },/'htpasswd: htpasswd.o$(CC) $(LDFLAGS) htpasswd.o $(CRYPT_LIB) -o htpasswdhtpasswd.o: htpasswd.c$(CC) $(CFLAGS) -c htpasswd.ccert: mini_httpd.pem
mini_httpd.pem: mini_httpd.cnfopenssl req -new -x509 -days 3650 -nodes -config mini_httpd.cnf -out mini_httpd.pem -keyout mini_httpd.pemopenssl x509 -subject -dates -fingerprint -noout -in mini_httpd.pemchmod 600 mini_httpd.peminstall: allrm -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)/man8cp mini_httpd.8 $(MANDIR)/man8-mkdir -p $(MANDIR)/man1cp htpasswd.1 $(MANDIR)/man1clean:rm -f mini_httpd mime_encodings.h mime_types.h htpasswd mini_httpd.rnd *.o core core.* *.coretar:@name=`sed -n -e '/#define 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
如上;mini-httpd的Makefile很简单;
把makefile改成如下:
CURRENT_DIR := $(CURDIR)
BINDIR = $(CURRENT_DIR)/usr/sbin
MANDIR = $(CURRENT_DIR)/usr/man
CC = arm-nuvoton-linux-gnueabi-gcc
arm-nuvoton-linux-gnueabi-gcc是我的交叉工具链,需要提前设置一下环境变量或者你直接复制全路径
执行如下命令:
make allmake install
root@ubuntu:~/share/gateway/app/openlib/mini_httpd-1.30/usr# ls
man sbin
root@ubuntu:~/share/gateway/app/openlib/mini_httpd-1.30/usr# tree
.
├── man
│ ├── man1
│ │ └── htpasswd.1
│ └── man8
│ └── mini_httpd.8
└── sbin├── htpasswd└── mini_httpd
为什么改成当前目录下创建usr目录下生成bin文件:因为方便拷贝到开发板及版本制作目录
root@ubuntu:~/share/gateway/app/openlib/mini_httpd-1.30# scp -r ../mini_httpd-1.30/usr/* root@192.168.2.99:/usr/
root@192.168.2.99's password:
htpasswd.1 100% 414 0.4KB/s 00:00
mini_httpd.8 100% 16KB 16.2KB/s 00:00
htpasswd 100% 6708 6.6KB/s 00:00
mini_httpd 100% 45KB 45.3KB/s 00:00
root@ubuntu:~/share/gateway/app/openlib/mini_httpd-1.30#
网页目录:
mkdir www
cgi的bin文件目录:
cd www
mkdir cgi-bin
配置:
vi /etc/mini-httpd.conf
编辑mini-httpd配置文件mini-httpd.conf ,先仅支持http(文章后面讲https)
# Example config for mini_httpd.# Uncomment this line for turning on ssl support.
#ssl# On which host mini_httpd should bind?
host=0.0.0.0# On which port mini_httpd should listen?
#port=443
port=80
# Which user mini_httpd should use?
user=root# Run in chroot mode?
#chroot # yes
#nochroot # no# Working directory of mini_httpd.
#dir=<work_dir># We are the web files stored?
data_dir=/root/www/# Which certificate to use?
#certfile=/etc/mini_httpd.pem# Which logfile to use?
logfile=/var/log/mini-httpd.log# Which pidfile to use?
pidfile=/var/run/mini-httpd.pid# Which charset to use?
charset=UTF-8cgipat=cgi-bin/*
运行:
/usr/sbin/mini_httpd -C /etc/mini-httpd.conf &
电脑访问:
http://终端ip
即可访问
重头戏来了,很多客户有加密链接的需求,即https访问;
如何支持https
makefile 修改
SSL_TREE = /root/share/nuvoton_cross/host/usr/arm-nuvoton-linux-gnueabi/sysroot
SSL_DEFS = -DUSE_SSL
SSL_INC = -I$(SSL_TREE)/include
SSL_LIBS = -L$(SSL_TREE)/lib -lssl -lcrypto
make clean
make all
make install
编译ok
生成证书
make cert
将
scp -r ../mini_httpd-1.30/usr/* root@192.168.2.99:/usr/
cp ../mini_httpd-1.30/usr/* /root/share/nuvoton-build-rootfs/buildroot_out/202401012033/rootfs/usr/ -rf
将mini_httpd.pem拷贝到开发板的/etc/打开https的话 将开发板的mini_httpd的配置文件/etc/mini-httpd.conf编辑如下:
# Example config for mini_httpd.# Uncomment this line for turning on ssl support.
ssl# On which host mini_httpd should bind?
host=0.0.0.0# On which port mini_httpd should listen?
port=443
#port=80
# Which user mini_httpd should use?
user=root# Run in chroot mode?
#chroot # yes
#nochroot # no# Working directory of mini_httpd.
#dir=<work_dir># We are the web files stored?
data_dir=/root/www/# Which certificate to use?
certfile=/etc/mini_httpd.pem# Which logfile to use?
logfile=/var/log/mini-httpd.log# Which pidfile to use?
pidfile=/var/run/mini-httpd.pid# Which charset to use?
charset=UTF-8cgipat=cgi-bin/*
=========================================
该配置文件与http的区别3个地方:
(1)ssl的注释打开
(2)port=443 而http的一般为port=80
(3)certfile=/etc/mini_httpd.pem //指定证书的位置
把开发板的文件放到以后打包版本的地方
scp /etc/mini-httpd.conf root@192.168.2.240:/root/share/nuvoton-build-rootfs/buildroot_out/202401012033/rootfs/etc/
cp ../mini_httpd-1.30/mini_httpd.pem /root/share/nuvoton-build-rootfs/buildroot_out/202401012033/rootfs/etc/mini_httpd.pem -rf