windows apache+cgi 运行c/c++

apache是运用比较广泛的web服务器,大多数是php应用,apache可以启动单独的c/c++进程,为web提供服务,这大大丰富的web内容,当然cgi这种方式不适用于大规模并发的场景. Ngix 据说比apache性能更高,可以考虑

1. apache安装

    windows下直接下载二进制安装文件,下一步即可; Linux下直接下载源码安装

2. 配置

   $APACHE/conf/httpd.conf

   1. 将 LoadModule cgi_module modules/mod_cgi.so 前注释取消

   2. cgi的执行权限

   <Directory "D:/Apache2.2/cgi-bin">

    AllowOverride None

    Options Indexes ExecCGI   # 添加ExecCGI,允许执行cgi程序

    Order allow,deny

    Allow from all

  </Directory>

   3. 添加文件映射

   AddHandler cgi-script .cgi .pl .py 

  

   注: *.html文件默认放在$APACHE/htdocs下,cgi处理程序放在$APACHE_HOME/cgi/bin下; 修改配置文件后,apache服务需要重启; 部署cgi程序,直接拷贝至/cgi-bin,无需重启服务

3. 测试运行

   3.1  安装成功后,直接运行键入http://localhost,会在浏览器在显示 It works! , 实际上执行的是 $APACHE/htdocs/index.html
   3.2  运行C++代码
#include <iostream>
#include <stdio.h>

using namespace std;

int main() {
    printf("Content-type:text/html\n\n");
    printf("");
    printf("");
    printf("Hello World - 第一个 CGI \n");
    printf(" ");

   /*
   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>Hello World - 第一个 CGI 程序</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";
   cout << "<h2>Hello World! 这是我的第一个 CGI 程序</h2>\n";
   cout << "</body>\n";
   */

    return 0;
}

1、编译后生成Hello.exe
2 、启动httpd服务
3 、浏览器输入: http://localhost/cgi-bin/Hello.exe, 我们可以在页面上看到如下内容:

猜你喜欢

转载自blog.csdn.net/qq_26591517/article/details/80413970