@[TOC]

The premise

The environment is Ubuntu20.04. Here I’m using MX6ull, the Alpha development board for Point Atom, but it’s a pure soft thing, the hardware platform is not important. And then cross compile the chain and I’m sure you’ve got that all right. < font color = red > I beginner this, this article mainly is my personal record, what some mistakes is inevitable, hope everybody bosses light spray < / font > find basic PHP fastcgi related online, found in c/c + + article is quite small, so to write a record the process.

Lighttpd cross compilation installation

Download the source code

Download the source code has nothing to say, direct official website start. I downloaded lighttpd-1.4.55.tar.gz here. In addition to download lighttpd source code, also need PCRE this thing (I also beginners, do not know the reason, probably is to rely on this). PCRE also website the next one. By the way, PCRE and PCRE2 are two things. I did not pay attention to the beginning of the next, toss about for a long time. Here I use the version pCR-8.42.tar. bz2.

cross-compilation

First compile PCRE, first unzip it:

Tar-xjvf pCR-8.42. tar.bz2 CD pCR-8.42

And then I’m going to configure this, arm-linux-gnueabihf-gcc which is my cross-compile chain, and then finally I’m going to set the install directory to the install folder under the current folder.

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

You can make install the directory you want to use when you configure it.

make
make install

I remember the first time I compiled it there was an error in the middle, but now I redo it without any error, probably the last time I installed the dependencies are installed (you can’t delay writing the article, otherwise some details will be forgotten). Next compile lighttpd and unzip it again

Tar-zxvf lighttpd-1.4.55.tar.gz CD lighttpd-1.4.55/

The configuration of lighttpd is a little bit longer. It took a long time for this site to be used for the first time. You may need CXX or something like CC or RANLIB when using other lighttpd versions, but you need to add everything to it. Then I ran into another pit where I couldn’t find the PCRE and finally asked GOOGL to manually specify the library path. And here’s the version I tried that worked.

./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 - 1 - DHAVE_LIBPCRE DHAVE_PCRE_H = = 1 - I/home/shi/Linux/web/new/pcre - 8.42 / install/include"

Then compile and install

make
make install

If all goes well, the install file is generated in the current directory and contains the following files.



Then download and create some new folders in the install directory. Cache, cgi-bin, config, log, sockets, upload, vhosts, webpages.

mkdir cache cgi-bin config log sockets upload vhosts webpages

Next, copy the required configuration files to the following path, and copy them all to the newly created config folder

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

Let’s make a simple configuration change. The default configuration for lighttpd is that its configuration files, logs, web files, etc., are stored in the standard Linux way. Here I put these files in a directory, so I need to change the configuration and tell the program where to put the corresponding files. The second point is in the embedded resources are limited, reduce some resource overhead. The modified part of my lighttpd.conf configuration file is as follows. The completed file is a little too large, and the code cloud is lost directly (it is not friendly to upload CSDN directly). See the address at the end of the article

When I finally migrated to the board, I changed the name of the install folder to lighttpd, Var. Log_root = "/lighttpd/log" var. Server_root = "/lighttpd" var. State_dir = "/lighttpd" var. Var. Home_dir = "/lighttpd" var. Conf_dir = "/lighttpd/config" ## Vhosts var.vhosts_dir = server_root + "/vhosts" ## and vhosts var.cache_dir = "/lighttpd/vhost Server_root + "/cache" ## Disables IPv6. We have IPv6 disabled at compile time. Server.use-ipv6 = "disable" : disable disable disable disable disable disable disable disable disable disable disable disable disable disable disable disable disable disable disable disable disable disable disable ## Server. username = "lighttpd" ## Server. groupName = "lighttpd" ## # Vhosts, This folder holds the HTML file server.document-root = server_root + "/webpages" ## # Vhosts server.pid-file = state_dir + "/lighttpd.pid" ## include "conf. D /access_log.conf" ## This is a simple server that doesn't need 2048. * * * * * * * * * * * * * * * * * * * * * * * * * Server.max-connections = 128 # Upload -dirs = ("/lighttpd/upload")

By now, you should be able to run and then access static HTML files. Create a new index.html file under your WebPages. The content of the file is arbitrary, but the name is not arbitrary. If the name is arbitrary, you must specify the name in the address bar.

<! DOCTYPE HTML > < HTML > <head> <meta charset="utf-8"> <title>test</ head> <body> <h1> </h1> <p> My first paragraph </p> </body> </html>

A simple test

Next, copy the entire install folder to the/directory on the development board and rename it to lighttpd. Then start lighttpd and give it a try.

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

If port 80 on your board is not occupied, the server should now be up

Enter board IP access in a computer browser to try. As shown below, we’re halfway there.

FastCGI compilation configuration

Download the source code

The source code of FastCGI is on GitHub, the download speed may be a bit impressive, Gitee seems to have a move plan or something, anyway, it is probably to move some projects on GitHub to download faster, I don’t want to see if this is in the plan, I put the version I use along with Gitee, if you need to pick up. The address is at the bottom of the article. I’m using fcgi2-2.4.2.tar.gz here.

Cross compilation generates dynamic libraries

First, stress.

Tar-xzvf fcgi2-2.4.2.tar.gz CD fcgi2-2.4.2

And then the configuration, which I can’t find a complete reference to, I guess is something like this

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

And then compile and install it

make
make install

After the completion of the install folder, three folders will be generated, namely bin,include, and lib. Put them aside for the time being and use them later.

Modify the lighttpd configuration

Next, modify the lighttpd configuration to enable support for factcgi in lighttpd. First find the following line in modules.conf and delete the # before include.

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

Configure the fastcgi.conf file in your conf.d folder, so modify the fastcgi.conf file to add the following line to the file:

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,
        )
    )
)

Copy the install folder from the FastCGI compilation to the board. Then modify your.bashrc PATH to install/bin,LD_LIBRARY_PATH to install/lib, or copy the files from install/bin to /bin and all the files from install/lib to the board The child/etc.

A simple test

C language FCGI program

There’s nothing to say about this piece of code directly

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

You’ll need to use the stuff in the install folder you just created when compiling. Note that this cannot be copied directly, {your path} must be replaced by your own path

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

Copy the generated test.fcgi to the directory /lighttpd/cgi-bin on the board. Then restart the server, type IP /test in the browser, and you’ll see Hello World returned.

C + + fcgi process

As always, code first

#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) {// Get the parameter from the URL in the request, POST also takes the parameter query = fcgx_getParam ("QUERY_STRING", evnp); Char *pLenstr = fcgx_getParam ("CONTENT_LENGTH", evnp); if(pLenstr ! = NULL) { long len = atoi(pLenstr); string postData; 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) {// getParam ("QUERY_STRING", evnp); // 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; }

Compiling is similar,

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

Gitee warehouse link

https://gitee.com/sutichunxiao/lighttpd

reference

[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