一:安装nginx
1:安装必要的依赖库:
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev
sudo apt-get install zlib1g-dev
2:下载并编译nginx
http://nginx.org/en/download.html下载最新nginx 解压
./configure --prefix=/home/yourpath/
make
sudo make install
3:配置nginx
编译完成后进入objs目录
sudo ./nginx 启动nginx
sudo ./nginx -t 检查配置文件
sudo ./nginx -v 检查nginx版本
sudo ./nginx -V 配置参数
sudo ./nginx -c 参数启动
ps -ef | grep nginx 查看进程号
sudo kill -QUIT 进程号
二:安装spawn_fastcgi, 这个东东可以认为是cgi进程管理器
sudo apt-get install libbz2-dev
wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
tar zxvf lighttpd-1.4.19.tar.gz
cd lighttpd-1.4.19
./configure
make
cp ./src/spawn-fcgi /your/nginxpath/sbin/
三:安装fastcgi, 这个东东可以提供cgi类库
wget http://www.fastcgi.com/dist/fcgi.tar.gz
tar zxvf fcgi.tar.gz
cd fcgi
cd include
vim fcgio.h , 头文件中加入 #include<stdio.h>
./configure
make
make install
sudo cp /usr/local/lib/libfcgi.so.0 /usr/lib
四:编写cgi测试程序
#include <fcgi_stdio.h>
int main( int argc, char *argv[] )
{
while( FCGI_Accept() >= 0 )
{
FCGI_printf( "Status: 200 OK\r\n" );
FCGI_printf( "Content-Type: text/html\r\n\r\n" );
FCGI_printf( "Hello world in C\n" );
}
return 0;
}
g++ -o test.cgi test.cpp -L /usr/local/lib/ -lfcgi
五:配置nginx.conf 和 启动spawn-fcgi
spawn-fcgi -a 127.0.0.1 -p 8088 -f /your/nginxpath/cgi-bin/yourcgi.cgi
vim nginx.conf,在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;
}
六:测试:
sudo kill -HUP [nginx pid]
sudo ./nginx
浏览器中输入 http://localhost/yourcgi.cgi