Linux apache + fastcgi 运行c/c++

本文提供了一个Linux环境下搭建Apache+FastCGI环境,运行C/C++程序的详细步骤,包括Apache的安装与配置、FastCGI模块的编译与安装、C/C++程序的测试与部署。通过本文,读者可以了解如何在Linux环境中配置和运行C/C++应用,特别适用于需要将C/C++应用作为CGI或FastCGI插件的场景。

       在Linux上搭建apache+fastcgi环境,说多了都是泪啊. 花费我几天时间,开源软件虽说好用,但是版本众多,文档缺乏,什么都只能自己摸索. 终于成功运行起来,特此记录. 

 

一. apache 安装

     此处下载 apache-2.2.27 版本,常规的 configure make 命令,安装非常方便

Shell代码   收藏代码
  1. wget http://mirrors.cnnic.cn/apache//httpd/httpd-2.2.27.tar.bz2  
  2. tar -xjzf httpd-2.2.27.tar.bz2  
  3. cd httpd-2.2.27  
  4. ./configure --prefix=/usr/local/apache2  
  5. make && make install  

 

  二. apache 配置

Conf代码   收藏代码
  1. <Directory "/usr/local/apache2/cgi-bin">  
  2.     AllowOverride None  
  3.     Options Indexes ExecCGI  # 执行CGI  
  4.     Order allow,deny  
  5.     Allow from all  
  6. </Directory>  
  7.   
  8. #添加文件映射  
  9. AddHandler cgi-script .cgi .py  

 

    运行服务: ./apachectl start

    打开 http://localhost, 成功会显示 It works!

 

三. 编译安装 fastcgi模块

    fastcgi有多个分支版本, mod_fastcgi 官方地址: http://www.fastcgi.com/,这个是官方提供的扩展模块,已经很久没维护了, 而且下载链接已失效了

    mod_fcgid 是apache官方维护的版本,与mod_fastcgi二进制兼容,原来的fastcgi程序不用重新编译就能工作. 其在mod_fastcgi基础上做了大量的代化

    这里选择mod_fastcgi测试,生产环境不建议使用:

 

Shell代码   收藏代码
  1. wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz  
  2. tar -xzvf mod_fastcgi-2.4.6.tar.gz  
  3. cd mod_fastcgi-2.4.6  
  4. cp Makefile.AP2 Makefile  
  5. make top_dir=/usr/local/apache2  
  6. make top_dir=/usr/local/apache install  

    

    编辑httpd.conf, 加入fastcgi模块

Httpd.conf代码   收藏代码
  1. LoadModule fastcgi_module modules/mod_fastcgi.so  
  2.     
  3. <IfModule fastcgi_module>  
  4.      AddHandler fastcgi-script .fcgi .py .pl  
  5. </IfModule>  

 

四. 测试运行

     下载安装 jnopy , fastcgi的python模块

      cd jonpy-2.2.27

      python setup.py install

   

Python代码   收藏代码
  1. #!/usr/bin/python  
  2. import jon.cgi as cgi  
  3. import jon.fcgi as fcgi  
  4.   
  5. class Handler(cgi.Handler):  
  6.   def process(self, req):  
  7.     req.set_header("Content-Type""text/plain")  
  8.     req.write("Hello, world!\n")  
  9.   
  10. fcgi.Server({fcgi.FCGI_RESPONDER: Handler}).run()  

 

    运行再一次出现500错误,查看日志:  [error]  No such file or directory exec of '/usr/local/apach2/cg-bin/hello_fcgi.py' failed 

    这就奇怪了,明明在/cgi-bin目录,打印的路径也是正常,怎么可能找不到呢? 难道是这脚本有猫腻,手动运行看看,报错:  ./hello_fcgi.py: /usr/bin/python^M: bad interpreter: 没有那个文件或目录

    原来这个文件有特殊字符,从windows下拷贝过来的文件很容易出现这个问题, vi 打开文件,直接 :set ff=unix 保存退出即可

 

    python with fastcgi

 

  五. 安装fastcgi开发库

      如果顺序进行上面几步,表明环境都已经搭建好,下面部署c++ fastcgi程序

      5.1 安装fastcgi开发库

Shell代码   收藏代码
  1. wget http://www.fastcgi.com/dist/fcgi.tar.gz  
  2. cd fcgi-2.4.1  
  3. ./configure  
  4. make   
  5. make install  

    make时出错, error: 'EOF' was not declared in this scope ,这EOF不是stdio.h中的吗, libfcgi/fcgio.cpp 中添加代码 #include <stdio.h> ,再次make,一切正常

   

   5.2 编码测试

C代码   收藏代码
  1. #include "fcgi_stdio.h"  
  2. #include <stdlib.h>  
  3.   
  4. void main(void)  
  5. {  
  6.     int count = 0;  
  7.     while(FCGI_Accept() >= 0)  
  8.     printf("Content-type: text/html\r\n"  
  9.            "\r\n"  
  10.            "<title>FastCGI Hello!</title>"  
  11.            "<h1>FastCGI Hello!</h1>"  
  12.            "Request number %d running on host <i>%s</i>\n",  
  13.             ++count, getenv("SERVER_NAME"));  
  14. }  

    编译: gcc -o fcgi_test.fcgi cgi_test.c -I/usr/local/include -L/usr/local/lib -lfcgi

  

   fast cgi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值