今天在Ubutun上装上了Apache 服务器和Mysql,实验了一个自定义生成网页的Hook,Hook处理客户端的请求,从Mysql中读取数据,生成网页返回,现在记录过程如下:
(1) 安装Apache和配置
sudo apt-get install apache2
安装结束后:
产生的启动和停止文件是:/etc/init.d/apache2
启动:sudo apache2ctl -k start
停止:sudo apache2ctl -k stop
重新启动:sudo apache2ctl -k restart
配置文件保存在:/etc/apache2
参考:http://www.blogjava.net/duanzhimin528/archive/2010/03/05/314564.html
(2)Apache Hook modules
原理:
参考:http://blog.csdn.net/hguisu/article/details/7395181
(3)安装MySQL
c 连接mysql
http://blog.sina.com.cn/s/blog_682a588a0100ikh6.html
sudo cp ./usr/lib/x86_64-linux-gnu/libmysqlclient* ./usr/lib/
test1.cpp
-
#include <iostream>
-
#include <mysql/mysql.h>
-
#include <string>
-
using namespace std;
-
int main()
-
{
-
MYSQL mysql;
-
mysql_init(&mysql);
-
mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 3306, NULL, 0);
-
string sql = "insert into t1 (id, name) values (1, 'java1');";
-
int ret = mysql_query(&mysql, sql.c_str());
-
mysql_close(&mysql);
-
cout<<"test2 finished"<<","<<ret;
-
return 0;
-
}
生成的表
MySQL配置==>(配置密码)
%E5%AE%89%E8%A3%85%E6%8C%87%E5%8D%97
(4)编写Hooks访问数据库
增加对Apache 对C++和MySql的支持
在httpd.conf中增加两个LoadFile分别指向libstdc++.so和libmysqlclient.so
然后重新启动Apache
编写
hooks mod_helloworld.c
-
#include "httpd.h"
-
#include "http_config.h"
-
#include "http_protocol.h"
-
#include "ap_config.h"
-
-
#include <mysql/mysql.h>
-
-
#include <string>
-
#include <iostream>
-
#ifdef __cplusplus
-
using namespace std;
-
-
extern "C" {
-
-
#endif
-
-
//一段代码
-
static string getDataFromDB();
-
static int helloworld_handler(request_rec *r);
-
static void helloworld_register_hooks(apr_pool_t *p);
-
#ifdef __cplusplus
-
-
}
-
-
#endif
-
static string getDataFromDB()
-
{
-
string retString;
-
MYSQL mysql;
-
MYSQL_RES *result = NULL;
-
MYSQL_FIELD *field = NULL;
-
mysql_init(&mysql);
-
mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 3306, NULL, 0);
-
string str = "select id,name from t1;";
-
mysql_query(&mysql, str.c_str());
-
result = mysql_store_result(&mysql);
-
int rowcount = mysql_num_rows(result);
-
cout << rowcount << endl;
-
int fieldcount = mysql_num_fields(result);
-
cout << fieldcount << endl;
-
for(int i = 0; i < fieldcount; i++)
-
{
-
field = mysql_fetch_field_direct(result,i);
-
retString+=field->name;
-
retString+="\t\t";
-
// cout << field->name << "\t\t";
-
}
-
retString+="
";
-
MYSQL_ROW row = NULL;
-
row = mysql_fetch_row(result);
-
while(NULL != row)
-
{
-
for(int i=1; i<fieldcount; i++)
-
{
-
retString+=row[i];
-
retString+="\t\t";
-
}
-
row = mysql_fetch_row(result);
-
}
-
retString+="
";
-
mysql_close(&mysql);
-
return retString;
-
}
-
-
/* 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("test", r);
-
ap_rputs(getDataFromDB().c_str(), 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, /* 创建目录配置结构*/
-
NULL, /* 合并目录配置结构 */
-
NULL, /* 创建主机配置结构 */
-
NULL, /* 合并主机配置结构 */
-
NULL, /* 为模块配置相关指令 */
-
helloworld_register_hooks /* 注册模块的钩子函数 */
-
};
编译
生成
mod_helloworld.so==>copy 到/usr/lib/apache2/modules/目录下
修改httpd.conf
重新启动Apache
通过电脑浏览器访问:
192.168.1.101是本机的IP 地址
通过手机浏览器访问(局域网WIFI)
参考:
Apache模块开发之HelloWorld
http://andrew913.iteye.com/blog/398648
Apache模块编写==>怎么用apxs编译C++ 程序
http://uuchi.iteye.com/blog/1005136
在Apache中增加对C++和SSL 的支持
Mysql中文API
http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html
问题解决
Commands out of sync; you can’t run this command now
http://blog.sina.com.cn/s/blog_93b1f3f00100uydl.html
阅读(1577) | 评论(0) | 转发(0) |