Monthly Archives: October 2010

Enabling/Disabling Directory listing in Apache

Enabling/Disabling Directory listing in Apache

Locate your apache configuration file – httpd.conf and edit it using vi editor, normally in the location /etc/httpd/conf/httpd.conf

To turn on automatic directory indexing, find the Options directive that applies to the directory and add the Indexes keyword. For example:

<Directory /path/to/directory>
Options +Indexes
</Directory>

To turn off automatic directory indexing, remove the Indexes keyword from the appropriate Options line. To turn off directory listing for a particular subdirectory, you can use Options -Indexes. For example:

<Directory /path/to/directory>
Options -Indexes
</Directory>

I had a situation where none of the Options combinations worked.  Then only I noticed that the mod_autoindex was not loaded and the module as suh is not compiled with apache. I had to compile and enable the module to get directory listing working. The steps for compilation are given below.

1. Download the apache source tarball from apache download site

2. Untar the tarball and locate the mod_autoindex.c file in the directory `generators`

3.Switch to the directory `generators` and compile the .c file as shown below using apxs

[root@localhost autoindex]# /opt/apache2.0.55/bin/apxs -c mod_autoindex.c
/opt/apache2.0.55/build/libtool –silent –mode=compile gcc -prefer-pic  -DAP_HAVE_DESIGNATED_INITIALIZER -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -g -O2 -pthread -I/opt/apache2.0.55/include  -I/opt/apache2.0.55/include   -I/opt/apache2.0.55/include   -c -o mod_autoindex.lo mod_autoindex.c && touch mod_autoindex.slo
/opt/apache2.0.55/build/libtool –silent –mode=link gcc -o mod_autoindex.la  -rpath /opt/apache2.0.55/modules -module -avoid-version    mod_autoindex.lo

4. Once compiled, you can find the mod_autoindex.so file generated in the following location

[root@localhost autoindex]# ls -la .libs/mod_autoindex.so
-rwxr-xr-x 1 root root 90078 Oct  4 18:16 .libs/mod_autoindex.so

5.Copy the .so file to modules directory

[root@localhost autoindex]# cp .libs/mod_autoindex.so /opt/apache2.0.55/modules/

6. Add the following line to apache configuration file

LoadModule autoindex_module modules/mod_autoindex.so

7. Do a apacheconfigtest and restart apache

[root@localhost autoindex]# /opt/apache2.0.55/bin/apachectl configtest
Syntax OK

[root@localhost autoindex]# /opt/apache2.0.55/bin/apachectl restart

Recheck now if the URL is listing directories!