乐趣区

关于arm:lighttpdfastcgi嵌入式web交叉编译到arm

@[TOC]

前提

环境我的是 Ubuntu20.04. 板子这里我用的是 mx6ull,晚点原子的 alpha 开发板,不过这是个纯软的货色,硬件平台不重要。而后穿插编译链我置信这些你都弄好了。
<font color=red> 我初学这个,本文次要是我的集体记录,一些谬误啥的不免,望各位大佬轻喷 </font>
网上找到的 fastcgi 根本是 php 相干的,发现用 c /c++ 文章还挺少,所以写一篇记录下过程。

lighttpd 穿插编译装置

源码下载

下载源码没啥好说的,间接官网走起。我这里下载的是 lighttpd-1.4.55.tar.gz 这个版本。
除了下载 lighttpd 的源码,还须要 pcre 这个货色(我也初学者,不晓得起因,大略是依赖这个吧)。pcre 同样官网下一个。顺带一提 pcre 和 pcre2 是两个货色。我开始下的时候没留神,折腾了半天。这里我用的 pcre-8.42.tar.bz2 这个版本。

穿插编译

首先编译 pcre 这个货色,先解压:

tar -xjvf pcre-8.42.tar.bz2
cd pcre-8.42

而后配置一下,arm-linux-gnueabihf-gcc 这个是我的穿插编译链,而后最初设置装置目录为以后文件夹下 install 文件夹。

./configure CC=arm-linux-gnueabihf-gcc --host=arm-linux-gnueabihf target=arm-linux --prefix=/ --enable-utf8 --enable-unicode-properties --prefix=$(pwd)/install

配置实现后编译而后装置,因为配置时候制订了装置目录,所以这里间接 make install 就好了。

make
make install

印象中我第一次编译时候两头有报错来着,然而当初重做没报错,可能该装的依赖上次都装好了 (写文章不能拖着呀,不然有些细节都忘了)。
接下来就编译 lighttpd,同样先解压

 tar -zxvf lighttpd-1.4.55.tar.gz
 cd lighttpd-1.4.55/

lighttpd 的配置就有一些长了,这个中央第一次弄的时候卡了良久,CC,RANLIB 这些你用其余 lighttpd 版本时候可能还须要 CXX 啥的,反正缺啥加啥,是在不想看就把这些都配。而后我遇到另个坑是始终找不到 pcre,最初问了 googl,要手动指明库门路。而后上面就是我试过的可用版本。

./configure -prefix=$(pwd)/install \
--host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc \
RANLIB=arm-linux-gnueabihf-ranlib STRIP=arm-linux-gnueabihf-strip\
--enable-shared --without-mysql --without-zlib --without-bzip2 \
--disable-ipv6 \
PCRECONFIG=/home/shi/linux/web/new/pcre-8.42/install/bin/pcre-config \
PCRE_LIB=/home/shi/linux/web/new/pcre-8.42/install/lib/libpcre.a \
CFLAGS="$CFLAGS -DHAVE_PCRE_H=1 -DHAVE_LIBPCRE=1 -I/home/shi/linux/web/new/pcre-8.42/install/include"

而后进行编译和装置

make
make install

一切顺利的话,在当前目录下生成了 install 文件,并且外面有如下文件。

而后在 install 目录下载新建一些文件夹。cache、cgi-bin、config、log、sockets、upload、vhosts、webpages。

mkdir cache cgi-bin config log sockets upload vhosts webpages

接下来把须要的配置文件拷贝过去,门路如下,全副拷贝到刚刚新建的 config 文件夹

doc/config/conf.d
doc/config/lighttpd.conf
doc/config/modules.conf

接下来简略批改一下配置。批改的思路是这样的,lighttpd 的默认配置他的配置文件,日志,网页文件等都是按着 linux 规范形式来的,这里我把这一些文件都放在了一个目录上面,所以要批改一下配置,通知程序这里对应文件别离放在哪。第二点就是在嵌入式上资源受限,缩小一些资源开销。
我的 lighttpd.conf 配置文件批改局部如下,实现的文件有点大,间接丢码云了(间接上传 csdn 又要积分啥的,不敌对),地址见文末

## 我最终移植到板子上的时候把 install 文件夹名字改成了 lighttpd,文件夹放
## 在根目录 "/" 下, 所以上面几个目录对应如下
var.log_root    = "/lighttpd/log"
var.server_root = "/lighttpd"
var.state_dir   = "/lighttpd"
var.home_dir    = "/lighttpd"
var.conf_dir    = "/lighttpd/config"
## 这里 server_root 和 "/vhost",两个字符串加起来正好 "/lighttpd/vhost", 正
## 好对应刚刚新建的 vhosts
var.vhosts_dir  = server_root + "/vhosts"
## 同 vhosts
var.cache_dir   = server_root + "/cache"
## 禁用 ipv6,咱们编译的时候禁用 ipv6 了。能够翻回去看编译配置
server.use-ipv6 = "disable"
## 看翻译这里是用一个不同的身份启动运行服务器,我这里正文了,间接 root 干就好
## server.username  = "lighttpd"
## server.groupname = "lighttpd"
## 同 vhosts,这个文件夹寄存 html 文件
server.document-root = server_root + "/webpages"
## 同 vhosts
server.pid-file = state_dir + "/lighttpd.pid"
## 拜访日志调试时候先开着,最终部署时候看需要开关(据说生成的日志文件比
## 较大,如果部署开启这个思考要定时删除)
include "conf.d/access_log.conf"
## 这里只是一个简略的服务器,用不着 2048,我这里用 256,server.max-fds = 256
## 这个和 max-fds 这个数值有关系,留神下面官网英语正文
server.max-connections = 128
## 上传文件夹门路
server.upload-dirs = ("/lighttpd/upload")

到当初为止,应该能够运行而后拜访动态 html 文件了。
在 webpages 上面轻易新建一个 index.html 文件,内容轻易,名字先不轻易,名字轻易的话就要在地址栏指定名字了。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
    <h1> 我的第一个题目 </h1>
    <p> 我的第一个段落。</p>
</body>
</html>

简略测试

接来下吧 install 整个文件夹复制到开发板的 / 目录上面,而后重命名为 lighttpd。
而后启动 lighttpd 试试看先。

cd /lighttpd/sbin
./lighttpd -f ../config/lighttpd.conf -m ../lib

如果你板子的 80 端口没有被占用,当初服务器应该是起来了
在电脑浏览器输出板子 ip 拜访试试. 显示如下,到此曾经胜利了一半。

fastcgi 编译配置

源码下载

fastcgi 的源码在 github 下面,下载速度可能有点感人,gitee 如同有个什么搬运打算还是啥,反正大略就是把 github 下面一些我的项目搬运过去提速下载,我懒得去看这个在不在打算之列,我把我用的这个版本一并放在 gitee,需要的话自取。地址在文末。
我这里用的是 fcgi2-2.4.2.tar.gz 版本。

穿插编译生成动静库

先解压,

tar -xzvf fcgi2-2.4.2.tar.gz
cd fcgi2-2.4.2

而后配置,这个配置我没找到残缺参考,我猜是这样的

./autogen.sh 
./configure CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ --host=arm-linux-gnueabihf --prefix=$(pwd)/install

而后编译装置

make
make install

执行实现后再 install 文件夹会生成三个文件夹,别离是 bin,include,lib,暂且放一边,等会再用他们。

批改 lighttpd 配置

接下来批改一下 lighttpd 的配置,开启 lighttpd 对 factcgi 的反对。
首先在 modules.conf 找到如下行, 并把 include 后面的 #删除。

##
## FastCGI (mod_fastcgi)
##
include "conf.d/fastcgi.conf"

下面这行配置意思很简略,就是蕴含 conf.d 文件夹下的 fastcgi.conf 配置文件,所以接下来批改 fastcgi.conf 这个文件
在这个文件中增加如下行:

fastcgi.debug = 1
fastcgi.server = (
    "/test" =>(
        "test.fastcgi.handler" =>(
            "socket" => socket_dir + "/test/test.fastcgi.socket",
            "check-local" => "disable",
            "bin-path" => home_dir + "/cgi-bin/test.fcgi",
            "max-procs" => 3,
        )
    )
)

把 fastcgi 编译生成的 install 文件夹拷贝到板子里,而后批改你的.bashrc 文件 PATH 门路退出 install/bin,LD_LIBRARY_PATH 门路退出 install/lib, 或者间接把 install/bin 目录下文件复制到板子 /bin, 把 install/lib 目录下文件全副复制到板子 /etc。

简略测试

c 语言 fcgi 程序

这块没啥好说的 间接上代码

#include <fcgi_stdio.h>
int main (void) {while (FCGI_Accept() >= 0) {printf("Status: 200 OK\r\n\r\nHello World!\n");
    }
    return 0;
}

编译的时候就须要用到刚刚生成的那个 install 文件夹下货色了。留神这个不能间接复制,{your path}要换成你本人的门路

arm-linux-gnueabihf-gcc -o test.fcgi test.c -L{your path}/install/lib \
-lfcgi -I{your path}/install/include

把刚刚生成的 test.fcgi 复制到板子 /lighttpd/cgi-bin 目录下。而后重启服务器,在浏览器输出 ip/test, 能够看到返回了 hello world。

c++ fcgi 程序

老规矩,先上代码

#include <string>
#include <iostream>
#include <string.h>
#include <fcgi_stdio.h>
#include <fcgiapp.h>


using namespace std;

int main(int argc, char **argv)
{
    FCGX_Stream *in, *out, *err;
    FCGX_ParamArray evnp;
    while(FCGX_Accept(&in, &out, &err, &evnp) >= 0)
    {
        string query;
        do
        {char *pRequestMoethod = FCGX_GetParam("REQUEST_METHOD", evnp);
            if(pRequestMoethod == NULL)
            {
                cout << "ERROR: getevn REQUEST_METHOD is failed." << endl;
                break;
            }
            // 判断申请办法
            if(strcmp(pRequestMoethod, "POST") == 0)
            {
                    // 获取申请中 URL 携带的参数,POST 也会在 URL 中携带参数
                    query = FCGX_GetParam("QUERY_STRING", evnp);
                    // 获取 POST 申请携带的参数长度
                    char *pLenstr = FCGX_GetParam("CONTENT_LENGTH", evnp);
                if(pLenstr != NULL)
                {long len = atoi(pLenstr);
                    string postData;
                    // 获取 POST 携带的数据
                    for(int i = 0; i < (int)len; i++)
                    {int ch = FCGX_GetChar(in);
                        if(ch < 0)
                        {break;}
                        postData += (char)ch;
                    }
                    // 解决申请, 具体逻辑疏忽
                    // processRequest(query, postData); 
                }
                else
                {cout << "getenv failed." << endl;}
            }
            else if(strcmp(pRequestMoethod, "GET") == 0)
            {
                // 获取申请中 URL 携带的参数
                query = FCGX_GetParam("QUERY_STRING", evnp);
                // 解决 GET 申请, 代码暗藏
                // processRequest(query); 
                break;
            }
            else
            {cout << "This is unknown quest." << endl;}
        }
        while(false);
        string reply = "hello world";
        // reply = getReply(); // 获取返回数据
        // 向客户端返回用到数据
        FCGX_FPrintF(out, "Content-type: text/html;\r\n"
                    "Content-Length: %d\r\n"
                    "\r\n"
                    "%s",reply.length(), reply.c_str());
    }
    return 0;
}

编译相似,

arm-linux-gnueabihf-g++ -o test.fcgi test.cpp -L{your path}/install/lib \
-lfcgi -I{your path}/install/include

gitee 仓库链接

https://gitee.com/sutichunxiao/lighttpd

参考

[1] https://blog.flowlore.com/passages/cc-fastcgi-http/
[2] https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModFastCGI
[3] https://www.huaweicloud.com/articles/2640eb7232b1794193263975ee0c65bc.html

退出移动版