Apache httpd module learning memo 1

module

Modules are pieces of code which can be used to provide or extend functionality of the Apache HTTP Server.
Modules can either be statically or dynamically included with the core.
For static inclusion, the module’s source code has to be added to the server’s source distribution and to compile the whole server.
Dynamically included modules add functionality to the server by being loading as shared libraries during start-up or restart of the server. In this case the module mod_so provides the functionality to add modules dynamically.

check your httpd is enable for module

[root@controller ~]# httpd -l
Compiled in modules:
  core.c
  mod_so.c # must have
  http_core.c

apxs - APache eXtenSion tool

可以使用apxs工具编译你的module

$ apxs -i -a -c mod_foo.c
gcc -fpic -DSHARED_MODULE -I/path/to/apache/include -c mod_foo.c
ld -Bshareable -o mod_foo.so mod_foo.o
cp mod_foo.so /path/to/apache/modules/mod_foo.so
chmod 755 /path/to/apache/modules/mod_foo.so
[activating module `foo' in /path/to/apache/etc/httpd.conf]
$ apachectl restart
/path/to/apache/sbin/apachectl restart: httpd not running, trying to start
[Tue Mar 31 11:27:55 1998] [debug] mod_so.c(303): loaded module foo_module
/path/to/apache/sbin/apachectl restart: httpd started
$ _ 

查看httpd加载的module

[root@controller ~]# httpd -t -D DUMP_MODULES
...
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 access_compat_module (shared)
 ...
 proxy_ftp_module (shared)
 proxy_http_module (shared)
 proxy_scgi_module (shared)
 proxy_wstunnel_module (shared)

查看每个module中的directive

# httpd -L >& httpd_directives.txt
# vi httpd_directives.txt
...

<Directory (core.c)
        Container for directives affecting resources located in the specified directories
        Allowed in *.conf only outside <Directory>, <Files>, <Location>, or <If>
<Location (core.c)
        Container for directives affecting resources accessed through the specified URL paths
        Allowed in *.conf only outside <Directory>, <Files>, <Location>, or <If>
<VirtualHost (core.c)
        Container to map directives to a particular virtual host, takes one or more host addresses
        Allowed in *.conf only outside <Directory>, <Files>, <Location>, or <If>
<Files (core.c)
        Container for directives affecting files matching specified patterns
        Allowed in *.conf anywhere and in .htaccess
        when AllowOverride isn't None
...
LoadModule (mod_so.c)
        a module name and the name of a shared object file to load it from
        Allowed in *.conf only outside <Directory>, <Files>, <Location>, or <If>
LoadFile (mod_so.c)
        shared object file or library to load into the server at runtime
...
WSGIScriptAlias (mod_wsgi.c)
        Map location to target WSGI script file.
        Allowed in *.conf only outside <Directory>, <Files>, <Location>, or <If>
WSGIScriptAliasMatch (mod_wsgi.c)
        Map location pattern to target WSGI script file.
        Allowed in *.conf only outside <Directory>, <Files>, <Location>, or <If>
WSGIDaemonProcess (mod_wsgi.c)
        Specify details of daemon processes to start.
        Allowed in *.conf only outside <Directory>, <Files>, <Location>, or <If>
WSGISocketPrefix (mod_wsgi.c)

列出所有的vhost

[root@controller logs]# httpd -t -D DUMP_VHOSTS
VirtualHost configuration:
*:8042                 controller.example.com (/etc/httpd/conf.d/10-aodh_wsgi.conf:6)
*:8041                 controller.example.com (/etc/httpd/conf.d/10-gnocchi_wsgi.conf:6)
*:5000                 controller.example.com (/etc/httpd/conf.d/10-keystone_wsgi.conf:6)
*:8778                 controller.example.com (/etc/httpd/conf.d/10-placement_wsgi.conf:6)
*:80                   is a NameVirtualHost
         default server default (/etc/httpd/conf.d/15-default.conf:6)
         port 80 namevhost default (/etc/httpd/conf.d/15-default.conf:6)
         port 80 namevhost controller.example.com (/etc/httpd/conf.d/15-horizon_vhost.conf:6)
                 alias 192.168.12.22
                 alias controller.example.com
                 alias localhost

在对应的log文件中可以看到启动了四个进程去处理每个vhost的请求

[root@controller logs]# pwd
/etc/httpd/logs
[root@controller logs]# vi error_log

[Thu May 20 06:23:11.964544 2021] [:debug] [pid 23778] mod_wsgi.c(10587): mod_wsgi (pid=23778): Socket for 'aodh' is '/var/run/wsgi.23778.0.1.sock'.
[Thu May 20 06:23:11.965285 2021] [:debug] [pid 23778] mod_wsgi.c(10587): mod_wsgi (pid=23778): Socket for 'gnocchi' is '/var/run/wsgi.23778.0.2.sock'.
[Thu May 20 06:23:11.965597 2021] [:debug] [pid 23778] mod_wsgi.c(10587): mod_wsgi (pid=23778): Socket for 'keystone' is '/var/run/wsgi.23778.0.3.sock'.
[Thu May 20 06:23:11.965906 2021] [:debug] [pid 23778] mod_wsgi.c(10587): mod_wsgi (pid=23778): Socket for 'placement-api' is '/var/run/wsgi.23778.0.4.sock'.
[Thu May 20 06:23:11.966240 2021] [:debug] [pid 23778] mod_wsgi.c(10587): mod_wsgi (pid=23778): Socket for 'apache' is '/var/run/wsgi.23778.0.5.sock'.

debug level

[root@controller logs]# vi /etc/httpd/conf/httpd.conf
LogLevel debug

查看http启动的进程

[root@controller logs]# /usr/sbin/apachectl status
* httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           `-openstack-dashboard.conf
   Active: active (running) since Thu 2021-05-20 06:23:11 EDT; 43min ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 23639 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 12666 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
  Process: 23731 ExecStartPre=/usr/bin/python2 /usr/share/openstack-dashboard/manage.py compress --force -v0 (code=exited, status=0/SUCCESS)
  Process: 23710 ExecStartPre=/usr/bin/python2 /usr/share/openstack-dashboard/manage.py collectstatic --noinput --clear -v0 (code=exited, status=0/SUCCESS)
 Main PID: 23778 (httpd)
   Status: "Total requests: 105; Current requests/sec: 0; Current traffic:   0 B/sec"
    Tasks: 33
   CGroup: /system.slice/httpd.service
           |-23778 /usr/sbin/httpd -DFOREGROUND #httpd的第一个进程
           |-23780 aodh_wsgi       -DFOREGROUND
           |-23781 gnocchi_wsgi    -DFOREGROUND
           |-23782 keystone        -DFOREGROUND
           |-23783 placement_wsgi  -DFOREGROUND
           |-23784 horizon         -DFOREGROUND
           |-23785 horizon         -DFOREGROUND
           |-23786 /usr/sbin/httpd -DFOREGROUND
           |-23787 /usr/sbin/httpd -DFOREGROUND
           |-23788 /usr/sbin/httpd -DFOREGROUND
           |-23789 /usr/sbin/httpd -DFOREGROUND
           |-23790 /usr/sbin/httpd -DFOREGROUND
           |-23791 /usr/sbin/httpd -DFOREGROUND
           |-23792 /usr/sbin/httpd -DFOREGROUND
           `-23793 /usr/sbin/httpd -DFOREGROUND

使用pstree可以查看具体的父子关系

[root@controller logs]# pstree -capsl 23778
systemd,1 --switched-root --system --deserialize 22
  └─httpd,23778 -DFOREGROUND
      ├─httpd,23784 -DFOREGROUND # horizon
      │   ├─{httpd},23796
      │   ├─{httpd},23797
      │   └─{httpd},23798
      ├─httpd,23785 -DFOREGROUND # horizon
      │   ├─{httpd},23808
      │   ├─{httpd},23809
      │   └─{httpd},23810
      ├─httpd,23786 -DFOREGROUND
      ├─httpd,23787 -DFOREGROUND
      ├─httpd,23788 -DFOREGROUND
      ├─httpd,23789 -DFOREGROUND
      ├─httpd,23790 -DFOREGROUND
      ├─httpd,23791 -DFOREGROUND
      ├─httpd,23792 -DFOREGROUND
      ├─httpd,23793 -DFOREGROUND
      ├─httpd,23783 -DFOREGROUND # placement_wsgi
      │   ├─{httpd},23799
      │   ├─{httpd},23800
      │   └─{httpd},23801
      ├─httpd,23782 -DFOREGROUND # keystone
      │   ├─{httpd},23802
      │   ├─{httpd},23803
      │   └─{httpd},23804
      ├─httpd,23780 -DFOREGROUND # aodh_wsgi
      │   ├─{httpd},23805
      │   ├─{httpd},23806
      │   └─{httpd},23807
      └─httpd,23781 -DFOREGROUND # gnocchi_wsgi
          ├─{httpd},23811
          ├─{httpd},23812
          └─{httpd},23813

pstree中:-c是禁用merge功能,因为它默认是把相同的brahcn合并的; -a显示命令行参数;-p pid -s 父pid; -l long模式

httpd的生命周期应该是这样的

以下的图从Writing Apache Modules with PERL and C by Lincoln Stein, Doug MacEachern中snap的
我们看到httpd启动很多的子进程去处理用户的请求,所以肯定会牵扯到父子之间的相互交互。
在这里插入图片描述交互的细节在这里:
在这里插入图片描述

http://www.fmc-modeling.org/category/projects/apache/amp/4_3Multitasking_server.html
https://httpd.apache.org/docs/2.4/programs/apxs.html
http://www.fmc-modeling.org/category/projects/apache/amp/3_3Extending_Apache.html
http://www.fmc-modeling.org/category/projects/apache/amp/4_3Multitasking_server.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值