How to generate index.html for each directory under /var/www/html

I have downloaded confluent software for upgrade under /var/www/html directory. I need to create an index.html for directory listing for each directory. I tried adding below to .htaccess file in the directory (I saw it somewhere, where the index.html existed with files in the directory), but it did not work:

RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]

Is there a utility or tool that will allow me to do that? Someone asked me to create it manually, but I don’t really want to create it manually.

UPDATE

@Ipor Sircer / @Marcus Müller

I updated the httpd.conf file – now it is like below:

# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

# Further relax access to the default document root:
<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options +Indexes +FollowSymLinks
    IndexOptions +FancyIndexing +HTMLTable

    #Options None

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    #AllowOverride None
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

This is under /var/www/html:

pwd
/var/www/html

drwxr-xr-x 5 root root  86 Nov  7 18:10 .
drwxr-xr-x 4 root root  33 Aug 12 15:33 ..
drwxr-xr-x 4 root root  46 Nov  6 13:35 clients
drwxr-xr-x 3 root root  55 Nov  6 19:52 packages.confluent.io
drwxr-xr-x 3 root root  17 Nov  6 15:57 rpm

But when I go to the server, I get the below:

http://server

Whereas if I go to server/rpm, I get this:

http://server/rpm

What else do I need to change, so I can see the directory under /var/www/html from the browser?

Asked By: adbdkb

||

As @Ipor Sircer suggested, you can use FancyIndexing like you did in your updated configuration:

<Directory "/var/www/html">
    Options +Indexes +FollowSymLinks
    IndexOptions +FancyIndexing +HTMLTable
</Directory>

You can read more about what this option does here. However, this likely won’t work if you don’t have the Apache module enabled.

To enable the mod_autoindex module in your Apache configuration, run the command sudo a2enmod autoindex then restart Apache with sudo systemctl restart apache2. Then your current configuration should display the directory contents as desired.


Update: My mistake, a2enmod is a debianism. This is how you can enable mod_autoindex on RHEL:

  1. sudo nano /etc/httpd/conf/httpd.conf
  2. In the configuration file, look for the LoadModule section, which contains the directives to load various Apache modules. Add the following line to enable mod_autoindex:
LoadModule autoindex_module modules/mod_autoindex.so
  1. Save and exit with CTRL+x and confirm with y
  2. Restart Apache with sudo systemctl restart httpd

Then mod_autoindex should be enabled on your system.


Update 2: Try following the instructions from this post, which involves removing the welcome.conf file and restarting the service again.

Answered By: ajmeese7
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.