web(五)---spawn-fastcgi

摘要:
""Requestnumber%drunningonhost%s""ProcessID:%d",++count,getenv,getpid());}return0;}2.编译.g++demo.cc-odemo-lfcgi注意:1.如果编译不通过,可能是库-lfcgi没找到,那就是有问题2.直接运行可执行文件,看看能否正常运行。

一.spawn_fastcgi的安装、部署与配置.

1. 下载spawn_fastcgi.
https://github.com/lighttpd/spawn-fcgi
这里使用的是1.6.3的版本https://github.com/lighttpd/spawn-fcgi/releases/tag/v1.6.3
2. 解压并安装(请记得看README).
(1)如果没有configure,请先执行./autogen.sh,生成configure
(2) ./configure
(3) make
注意: 如果通过上面安装不成功, 试试直接 sudo apt-get installspawn_fastcgi
3.编译好以后,将可执行文件移动到nginx的sbin目录下.
cp ./src/spawn-fcgi /usr/local/nginx/sbin/(cp到nginx的安装目录下)
注意: 1. 第3步, 如果拷贝过程中提示找不到./src/spawn-fcgi, 则用whichspawn-fcgi找到该二进制拷贝;
2.第3步,如果拷贝过程中提示找不到/usr/local/nginx/sbin/, 则手动创建目录.
二.fastcgi库的安装.
1.下载http://www.fastcgi.com/dist/fcgi.tar.gz(从这下载的可能有问题, 最好用本文给出的.)
2. $./configure
$make
$make install
注意: 1. 安装过程中如果出现如下错误, 则在fcgio.cpp中添加头文件 #include <stdio.h>, 重新编译即可.
参考http://stackoverflow.com/questions/8833718/installing-fastcgi-dev-kit
2. 如果make通过, 但是make install有错, 尝试用本文给的文件, 不用步骤1链接中的.

fcgio.cpp: In destructor 'virtual fcgi_streambuf::~fcgi_streambuf()':
fcgio.cpp:50:14: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::overflow(int)':
fcgio.cpp:70:72: error: 'EOF' was not declared in this scope
fcgio.cpp:75:14: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::sync()':
fcgio.cpp:86:18: error: 'EOF' was not declared in this scope
fcgio.cpp:87:41: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::underflow()':
fcgio.cpp:113:35: error: 'EOF' was not declared in this scope
make[2]: *** [fcgio.lo] Error 1
make[2]: Leaving directory `/home/lucasysfeng/workspace/web/download/nginx_spawn_fcgi_install_package/fcgi-2.4.1-SNAP-0311112127/libfcgi'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/lucasysfeng/workspace/web/download/nginx_spawn_fcgi_install_package/fcgi-2.4.1-SNAP-0311112127'
make: *** [all] Error 2

三.Demo程序.
1. demo.cc如下所示:
#include <fcgi_stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    int count = 0;
    while (FCGI_Accept() >= 0) {
        printf("Content-type: text/html
"
                "
"
                ""
                "FastCGI Hello!"
                "Request number %d running on host%s "
                "Process ID: %d
", ++count, getenv("SERVER_NAME"), getpid());
    }
    return 0;
}

2. 编译.

g++ demo.cc-o demo -lfcgi​

注意:
1. 如果编译不通过, 可能是库-lfcgi​没找到, 那就是<二.fastcgi库的安装.>有问题
2. 直接运行可执行文件,看看能否正常运行。
如果出现缺少库libfcgi.so.0(如:error while loading shared libraries: libfcgi.so.0: cannot open shared object file: No such file or directory)
(1) 则自己需要手动把/usr/local/lib/libfcgi.so.0库建立一个链接到/usr/lib/目录下:
ln -s /usr/local/libfcgi.so.0 /usr/lib/(或者把so的库路径添加到/etc/ld.so.conf,并执行ldconfig更新一下
(2) 或者找到libfcgi.so.0(find命令), 将它拷贝到/usr/lib(如:cp/usr/local/lib/libfcgi.so.0/usr/lib)

(3) 或者设置下面环境变量试试.

export LD_LIBRARY_PATH=/usr/local/lib

sudo ldconfig

四. demo发布.
1)将CGI可执行程序移动到nginx的安装目录下 /usr/local/nginx/cgibin (文件夹不存在则自己创建)
cp ***/demo /usr/local/nginx/cgibin
2)启动spawn-fcgi管理进程,并绑定serverIP和端口(不要跟nginx的监听端口重合
/usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088-f /usr/local/nginx/cgibin/demo
查看一下8088端口是否已成功:netstat -na | grep 8088
3)更改nginx.conf配置文件(或者是/etc/nginx/sites-available/default),让nginx转发请求
在http节点的子节点-"server节"点中下添加配置
location ~ .cgi$ {
fastcgi_pass 127.0.0.1:8088;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME fcgi$fastcgi_script_name;

include fastcgi_params;
}
4)重启nginx或者重新加载配置文件
sudo service nginx restart
5)打开浏览器访问一下吧
http://localhost/demo.cgi

免责声明:文章转载自《web(五)---spawn-fastcgi》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇使用PHP文件锁写一个多个请求同时并发写入一个文件,要求不脏读、数据不丢失dart系列之:dart中的异步编程下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

解决ThinkPhp在nginx下404问题

 以宝塔面板为例:  找到宝塔面板域名的nginx配置文件进行编辑增加以下内容 fastcgi_split_path_info ^(.+.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastc...

SUSE12Sp3-Nginx安装

1、安装pcre(nginx 依赖) 把安装包pcre-8.12.tar.gz复制到服务器指定目录 tar -zxvf pcre-8.12.tar.gz # 解压 cd pcre-8.12 #进入目录 ./configure && sudo make && sudo make install #配置编译安装 2、安装z...

各种WAF收集集锦

开源WAF集锦收集 https://blog.51cto.com/14678079/2474927 1.免费版本sharewaf http://www.sharewaf.com/ 2.hihttps hihttps是一款少有完整源码的高性能WEB应用 + MQTT物联网防火墙,兼容ModSecurity规则并开源。特点是使用超级简单,就一个约10M的可执行...

Nginx设置404页面转发

Nginx设置404有很多用户都会遇到,在真正的使用中会有相当的问题解决方法。下面我们就来学些有关于Nginx设置404的解决方法。今天帮客户的Nginx设置404页面转发,按平常的写法 error_page 404 http://www.xx.com/?page-error.html;  这样写发现不能正常转跳,看来Nginx不能自动转义,加上转义后还是...

Nginx部署文件(一)-nginx.conf文件

一,默认文件说明1.以nginx/1.14.0 (Ubuntu)为例 1 # 使用的用户和组 2 user www-data; 3 # 定义nginx对外提供web服务时的worker进程数 4 worker_processes auto; 5 # 指定pid存放的路径,应该记录了nginx守护进程的进程号,是其他进程的父进程id!(如Apach...

NGINX----源码阅读一(main函数)

1、ngx_debug_init(); 初始化debug函数,一般为空。 2、ngx_strerror_init(); 将系统错误码+错误信息,以ngx_str_t数组保存。 3、ngx_get_options(int argc, char *const *argv) nginx启动函数选项, 4、ngx_show_version_info(); 如果上一...