Lighttpd+fastcgi+C语言

                         

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

交叉编译 lighttpd 和 php_fpm 需要进行以下步骤: 1. 安装交叉编译工具链:根据目标平台选择合适的交叉编译工具链,例如 arm-linux-gnueabi-gcc 或者 aarch64-linux-gnu-gcc 等,并将其添加到 PATH 环境变量中。 2. 下载 lighttpd 和 php 源码:从官网下载 lighttpd 和 php 的源码包,例如 lighttpd-1.4.54.tar.gz 和 php-7.4.16.tar.gz。 3. 解压源码包:使用 tar 命令解压源码包,例如: ``` tar zxvf lighttpd-1.4.54.tar.gz tar zxvf php-7.4.16.tar.gz ``` 4. 进入 lighttpd 源码目录:进入解压后的 lighttpd 源码目录,例如: ``` cd lighttpd-1.4.54 ``` 5. 配置编译选项:使用交叉编译工具链配置编译选项,例如: ``` ./configure --host=arm-linux-gnueabi ``` 6. 编译 lighttpd:使用 make 命令编译 lighttpd,例如: ``` make ``` 7. 进入 php 源码目录:返回上级目录,进入解压后的 php 源码目录,例如: ``` cd ../php-7.4.16 ``` 8. 配置编译选项:使用交叉编译工具链配置编译选项,例如: ``` ./configure --host=arm-linux-gnueabi --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-short-tags --disable-phpdbg ``` 其中,--with-pdo-mysql 和 --with-mysqli 用于支持 MySQL 数据库,--with-openssl 用于支持 SSL/TLS 加密,--enable-fpm 和 --with-fpm-user、--with-fpm-group 用于编译 php_fpm。 9. 编译 php_fpm:使用 make 命令编译 php_fpm,例如: ``` make ``` 10. 将编译完成的 lighttpd 和 php_fpm 拷贝到目标平台上。 以上就是交叉编译 lighttpd 和 php_fpm 的步骤,需要注意的是,编译选项需要根据实际情况进行调整,例如根据目标平台选择合适的交叉编译工具链、选择需要支持的模块等。
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值