Centos 安装fastcgi详解与用例
  lrSNmASQAHAD 2023年11月02日 92 0


1、fastcgi简介


fastcgi解决了cgi程序处理请求每次都要初始化和结束造成的性能问题。fastcgi并且是独立于webserver的,fastcgi的crash并不影响webserver,然后他们之间通过soket通信。与fastcgi不同的另一种解决cgi程序反复创建,销毁的方法是让webserver开放api,然后编写cgi的时候,把cgi嵌入到webserver中,这样有个不好的地方就是cgi的crash会影响到webserver。

支持fastcgi的服务器有很多比如,nginx IIS什么的。他们都是把http请求转换为stdin和一些环境变量传递给fastcgi程序,然后返回stdout。编写fastcgi程序最终要的一个api就是int FCGI_Accept(void);当一个请求被送达的时候返回。

2、fastcgi安装步骤:


step1:
下载地址:​​​http://www.fastcgi.com/drupal/node/5​​​ 点击Current: download | docs | browse 中的download链接即可。
step2:
tar -zxvf fcgi.tar.gz

cd fcgi-2.4.1-SNAP-0311112127/

./configure –prefix=/etc/fcgi

make && make install

出现fcgio.cpp:50: error: ‘EOF’ was not declared in this scope的话只需要 在/include/fcgio.h文件中加上 #include

到此就安装完了。

3、编写并测试fcgi程序


1)Demo测试程序fcgi_test2.c如下:

#include "fcgi_stdio.h"
#include <stdlib.h>

int main(void)
{
int count = 0;
while(FCGI_Accept() >= 0)
{
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello!</title>"
"<h1>FastCGI Hello!</h1>"
"Request number %d running on host <i>%s</i>\n",
++count, getenv("SERVER_NAME"));
}
return 0;
}

2)编译
[root@cb24f769838b cgi-bin]# gcc -g fcgi_test2.c -o fcgi_test2.fcgi -lfcgi

//此时直接运行 fcgi_test2.fcgi 会报如下错:
error while loading shared libraries: libfcgi.so.0:

3)链接库错误解决方案:
参考:​​​https://www.apachelounge.com/viewtopic.php?p=8160​​​
方案核心——“Link echo with the static library, libfcgi.a, instead of the shared library.”链接到静态库,而非共享库。
[root@cb24f769838b cgi-bin]# gcc fcgi_test2.c -o fcgi_test2.fcgi -I/etc/fcgi/include /usr/local/lib/libfcgi.a

4)查看关联链接
[root@cb24f769838b cgi-bin]# ldd fcgi_test2.fcgi
linux-vdso.so.1 => (0x00007fff7fdc3000)
libc.so.6 => /lib64/libc.so.6 (0x00007fa0229ed000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa022d86000)

4、正确运行结果如下


1)程序运行结果如下
[root@cb24f769838b cgi-bin]# ./fcgi_test2.fcgi

Content-type: text/html

<title>FastCGI Hello!</title><h1>FastCGI Hello!</h1>Request number 1 running on host <i>(null)</i>

2)浏览器运行结果如下:

Centos 安装fastcgi详解与用例_linux

5、常见出错:

FastCGI: can’t create server “/var/www/fcgi/groundService.fcgi”: bind() failed [/etc/httpd/runtimedir/fastcgi/d2f3a2fea2f2f1b44954766d53644c3c]
解决方案:
修改配置:
586 #FastCgiIpcDir runtimedir/fastcgi
587 FastCgiIpcDir /tmp/fastcgi
第586行改为587行。

6、fastcgi部署参考

​https://www.sympa.org/faq/fastcgi​

作者:铭毅天下

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  HE3leaVn7jMN   2023年11月24日   31   0   0 Timei++#include
  HE3leaVn7jMN   2023年11月26日   30   0   0 i++#include
lrSNmASQAHAD