C语言-apache mod(模块开发)-采用apxs开发实战(centos7.2 linux篇)
C语言-apache mod(模块开发)-采用apxs开发实战(centos7.2 linux篇)
名词解释: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功能。
二、centos7.2 linux下开发
0)(apache安装配置略)
1)安装 apxs
yum search apxs #查找apxs安装包,
yum install httpd-devel #apxs所在包,里面带的有apxs
2)建立一个开发目录
mkdir apachemod
进入apachemod目录
cd apachemod
3)用apxs建立一个mod模版
运行apxs -g -n helloworld(helloworld为模块名),会生成一个叫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,这下牛逼了吧</br>在centos7.2上哦\n", r);
return OK;
}
static void helloworld_register_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_register_hooks /* register hooks */
};
====================================================================================
4)编译并安装:
apxs -i -a -c mod_helloworld.c #运行之后生成的mod_helloworld.so
apxs生成之后会自动将
LoadModule helloworld_module /usr/lib64/httpd/modules/mod_helloworld.so
这一行添加到apache.conf 文件中
centos终端编译效果如下图:

5)配置apache
我们要做的就是编辑 apache.conf
编辑命令:
vi /etc/httpd/conf/httpd.conf
找到 LoadModule helloworld_module /usr/lib64/httpd/modules/mod_helloworld.so 这一行在下面加入
<Location /helloworld>
setHandler helloworld
</Location>
:wq #进行保存
6)然后重启apache服务
service httpd restart
重启后查看一下咱们的服务状态:
serice httpd status
查看一下8080端口(我设置的是8080,你也可以按照你的实际来)
lsof -i:8080
出现:
# lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 142314 root 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142315 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142316 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142317 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142318 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142319 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142320 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
表示一切正常
7)在浏览器里面输入http://loacalhost/helloworld,就可以看到我们返回的内容)
运行结果如下图:

大功告成
8)centos的代码和编译后的so 模块下载地址
http://download.csdn.net/detail/tengyunjiawu_com/9811657
9)参考资料:
名词解释: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功能。
二、centos7.2 linux下开发
0)(apache安装配置略)
1)安装 apxs
yum search apxs #查找apxs安装包,
yum install httpd-devel #apxs所在包,里面带的有apxs
2)建立一个开发目录
mkdir apachemod
进入apachemod目录
cd apachemod
3)用apxs建立一个mod模版
运行apxs -g -n helloworld(helloworld为模块名),会生成一个叫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,这下牛逼了吧</br>在centos7.2上哦\n", r);
return OK;
}
static void helloworld_register_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_register_hooks /* register hooks */
};
====================================================================================
4)编译并安装:
apxs -i -a -c mod_helloworld.c #运行之后生成的mod_helloworld.so
apxs生成之后会自动将
LoadModule helloworld_module /usr/lib64/httpd/modules/mod_helloworld.so
这一行添加到apache.conf 文件中
centos终端编译效果如下图:
5)配置apache
我们要做的就是编辑 apache.conf
编辑命令:
vi /etc/httpd/conf/httpd.conf
找到 LoadModule helloworld_module /usr/lib64/httpd/modules/mod_helloworld.so 这一行在下面加入
<Location /helloworld>
setHandler helloworld
</Location>
:wq #进行保存
6)然后重启apache服务
service httpd restart
重启后查看一下咱们的服务状态:
serice httpd status
查看一下8080端口(我设置的是8080,你也可以按照你的实际来)
lsof -i:8080
出现:
# lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 142314 root 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142315 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142316 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142317 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142318 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142319 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
httpd 142320 apache 4u IPv6 1580018 0t0 TCP *:webcache (LISTEN)
表示一切正常
7)在浏览器里面输入http://loacalhost/helloworld,就可以看到我们返回的内容)
运行结果如下图:
大功告成
8)centos的代码和编译后的so 模块下载地址
http://download.csdn.net/detail/tengyunjiawu_com/9811657
9)参考资料:
http://httpd.apache.org/docs/2.2/programs/apxs.html
小白救星 QQ 99923309
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
2013-04-12 我的 云寻觅 搜索引擎 开始公测,前天开始开发,昨天买域名,今天发布在本机,请各路专家指正! 顺便开源!