一. 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 install spawn_fastcgi
3. 编译好以后,将可执行文件移动到nginx的sbin目录下.
cp ./src/spawn-fcgi /usr/local/nginx/sbin/ (cp到nginx的安装目录下)
注意: 1. 第3步, 如果拷贝过程中提示找不到./src/spawn-fcgi, 则用which spawn-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\r\n"
"\r\n"
""
"FastCGI Hello!"
"Request number %d running on host%s "
"Process ID: %d\n", ++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管理进程,并绑定server IP和端口(不要跟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