Which is the most elegant way to check which apache modules are enabled?
11 Answers
httpd -M will tell you which modules are built-in or shared.
- 112,653
-
hmm... I'm getting a "bash: httpd: command not found" when launching httpd -M as root – udo May 17 '11 at 19:58
-
-
10@IgnacioVazquez-Abrams: On Ubuntu (and other Debian based distributions), the name is
apache2and nothttpd, which is why it is not found. – Daniel Andersson Apr 11 '12 at 09:13 -
3Apache is httpd on redhat. Try one of the other answers if this one doesn't work for you. – Bryan Austin Jan 14 '13 at 15:46
-
4
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.
- 651
-
4Also, 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
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/
- 521
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
- 31,505
-
1Useful 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
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.
- 217
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.
- 381
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']
- 121
apache2: bad user name ${APACHE_RUN_USER}– udo May 17 '11 at 20:07sudo apache2ctl -M | sort– mmdemirbas Jul 06 '12 at 13:30apache2ctlbut they are listed neither in the manpage nor inapache2ctl --help. That is because they are handed through tohttpd. They are listed in the httpd documentation only. – Lutz Prechelt Sep 27 '14 at 09:55/usr/sbin/apachectl -Min RHEL, Centos, Amazonlinux, ... – lrkwz Feb 23 '17 at 14:25apache2ctl -M | sort -b- this way theLoaded Modules:line still goes first. – Tomasz Gandor Oct 30 '17 at 21:21