411

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

bertieb
  • 7,344
  • 36
  • 42
  • 54
udo
  • 8,001
  • 7
  • 38
  • 47

11 Answers11

498

You're on Ubuntu so try:

apache2ctl -M
Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
Linker3000
  • 27,498
  • 3
  • 52
  • 73
  • 13
    **apache2ctl -M** works great – udo May 17 '11 at 20:06
  • 3
    **apache2 -M** results in this error `apache2: bad user name ${APACHE_RUN_USER}` – udo May 17 '11 at 20:07
  • 4
    Fair enough - it's due to the fact that you are not running the command as the apache run time user (probably www-data) defined in the apache config. There is a way to fix this but you might as well stick to apache2ctl. – Linker3000 May 17 '11 at 20:23
  • 9
    `sudo apache2ctl -M | sort` – mmdemirbas Jul 06 '12 at 13:30
  • 4
    Note there are many useful options (flags) to `apache2ctl` but they are listed neither in the manpage nor in `apache2ctl --help`. That is because they are handed through to `httpd`. They are listed in the [httpd documentation](http://httpd.apache.org/docs/2.2/programs/httpd.html) only. – Lutz Prechelt Sep 27 '14 at 09:55
  • @Linker3000 : This require root whereas I only have an apache directory and basic user rights with command line. – user2284570 Jun 13 '15 at 00:33
  • 1
    use ```/usr/sbin/apachectl -M``` in RHEL, Centos, Amazonlinux, ... – lrkwz Feb 23 '17 at 14:25
  • @mmdemirbas I run with: `apache2ctl -M | sort -b` - this way the `Loaded Modules:` line still goes first. – Tomasz Gandor Oct 30 '17 at 21:21
  • works with suse as well – Dwza Mar 07 '18 at 15:54
127

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

Ignacio Vazquez-Abrams
  • 111,361
  • 10
  • 201
  • 247
42

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.

gignu
  • 129
  • 4
yatsek
  • 631
  • 7
  • 7
  • 4
    Also, you'll want to not have this be publicly visible. Might want to restrict that result to client's with an administrator's IP. And you'll want to remove that script as soon as you're done with it. Because defense in depth; don't make it easier than it needs to be. – Parthian Shot Jul 15 '15 at 20:20
33

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/

CodeBrauer
  • 501
  • 4
  • 13
19

You can also use apachectl

apachectl -t -D DUMP_MODULES
Edward J Beckett
  • 1,059
  • 1
  • 10
  • 15
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
  • 31,057
  • 6
  • 55
  • 80
  • 1
    Useful info but because the OP is using Ubuntu, the file names and locations are different - for example: /usr/sbin/apache2 instead of httpd, and ps -ef | gawk '/apache2/ && $3 == 1{print $2}' The location of the modules is handled differently, with mods-available and mods-enabled subfolders – Linker3000 May 17 '11 at 21:40
  • Thanks @Linker3000... You're right, this is for RedHat/Centos, I'll let your comment stand on how to convert to Ubuntu – Rich Homolka May 20 '11 at 17:03
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
  • 2
  • 4
6

List all enabled modules

a2query -m
MetalGodwin
  • 206
  • 2
  • 3
3

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

Canadian Luke
  • 24,199
  • 39
  • 117
  • 171
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
  • 4
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
  • 4