• C语言-apache mod(模块开发)-采用VS2017开发实战(windows篇)


    C语言-apache mod(模块开发)-采用VS2017开发实战(windows篇)

    名词解释:apxs

    apxs is a tool for building and installing extension modules for the Apache HyperText Transfer Protocol (HTTP) server. 

    apxs是用来编译和安装 apache 服务器的扩展模块(mod)、也能生成项目模版(下面有具体使用说明)


    名词解释:MinGW
    MinGW,是Minimalist GNUfor Windows的缩写。它是一个可自由使用和自由发布的Windows特定头文件和使用GNU工具集导入库的集合,
    允许你在GNU/Linux和Windows平台生成本地的Windows程序而不需要第三方C运行时(C Runtime)库。
    MinGW 是一组包含文件和端口库,其功能是允许控制台模式的程序使用微软的标准C运行时(C Runtime)库(MSVCRT.DLL),该库在所有的 NT OS 上有效,
    在所有的 Windows 95发行版以上的 Windows OS 有效,使用基本运行时,你可以使用 GCC 写控制台模式的符合美国标准化组织(ANSI)程序,
    可以使用微软提供的 C 运行时(C Runtime)扩展,与基本运行时相结合,就可以有充分的权利既使用 CRT(C Runtime)又使用 WindowsAPI功能。

    一、apache mod模块 windows开发
    1)

    下载apxs apxs_win32.tar.gz,下载地址
    http://download.csdn.net/detail/tengyunjiawu_com/9811400

    2)
    安装strawberryperl或active perl
    下载地址:http://strawberryperl.com/releases.html
    我安装的是active perl,所以还需要安装 dmake
    dos命令行执行:ppm install dmake

    3)解压apxs_win32.tar.gz 
       解压到C:apacheapache2.4.9inapxs

    4)进入C:apacheapache2.4.9inapxs目录
    perl Configure.pl --with-apache2=D:/wamp/bin/apache/apache2.4.9 --with-apache-prog=httpd.exe

    5)切换到Apache安装目录下的bin文件夹中,输入apxs,
    cd C:apacheapache2.4.9in
    apxs
    输出:如有解释apxs用法的文本出现(如下),则表明安装完成。
    D:wampinapacheapache2.4.9in>apxs
    Usage: apxs -g [-S <var>=<val>] -n <modname>
          apxs -q [-v] [-S <var>=<val>] <query> ...
          apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]
          [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]
          [-Wl,<flags>] [-p] <files> ...
          apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...
          apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...

    6)将apxs所在的目录设置加入系统环境变量PATH

    7)使用apxs

    apxs编译时还需用到C/C++编译器,一般有两种选择,一种是使用VC(Visual Studio)自带的编译器,还有一种就是自行安装,比如minGW等。
    对使用VS编译的,可尝试修改apache下的build目录中config_vars.mk配置文件:
    CC = gcc 的gcc改为cl.exe
    LD = g++的g++改为link.exe
    CPP = gcc-E的gcc-E删掉

    我因为使用的是mingw,所以,实际应用步骤可能不太完整,所以使用VS2017编译的时候没有使用apxs进行编译

    vs2017编译如下:


    8)用VS2017编译apache模块
       a)运行apxs -g -n helloworld(helloworld为模块名),会生成一个叫helloworld的目录和模板代码
       b)打开vs2017建立一个空的C语言项目,项目名称mod_helloworld,将上面用apxs生成的mod_helloworld.c拖进项目
       c)项目设置部分
    1、include C:apacheapache2.4.9include 
    2、lib部分附加目录库 C:apacheapache2.4.9lib 
    3、链接器,命令行,其他选项添加 libapr-1.lib libaprutil-1.lib libapriconv-1.lib libhttpd.lib
            4、代码生成 运行库选择:多线程 (/MT)
       d)进入helloworld目录,编辑mod_helloworld.c(这就是我们要开发的内容),代码如下:

    =========================================================================================

    /* 
    **  mod_helloworld.c -- Apache sample helloworld module
    **  [Autogenerated via ``apxs -n helloworld -g'']
    **
    **  To play with this sample module first compile it into a
    **  DSO file and install it into Apache's modules directory 
    **  by running:
    **
    **    $ apxs -c -i mod_helloworld.c
    **
    **  Then activate it in Apache's httpd.conf file for instance
    **  for the URL /helloworld in as follows:
    **
    **    #   httpd.conf
    **    LoadModule helloworld_module modules/mod_helloworld.so
    **    <Location /helloworld>
    **    SetHandler helloworld
    **    </Location>
    **
    **  Then after restarting Apache via
    **
    **    $ apachectl restart
    **
    **  you immediately can request the URL /helloworld and watch for the
    **  output of this module. This can be achieved for instance via:
    **
    **    $ lynx -mime_header http://localhost/helloworld 
    **
    **  The output should be similar to the following one:
    **
    **    HTTP/1.1 200 OK
    **    Date: Tue, 31 Mar 1998 14:42:22 GMT
    **    Server: Apache/1.3.4 (Unix)
    **    Connection: close
    **    Content-Type: text/html
    **  
    **    The sample page from mod_helloworld.c
    */ 


    #include "httpd.h"
    #include "http_config.h"
    #include "http_protocol.h"
    #include "ap_config.h"


    /* The sample content handler */
    static int helloworld_handler(request_rec *r)
    {
        if (strcmp(r->handler, "helloworld")) {
            return DECLINED;
        }
        r->content_type = "text/html";      


        if (!r->header_only)
            ap_rputs("小样,用C语言开发一个Apache的Module,这下牛逼了吧 ", r);
        return OK;
    }


    static void helloworld_hooks(apr_pool_t *p)
    {
        ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
    }


    /* Dispatch list for API hooks */
    module AP_MODULE_DECLARE_DATA helloworld_module = {
        STANDARD20_MODULE_STUFF, 
        NULL,                  /* create per-dir    config structures */
        NULL,                  /* merge  per-dir    config structures */
        NULL,                  /* create per-server config structures */
        NULL,                  /* merge  per-server config structures */
        NULL,                  /* table of config file commands       */
        helloworld_hooks  /* register hooks                      */
    };


    =========================================================================================

       e)编译生成如下图:


       
       f)将mod_helloworld.dll拷贝到Apache2.2modules下
       g)修改Apache2.2confhttpd.conf,在末尾加上
         LoadModule helloworld_module modules/mod_helloworld.dll #(dll改成扩展名.so也一样的)
         <Location /helloworld>
    setHandler helloworld
         </Location>
        
       h)启动apache,在浏览器里面输入http://loacalhost/helloworld,就可以看到我们返回的内容)
         大功告成


    9)VS2017整体工程代码,下载地址:

       http://download.csdn.net/detail/tengyunjiawu_com/9811386


    10)运行结果如下:http://loacalhost/helloworld



    本人原创,未经许可,可以随意转载!

  • 相关阅读:
    随机数组找丢失数据
    js打开新页面 关闭当前页 关闭父页面
    Markdown测试
    ThinkPHP中的时间自动填充 无法获取时间
    页面在谷歌浏览器和IE浏览器顶部多出空白行,火狐显示正常
    Thinkphp中验证码不显示解决办法
    106运用SWITCH语句打印星期几的单词
    声明两个变量并赋值计算出矩形的面积
    不用*用移位运算符计算21*16的数值是多少
    97.经典实例,计算球形的面积和体积
  • 原文地址:https://www.cnblogs.com/bdccloudy/p/7665218.html
Copyright © 2020-2023  润新知