环境 Ubuntu 16.04
下载lighttpd,这个网上资源很多。
编译
/etc/lighttpd/lighttpd.conf文件内添加
fastcgi.server = (
"/login" => (
"test.fastcgi.handler" => (
"socket" => "/tmp/test.fastcgi.socket",
"check-local" => "disable",
"bin-path" => "/usr/bin/test.fastcgi",
"max-procs" => 30,
)
)
)
C 代码
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <alloca.h>
#include <fcgiapp.h>
#define LISTENSOCK_FILENO 0
#define LISTENSOCK_FLAGS 0
int main(int argc, char **argv)
{
openlog("testfastcgi", LOG_CONS | LOG_NDELAY, LOG_USER);
int err = FCGX_Init(); /* call before Accept in multithreaded apps */
if (err)
{
syslog(LOG_INFO, "FCGX_Init failed: %d", err);
return 1;
}
FCGX_Request cgi;
err = FCGX_InitRequest(&cgi, LISTENSOCK_FILENO, LISTENSOCK_FLAGS);
if (err)
{
syslog(LOG_INFO, "FCGX_InitRequest failed: %d", err);
return 2;
}
while (1)
{
err = FCGX_Accept_r(&cgi);
if (err)
{
syslog(LOG_INFO, "FCGX_Accept_r stopped: %d", err);
break;
}
char **envp;
int size = 200;
char *result = (char *)alloca(size);
strcpy(result, "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n");
strcat(result, "<html><h1>TestCGI!</h1></html>\r\n");
FCGX_PutStr(result, strlen(result), cgi.out);
}
return 0;
}
编译C代码,重启lighttpd
gcc test.c -lfcgi -o test.fastcgi
sudo cp test.fastcgi /usr/bin
/etc/init.d/lighttpd restart
打开firefox,输入localhost/login。
结束。
最后效果如下图。