lighttpd+fastcgi+C语言
一、简述
记--lighttpd+fastcgi。fastcgi相对于传统cgi,可以实现常驻进程的效果。传统cgi是收到请求之后才会启动cgi程序,处理完成后就退出,这对响应速度、资源不断的申请和释放,状态的保持有影响。
例子及相关资料下载:下载:https://wwa.lanzoui.com/b0c9czcbc 密码:1vyi
二、安装lighttpd
2.1 下载lighttpd
官网:https://download.lighttpd.net/lighttpd
2.2 解压
tar xzf lighttpd-1.4.59.tar.gz
2.3 创建lighttpd目录,后面用作lighttpd安装路径
2.4 安装
配置:
执行./configure -C --prefix=/home/liang/lighttpd/ --without-pcre --without-zlib
--without-pcre --without-zlib 附带这两个参数是因为例子中的ubuntu没有安装pcre和zlib,如果已经安装,或需要请自行安装,然后就不需要这两个参数了
执行make
执行make install
成功安装到指定的目录:
三、配置lighttpd
3.1 拷贝一份配置
3.2 创建相关目录,后面修改配置会使用到
mkdir cache log sockets vhosts www
log目录存放lighttpd相关日志
sockets目录存放的是与fcgi通信的本地域socket
www用作存放html相关,cgi程序相关
3.3 修改配置
修改lighttpd.conf
修改modules.conf
修改fastcgi.conf
fastcgi.server = (
"test.fcgi" =>
((
"bin-path" => "/home/liang/lighttpd/www/test.fcgi",
"socket" => socket_dir + "/test.socket",
"min-procs" => 0,
"max-procs" => 1,
"bin-enviroment" => (
"REAL_SCRIPT_NAME" => ""
),
"check-local" => "disable"
))
)
修改cgi.conf使得可以使用cgi
3.4 启动lighttpd
执行sudo /home/liang/lighttpd/sbin/lighttpd -f /home/liang/lighttpd/config/lighttpd.conf
使用-f参数指定配置文件:/home/liang/lighttpd/config/lighttpd.conf
还没有编写test.fcgi, 暂时屏蔽fcgi,后面写好test.fcgi,再打开
成功运行
在 /home/liang/lighttpd/www/下放一个index.html以便启动lighttpd测试, 使用浏览器访问127.0.0.1/index.html即可看到如下页面
四、安装fastcgi
4.1 下载fcgi-2.4.1.tar.gz
链接1:resources/fastCGI安装包 at master · Seaworth/resources · GitHub
链接2:下载:https://wwa.lanzoui.com/b0c9czcbc 密码:1vyi
4.2 解压:tar xzf fcgi-2.4.1.tar.gz
4.3 添加头文件引用
修改fcgio.cpp,添加引用stdio.h头文件,不然后面make会报找不到EOF错误
4.4 编译
./configure(默认安装位置/usr/local/bin, /usr/local/lib/)
执行make
sudo make install
安装后可执行文件在/usr/local/bin, fcgi库文件在/usr/local/lib/ ,头文件在/usr/local/include/
编写fcgi程序需要使用到fcgi_stdio.h和libfcgi.so
五、测试fastcgi程序
简单的测试程序,如果需要更符合的使用场景,可以进行个性化配置。
5.1 测试代码
#include <stdio.h>
#include <stdlib.h>
#include "fcgi_stdio.h"
int main(void)
{
int count = 0;
while(FCGI_Accept() >= 0) {
printf("Content-type: text/html\r\n\r\n");
printf("Hello Fastcgi. count=%d", ++count);
}
return 0;
}
5.2 编译
gcc fcgi.c -o test.fcgi -lfcgi
5.3 测试结果
注意:需要在配置中启用fcgi,并重新启动lighttpd服务器,如果还是不能访问fcgi程序,可以尝试重启ubuntu。
六、 测试传统cgi程序
测试代码:
#include <stdio.h>
int main(void)
{
//printf("Content-type: text/html\r\n");
printf("hello cgi. %s\r\n", __DATE__);
return 0;
}
编译:gcc cgi.c -o test.cgi
如果test.cgi没有执行权限就需要给权限,chmod a+x test.cgi
测试结果:
此时查看所有进程,是没有test.cgi进程的,因为它已经退出了,只有在被调用的瞬间启动,处理完成就退出,ps ef | grep test.fcgi
附:
1、结束lighttpd:
pkill lighttpd
killall lighttpd
2、关于503 Service Unavailable
通常是服务器不可访问。请尝试:
2.1 排查网络连通性,可能windows防火墙,ubuntu防火墙等;
2.2 请检查lighttpd服务器是否正常启动,启动的时候是否有报错,有报错信息请查看报错信息。
2.3 请检查FCGI程序是否正常启动: 通常可以使用该指令检查,ps -ef | grep xxx.fcgi
2.4 如果fcgi程序不能运行,请检查FCGI程序的执行权限,ls -l xxx.fcgi, 如果没有权限请添加,chmod a+x xxx.fcgi。
2.5 如果fcgi程序不能运行,请检查FCGI程序的动态库(fastcgi.so、fastcgi.so.0 fastcgi.so.0.0.0.0)是否放在动态库环境变量中。在终端执行export, 然后查看LD_LIBRARY_PATH这一行有没有FCGI程序的动态库(fastcgi.so、fastcgi.so.0 fastcgi.so.0.0.0.0)所在的路径,如果没有请添加, 执行:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:FCGI程序的动态库所在路径。
或者直接将FCGI程序的动态库拷贝到已经添加到LD_LIBRARY_PATH环境变量的路径之一。
2.6 lighttpd服务器启动报错是有日志的,在lighttpd.conf配置的日志路径,日志文件通常是error.log
2.7 lighttpd服务器启动报错: failed to start xxx.fcgi, 这个问题请检查fcgi程序的动态库,见附2.5
(log.c.166) server started
(mod_fastcgi.c.1104) failed to start: xxx.fcgi
(mod_fastcgi.c.1108) child exited with status 255
(mod_fastcgi.c.1111) If you're trying to run your app as a FastCGI backend, make sure you're using the FastCGI-enabled version.
If this is PHP on Gentoo, add 'fastcgi' to the USE flags.
(mod_fastcgi.c.1399) [ERROR]: spawning fcgi failed.
(server.c.938) Configuration of plugins failed. Going down