arm 移植 lighttpd + CGI 配置

--- title: arm 移植 lighttpd + CGI 配置 EntryName: porting-lighttpd-on-arm-and-make-cgi-config date: 2020-07-31 03:14:23 categories: tags: - arm - web - lighttpd - cgi - config ---

章节描述:

介绍 lighttpd 的移植,以及 CGI 配置

lighttpd介绍

lighttpd is a secure, fast, compliant, and very flexible web-server that has been optimized for high-performance environments. It has a very low memory footprint compared to other webservers and takes care of cpu-load. Its advanced feature-set (FastCGI, CGI, Auth, Output-Compression, URL-Rewriting and many more) make lighttpd the perfect webserver-software for every server that suffers load problems.

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

维基百科:

背景

需要一个arm平台的方案。

version:

  • lighttpd 1.4.55
  • gcc-3.4.5-glibc-2.3.6/arm-softfloat (采用cross tool制作)
#!/bin/bash
export CC=arm-softfloat-linux-gnu-gcc
export AR=arm-softfloat-linux-gnu-ar
export LD=arm-softfloat-linux-gnu-ld
export RANLIB=arm-softfloat-linux-gnu-ranlib
export STRIP=arm-softfloat-linux-gnu-strip 


./configure --prefix=/usr/local/lighttpd-arm --host=arm-softfloat-linux-gnu  --build=i686-pc-linux --disable-FEUTARE --disable-ipv6 --disable-lfs  --without-bzip2
make 
sudo make install

#--prefix 为安装目录(主机&目标板一致)

我的移植方案:

##
#    Copyright By Schips, All Rights Reserved
#    https://gitee.com/schips/
#
#    File Name:  make.sh
#    Created  :  2020-07-31 09:38:39
#
##
#!/bin/sh

BASE=`pwd`
#BUILD_HOST=arm-linux
# 最终的运行环境
INSTALL=${BASE}/install
FIN_INSTALL=/usr/local/lighttpd


make_dirs() {
    cd ${BASE}
    mkdir  compressed  install  source -p
}

tget () { #try wget
    filename=`basename $1`
    echo "Downloading [${filename}]..."
    if [ ! -f ${filename} ];then
        wget $1
    fi

    echo "[OK] Downloaded [${filename}] "
}

download_package () {
    cd ${BASE}/compressed
    #下载包
    tget https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.55.tar.gz
}

tar_package () {
    cd ${BASE}/compressed
    ls * > /tmp/list.txt
    for TAR in `cat /tmp/list.txt`
    do
        tar -xf $TAR -C  ../source
    done
    rm -rf /tmp/list.txt
}

set_compile_env () {
    #GCC_PATH=`whereis gcc | awk -F: '{print $2}' | awk '{print $1}' | xargs dirname`
    #export PATH=$PATH:$GCC_PATH
	export CC=${BUILD_HOST}gcc
	export AR=${BUILD_HOST}ar
	export LD=${BUILD_HOST}ld
	export RANLIB=${BUILD_HOST}ranlib
	export STRIP=${BUILD_HOST}strip
}

make_taget () {
    cd ${BASE}/source/*
    ./configure --disable-ipv6 --disable-lfs  --without-bzip2 \
        --prefix=${FIN_INSTALL}  && echo "${FIN_INSTALL} with ${BUILD_HOST}gcc" > ${BASE}/ccinfo
    make 
}

make_install () {
    mkdir ${BASE}/install/lib  -p
    mkdir ${BASE}/install/sbin -p
    cd ${BASE}/source/*
    SRCTOP=`pwd`
    echo "${FIN_INSTALL} with ${BUILD_HOST}gcc" > ${BASE}/ccinfo
    cp ${BASE}/ccinfo               ${BASE}/install
    cp $SRCTOP/src/.libs/*.so       ${BASE}/install/lib  -r
    cp $SRCTOP/src/lighttpd-angel   ${BASE}/install/sbin
    cp $SRCTOP/src/lighttpd         ${BASE}/install/sbin
    #cp `find . -type f -name "lighttpd.conf" 2> /dev/null | grep doc` -v ${BASE}/install/sbin
    cp $SRCTOP/doc/config  -r       ${BASE}/install/
    rm  ${BASE}/install/config/Make*
}

make_dirs
set_compile_env
#tar_package
#make_taget
make_install
exit $?

配置 lighttpd.conf

一般配置

修改工作路径:

server.document-root        = "/home/xx/lighttpd/"

关闭http范围请求:

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

修改组:

lighttpd.conf文件中:注释掉username行,

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

否则会出现:

can't find username lighttpd

其他:

文件系统不能读写的话用#号注释掉这两个选项:

#server.errorlog  
#accesslog.filename

CGI支持

CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上,提供同客户端 HTML 页面的接口。

我们这里以自定义的cgi程序处理为例子。

确定目录结构

假定目录位于/home/xx/lighttpd/,目录结构如下:

/home/xx/lighttpd/install
├── config
├───lighttpd.conf
│   ├── conf.d
│   └── vhosts.d
├── lib
└── sbin

配置 lighttpd

说明:

由于这个版本的lighttpd是很新的一个版本,很多文章的配置都有一些变化。但是实际上是类似的。

新版lighttpd.conf中,include了很多子配置,例如include "modules.conf"

如果需要配置cgi,一般在modules.conf中使能include conf.d/cgi.conf比较合理。

当然,也可以在lighttpd.conf中注释掉include "modules.conf",将所有的配置写在lighttpd.conf里面。

编辑conf.d/cgi.conf

# 确保 此行有效:
server.modules += ( "mod_cgi" )

# 修改 cgi.assign
	## 注:对于带扩展名且需要特定解析程序执行的CGI,可以指定解析程序的路径

cgi.assign                 = ( ".pl"  => "/usr/bin/perl",
                               ".rb"  => "/usr/bin/ruby",
                               ".erb" => "/usr/bin/eruby",
	## 对于带扩展名却不需要特定解析程序就能执行的CGI,可指定解析程序为空,此时可由html中指定需要执行的cgi程序。(例如下面这行)
                               # 新增这一行
                               ".cgi" => "",
                               
                               ".py"  => "/usr/bin/python" )
                               
	## 对于不带扩展名的CGI程序,只能通过固定路径存取了,如:
	##   cgi.assgin = ( "/cgi-bin/mycgi" => "/usr/local/cgi/mycgi )

编写网页

为了方便,以index.html修改,内容为:

注意其中的Action指定的路径要与实际上的服务器工作路径相对应。

我们这里将请求发送给cgi-bin下的程序

  • mult_sh.cgi :由sh处理
  • mult_c.cgi : 由c程序处理
<html>
	<head>
		<meta charset="utf-8">
		<title>测试</title>
	</head>
	<body>
        <form name="form1" ACTION="/cgi-bin/mult_sh.cgi">
			<P>计算两个数的乘积,请输入两个乘数。
			<INPUT NAME="m" SIZE="5">
			<INPUT NAME="n" SIZE="5"><BR>
			<INPUT TYPE="SUBMIT" values="提交">
		</form>
	</body>
</html>

创建cgi目录

假设新建的目录名为:cgi-bin,此时的目录如下:

/home/xx/lighttpd/install
├─▲ cgi-bin
├── config
│   ├── conf.d
│   └── vhosts.d
├── lib
└── sbin

编写自定义cgi程序

cgi程序可以有很多种写法,这里以bash与c语言为例。

程序需要放在/home/xx/lighttpd/install/cgi-bin中,这是由html文件指定的。

c程序

这段C程序会处理网络请求,并做正确的运算。

/*
#    Copyright By Schips, All Rights Reserved
#    https://gitee.com/schips/
#
#    File Name:  mult_c.c
#    Created  :  2020-07-31 14:20:53
*/

#include <stdio.h>
#include <stdlib.h>

int main(void)
{ 
    char *data;
    long m,n;

    printf("%s\n\n","Content-Type:text/html;charset=utf8");
    printf("<TITLE>乘法结果</TITLE> ");
    printf("<H3>乘法结果</H3> ");

    data = getenv("QUERY_STRING"); //获得form传来的参数——两个乘数m和n
    if(data == NULL)
        printf("<P>错误!数据没有被输入或者数据传输有问题");
    else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
        printf("<P>错误!输入数据非法。表单中输入的必须是数字。");
    else
        printf("<P>%ld和%ld的乘积是:%ld。",m,n,m*n);
    return 0;
}

编译:

注意,执行文件的后缀名为配置好的.cgi,否则会变成文件传输。

gcc mult_c.c -o mult_c.cgi
脚本
##
#    Copyright By Schips, All Rights Reserved
#    https://gitee.com/schips/
#
#    File Name:  mult_sh.cgi
#    Created  :  2020-07-31 14:18:24
#
##
#!/bin/sh

# 获取网络传过来的参数
str=$QUERY_STRING


echo "Content-type: text/html" # HTTP头部的一部分,告诉浏览器文件的内容类型。
echo # 空行,告诉服务器结束头部

## 自定义的处理(这里不做处理,只做数据回环)
echo
echo
echo ""
echo ""
echo "hello, this is the output from shell"
echo "<br>Data send by Browser : [$QUERY_STRING]</br>" 
echo ""

测试

指定配置文件启动:

cd /home/xx/lighttpd/
sbin/lighttpd -f config/lighttpd.conf

打开网页,测试

posted @ 2020-07-31 15:17  schips  阅读(2950)  评论(0编辑  收藏  举报