Which is the most elegant way to check which apache modules are enabled?
11 Answers
You're on Ubuntu so try:
apache2ctl -M
-
13
-
3apache2 -M results in this error
apache2: bad user name ${APACHE_RUN_USER}
– udoCommented May 17, 2011 at 20:07 -
4Fair 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. Commented May 17, 2011 at 20:23
-
10
-
4Note there are many useful options (flags) to
apache2ctl
but 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. Commented Sep 27, 2014 at 9:55
httpd -M
will tell you which modules are built-in or shared.
-
hmm... I'm getting a "bash: httpd: command not found" when launching httpd -M as root– udoCommented May 17, 2011 at 19:58
-
So then specify the full path to the
httpd
executable. Commented May 17, 2011 at 19:59 -
11@IgnacioVazquez-Abrams: On Ubuntu (and other Debian based distributions), the name is
apache2
and nothttpd
, which is why it is not found. Commented Apr 11, 2012 at 9:13 -
3Apache is httpd on redhat. Try one of the other answers if this one doesn't work for you. Commented Jan 14, 2013 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 *.php
file created for this trick should have random/hard-to-guess name and be deleted as quickly as possible after gathering neccessary info.
-
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. Commented Jul 15, 2015 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/
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
-
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 Commented May 17, 2011 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 Commented May 20, 2011 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.
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.
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']