Apache模块开发
环境
Centos8
Step1: 安装必要软件
yum install gcc httpd-devel.x86_64 libffi-devel openssl-devel -y
dnf install redhat-rpm-config
Step2: 生成代码
[root@localhost helloworld]# apxs -g -n helloworld
Creating [DIR] helloworld
Creating [FILE] helloworld/Makefile
Creating [FILE] helloworld/modules.mk
Creating [FILE] helloworld/mod_helloworld.c
Creating [FILE] helloworld/.deps
Step3:编译
[root@localhost helloworld]# apxs -c mod_helloworld.c
/usr/lib64/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -DLINUX -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/httpd -I/usr/include/apr-1 -I/usr/include/apr-1 -c -o mod_helloworld.lo mod_helloworld.c && touch mod_helloworld.slo
/usr/lib64/apr-1/build/libtool --silent --mode=link gcc -Wl,-z,relro,-z,now -o mod_helloworld.la -rpath /usr/lib64/httpd/modules -module -avoid-version mod_helloworld.lo
[root@localhost helloworld]# ll .libs/
total 120
-rw-r--r--. 1 root root 41304 Apr 9 00:39 mod_helloworld.a
lrwxrwxrwx. 1 root root 20 Apr 9 00:39 mod_helloworld.la -> ../mod_helloworld.la
-rw-r--r--. 1 root root 966 Apr 9 00:39 mod_helloworld.lai
-rw-r--r--. 1 root root 41072 Apr 9 00:39 mod_helloworld.o
-rwxr-xr-x. 1 root root 28040 Apr 9 00:39 mod_helloworld.so
Step4:安装
[root@localhost helloworld]# apxs -i mod_helloworld.la
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' mod_helloworld.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install install mod_helloworld.la /usr/lib64/httpd/modules/
libtool: install: install .libs/mod_helloworld.so /usr/lib64/httpd/modules/mod_helloworld.so
libtool: install: install .libs/mod_helloworld.lai /usr/lib64/httpd/modules/mod_helloworld.la
libtool: install: install .libs/mod_helloworld.a /usr/lib64/httpd/modules/mod_helloworld.a
libtool: install: chmod 644 /usr/lib64/httpd/modules/mod_helloworld.a
libtool: install: ranlib /usr/lib64/httpd/modules/mod_helloworld.a
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/jdk14/bin:/root/bin:/sbin" ldconfig -n /usr/lib64/httpd/modules
----------------------------------------------------------------------
Libraries have been installed in:
/usr/lib64/httpd/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/lib64/httpd/modules/mod_helloworld.so
Step5:配置
再在httpd.conf中加入这一Module:
vi /etc/httpd/conf/httpd.conf
LoadModule helloworld_module modules/mod_helloworld.so
<Location /helloworld>
SetHandler helloworld
</Location>
Step6:重启apache 然后输入 http://127.0.0.1/helloworld 就可以看到
[root@localhost helloworld]#systemctl restart httpd
[root@localhost helloworld]# curl http://127.0.0.1/helloworld
The sample page from mod_helloworld.c