lighttpd+fastcgi+C语言程序

1、安装环境Ubuntu16.04
2、安装lighttpd
apt install lighttpd
3、安装fastcgi
下载源码:fastcgi-2.4.1-SNAP-0910052249.tar.bz2
解压:tar -xjvf fastcgi-2.4.1-SNAP-0910052249.tar.bz2
进入源码:cd fastcgi-2.4.1-SNAP-0910052249
修改fcgi-2.4.1-SNAP-0910052249/libfcgi/fcgiapp.c,在里面加一句#include 。否则编译报错。
配置:./config
编译:make
安装:make install
将编译出的动态库加入环境变量:vim /etc/ld.so.conf
在下面加入一行:/usr/local/lib
使环境变量生效:ldconfig
4、编写C语言程序
vim test.c

#include 
#include 
#include 
#include 
#include 
#define LISTENSOCK_FILENO 0
#define LISTENSOCK_FLAGS 0
int main(int argc, char **argv)
{
  int err = FCGX_Init(); /* call before Accept in multithreaded apps */
  if (err)
  {
    return 1;
  }
  FCGX_Request cgi;
  err = FCGX_InitRequest(&cgi, LISTENSOCK_FILENO, LISTENSOCK_FLAGS);
  if (err)
  {
    return 2;
  }
 
  while (1)
  {
    err = FCGX_Accept_r(&cgi);
    if (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, "

TestCGI!

\r\n"); FCGX_PutStr(result, strlen(result), cgi.out); } return 0; }

编写makefile:vim Makefile

all:
	gcc test.c -o test.fastcgi -lfcgi

编译C语言程序:make
5、编写index.html



  first cgi page



  

hahaha...

6、修改配置
创建/var/www/html目录,将上一步编写的index.html拷贝到该目录下。
创建/var/www/html/cgi-bin目录,将之前编写的test.fastcgi程序拷贝到该目录。
lighttpd+fastcgi+C语言程序_第1张图片
修改lighttpd配置:vim /etc/lighttpd/lighttpd.conf

server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
        "mod_rewrite",
        "mod_fastcgi"	#新增fastcgi模块
)

server.document-root        = "/var/www/html" #网页主目录
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
#server.username             = "www-data"
#server.groupname            = "www-data"
server.port                 = 80


index-file.names            = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

vim /etc/lighttpd/conf-available/10-fastcgi.conf

# /usr/share/doc/lighttpd/fastcgi.txt.gz
# http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi
server.modules += ( "mod_fastcgi" )
fastcgi.server = (
        "/test" => ((
                "socket" => "/var/www/html/test.socket",
                "bin-path" => "/var/www/html/cgi-bin/test.fastcgi",
                "check-local" => "disable"
        ))
)

7、启动lighttpd服务
service lighttpd start
可以通过lsof -i:80查看服务是否启动成功
在这里插入图片描述
lighttpd使能fastcgi模块lighty-enable-mod
lighttpd+fastcgi+C语言程序_第2张图片
重新加载配置文件/etc/init.d/lighttpd force-reload
8、测试
浏览器登录127.0.0.1
lighttpd+fastcgi+C语言程序_第3张图片

浏览器登录127.0.0.1/test
lighttpd+fastcgi+C语言程序_第4张图片

你可能感兴趣的:(lighttpd,c语言,linux,lighttpd,fastcgi,网页服务器)