在Ubuntu 16.04上配置Apache模块

3
我在Ubuntu 16.04上创建了一个“Hello World”模块。
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>

static int helloworld_handler(request_rec* r)
{
    if (!r->handler || strcmp(r->handler, "helloworld"))
        return DECLINED;

    if (r->method_number != M_GET)
        return HTTP_METHOD_NOT_ALLOWED;

    ap_set_content_type(r, "text/html");
    ap_rprintf(r, "Hello, world!");
    return OK;
}

static void register_hooks(apr_pool_t* pool)
{
    ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

module AP_MODULE_DECLARE_DATA helloworld_module = {
    STANDARD20_MODULE_STUFF,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    register_hooks
};

使用以下命令编译它:

apxs -iac mod_helloworld.c

我能看到该模块正在启动。
apache2ctl -M

并且。
a2enmod

如果我运行

sudo a2enmod helloworld

我看到这个模块已经启用了

我还将helloworld.conf插入到mods-available文件夹中

<Location /helloworld>
    SetHandler helloworld_handler
</Location>

文件helloworld.load包含以下内容:

LoadModule helloworld_module  /usr/lib/apache2/modules/mod_helloworld.so

我该如何配置它,以便在浏览器中调用输出?例如:

http://localhost/helloworld

mod_helloworld.so 位于 /usr/lib/apache2/modules

如果我执行 root@ubuntu:/var/log/apache2# tail -f access.log

我会得到以下结果:

127.0.0.1 - - [03/Aug/2017:03:18:14 -0700] "GET /helloworld HTTP/1.1" 404 500 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0"

如何解决这个问题?
1个回答

1
你正在检查 C 代码中的 "helloworld",但是使用 SetHandler 设置的是 "helloworld_handler"。你需要改为 "SetHandler helloworld" 才能使你的模块不被 DECLINE。

谢谢,我以为必须设置处理程序函数。 - eeadev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接