关于在Ubuntu 16.04上安装Nginx,MySQL,PHP (LEMP),已经讲过:
http://www.linuxdiyf.com/linux/20461.html
安装spawn-fcgi
sudo apt-get install spawn-fcgi
安装完成之后,spawn-fcgi的目录为 /usr/bin/spawn-fcgi
编写简单C程序
创建文件testcgi。c,输入以下内容进行测试。
#include <iostream>
#include <fcgi_stdio.h>
#include <stdlib.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"));
}
return 0;
}
编译程序
g++ testcgi.c -o upload -lfcgi
将编译成功的upload复制到工作目录(随意)
mkdir
/usr/share/nginx/cgi-bin
cp
upload
/usr/share/nginx/cgi-bin
启动spawn-fcgi
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9002 -C 25 -f /usr/share/nginx/cgi-bin/upload
成功启动提示如下:
更改Nginx配置
sudo vim /etc/nginx/sites-available/default
添加以下内容:
location ~ \.cgi$ {
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME fcgi$fastcgi_script_name;
include fastcgi_params;
}
重启Nginx
sudo service nginx restart
测试
在浏览器输入以下网址:
http://10.102.7.193/upload.cgi
显示以下内容,则为成功: