416

Which is the most elegant way to check which apache modules are enabled?

bertieb
  • 7,543
udo
  • 8,061

11 Answers11

504

You're on Ubuntu so try:

apache2ctl -M
Giacomo1968
  • 58,727
Linker3000
  • 28,240
128

httpd -M will tell you which modules are built-in or shared.

43

Edit 2023: WARNING - use this with caution. See below.

Nothing from the answers above works if you can’t run commands on a remote server. If you have only “user” privileges or none at all try creating a test.php script:

<pre>
<?php
print_r(apache_get_modules());
?>
</pre>

Though, it will only work if PHP is installed as mod_php.


To explain warning part - seeing this to gain some popularity over the years I feel obligated to ask to look at the first comment below.

In short - exposing this information in public my hurt you. So any *.phpfile created for this trick should have random/hard-to-guess name and be deleted as quickly as possible after gathering neccessary info.

yatsek
  • 671
34

Maybe this will help for some people on shared hosts with no access to httpd, apachectl or processes:

Enabled modules: ls /etc/apache2/mods-enabled/

Available modules: ls /etc/apache2/mods-available/

19

You can also use apachectl

apachectl -t -D DUMP_MODULES
14

I think there are actually three questions here. I'm not sure which you're asking.

  • What modules do you have on disk. What are all the modules you can use.

This would be (usually) in the modules directory of your apache distribution, usually /etc/httpd/modules/

  • What modules is any specific instance configured to run.

This can be checked with /usr/sbin/httpd -M, at least for the base system apache. If you want to check on a specific config file /usr/sbin/httpd -M -f /path/to/config/file

  • What's in a running apache

To get a lot of info, you can see it with http://machinename/server-info/ This isn't configured by default, you'd have to configure it in. Its a bit of an info leak, so configure it so only local people can see it.

If you're on the machine and you have access to be the running user, you can also see what's loaded by checking the process. You can find the parent process with:

ps -ef | gawk '/httpd/ && $3 == 1{print $2}'

Then check out

cat /proc/PID_FROM_ABOVE/maps
Rich Homolka
  • 32,350
11

If you are on Redhat/CentOS, httpd is used in place of apache2ctl.

This means you need to use the

httpd -M

However, httpd is almost never in the path you expect.

I can confirm on CentOS 5.8 the actual path is /usr/sbin/httpd.

/usr/sbin/httpd -M

But if that is not the path, you can discover it. Here is how I was able to do so.

First, I checked the daemon being used to control it.

less /init.d/httpd

Around line 40ish

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd

Which told me exactly where to find it. Hope this helps.

geedew
  • 217
8

List all enabled modules

a2query -m
3

On my gentoo, I can execute apache2ctl modules and see the modules listed.

Canadian Luke
  • 24,640
2

Checking from within php script (for mod_xsendfile):

if (in_array(PHP_SAPI, array('apache','apache2filter','apache2handler'))
  && in_array('mod_xsendfile', apache_get_modules()))
  \\doSomething();

The check for PHP_SAPI is to exclude when php is running as CGI, as apache_get_modules() does not work in that context. Additionally, if this is run on php < 5.0.0, only the apache2handler context will produce the expected result.

noobish
  • 381
1

I created a small python script to help you with it. Please have a look at https://github.com/zioalex/unused_apache_modules

This is what you can expect from it:

curl http://localhost/server-info > http_modules_test.txt
cat http_modules_test.txt| python find_unused_apache_mod.py

1
Module name mod_python.c
Configuration Phase Participation: 4
Request Phase Participation: 11
Current Configuration: 3

2
Module name mod_version.c
Configuration Phase Participation: 0
Request Phase Participation: 0
Current Configuration: 1

3
Module name mod_proxy_connect.c
Configuration Phase Participation: 0
Request Phase Participation: 0
Current Configuration: 0

To remove safely:
 ['mod_proxy_connect.c']
POPPED:  mod_proxy_connect.c

To KEEP:  ['mod_python.c', 'mod_version.c', 'mod_proxy_connect.c']
Zioalex
  • 121