FastCGI程序的编写和部署(C++语言)

首先,确认已经安装配置好fcgi+spawn-fcgi+nginx 环境了。

一、编写C++程序并编译

1.编程:$vim test.cpp

#include <stdlib.h>
#include "fcgi_stdio.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>"
        "<div>Request number %d running on host : %s </div>\n"
        "<div>QUERY_STRING : %s\n</div>"    
        "<div>REMOTE_ADDR : %s\n</div>"
        "<div>REMOTE_PORT : %s\n</div>"
        "<div>REQUEST_METHOD : %s\n</div>"
        "<div>CONTENT_TYPE : %s\n</div>"
        "<div>CONTENT_LENGTH : %s\n</div>"
        "<div>SERVER_PROTOCOL : %s\n</div>"
        "<div>REQUEST_URI : %s\n</div>"
        "<div>SERVER_SOFTWARE : %s\n</div>",
        ++count, getenv("SERVER_NAME"),getenv("QUERY_STRING"),
        getenv("REMOTE_ADDR"), getenv("REMOTE_PORT"), getenv("REQUEST_METHOD"),
        getenv("CONTENT_TYPE"),getenv("CONTENT_LENGTH"),getenv("REQUEST_URI"),
        getenv("SERVER_PROTOCOL"), getenv("SERVER_SOFTWARE"));
    return 0;
}

2.编译:$g++ test.cpp -o test -l fcgi

如果提示找不到动态库,请在LD_LIBRARY_PATH或/etc/ld.so.conf中添加fcgi的安装路径

$export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/../fcgi/lib/

二、配置nginx.conf

$vim (nginx安装目录)/conf/nginx.conf

server {
    listen       8013; //设置监听端口
    server_name  localhost;

location =/test.cgi {

               fastcgi_pass  127.0.0.1:1010; //设置服务程序的地址和端口号,nginx收到http://127.0.0.1:8013/test.cgi请求时,会匹配到location = /test.cgi块,将请求传到后端的fastcgi应用程序处理

               fastcgi_index index.cgi;
               include fastcgi.conf;

    }

}

按Esc输入:wq进行保存

四、启动fcgi进程

spawn-fcgi默认在spawn-fcgi-1.6.3/src中,移动到spawn-fcgi中

$mv spawn-fcgi ../

启动fcgi进程spawn-fcgi -p 1010 ./(test所在的路径)/test

五、测试

$curl "http//127.0.0.1:8013/test.cgi"

<title>FastCGI Hello!</title><h1>FastCGI Hello!</h1><div>Request number 1 running on host : localhost </div>
<div>QUERY_STRING : 
</div><div>REMOTE_ADDR : 127.0.0.1
</div><div>REMOTE_PORT : 47058
</div><div>REQUEST_METHOD : GET
</div><div>CONTENT_TYPE : 
</div><div>CONTENT_LENGTH : 
</div><div>SERVER_PROTOCOL : /test.cgi
</div><div>REQUEST_URI : HTTP/1.1

</div><div>SERVER_SOFTWARE : nginx/1.12.2

说明cgi程序能够正常访问。

参考:https://www.cnblogs.com/studyskill/p/6524220.html

猜你喜欢

转载自blog.csdn.net/Li_suhuan/article/details/79933395