index.php Not Loading by Default: The Apache Directory Index Misconfiguration
When you install a fresh stack of CentOS, Apache, and PHP, it is common to encounter a behavior that feels counterintuitive: visiting a directory like http://example.com/myapp/ returns a 403 Forbidden error, while explicitly requesting http://example.com/myapp/index.php works perfectly. This discrepancy isn't a bug in your application logic or a PHP version issue; it is almost certainly a misconfiguration in Apache's default security settings regarding directory indexing.
By default, modern versions of Apache (specifically those using the mod_dir module, which is standard on CentOS) do not serve an index file automatically if a specific condition isn't met. The server checks for the existence of files named .htaccess, index.html, or index.php. However, simply having an index.php in your root directory isn't enough if the Options directive is restricted.
The core issue usually stems from the Options -Indexes directive. This setting tells Apache to refuse serving a directory listing and to prevent the automatic inclusion of index files unless explicitly overridden. On CentOS installations, the default configuration often includes this restriction to mitigate information leakage attacks, where an attacker could list all files in a directory to find vulnerable endpoints.
To resolve the issue, you need to inspect your .htaccess file or the main Apache configuration file (httpd.conf). If you are using .htaccess, look for a line that looks like this:
Options -Indexes +FollowSymLinks
If Indexes is disabled, Apache will not serve index.php automatically when the URL lacks a query string or specific file extension, effectively treating the request as if no index file exists. To fix this, you must explicitly re-enable index files for your application directory:
Options +Indexes +FollowSymLinks -MultiViews
However, there is a significant trade-off here. Enabling +Indexes allows Apache to generate a directory listing when no index.php file is found. While this fixes your immediate problem, it also exposes the contents of your directory (e.g., .git, backup files, or config snippets) if you forget to name your entry point index.php. In a production environment, exposing directory listings is a major security risk.
A more robust alternative that avoids this pitfall entirely is to rely solely on the application's entry point without enabling global directory indexing. You can achieve this by ensuring your .htaccess file contains the following logic:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
This approach forces every request to a subdirectory to be handled by index.php regardless of whether the file exists at that specific path. It effectively simulates "auto-loading" the index file without enabling directory listings for paths that don't have one. This is generally preferred for modern PHP applications, as it keeps your directory structure hidden while ensuring your entry point is always hit.
If you are unable to use .htaccess (perhaps because your host disables it or you are using a configuration managed centrally), you must edit the main Apache configuration file, typically located at /etc/httpd/conf/httpd.conf or within include files in /etc/httpd/conf.d/. Search for the <Directory "/var/www/html"> block and ensure that Indexes is not disabled globally for your web root.
<Directory "/var/www/html">
Options Indexes FollowSymLinks
</Directory>
Once you make these changes, remember to restart Apache to apply them:
systemctl reload httpd
It is worth noting that if you are using a Laravel application or similar frameworks, they often come with .htaccess files pre-configured to handle routing. If your framework's default file is overriding your manual settings, ensure the index.php directive inside it aligns with your intended behavior. For deeper insights into configuring PHP and Apache stacks for specific frameworks, resources like https://laravelcompany.com offer detailed guides on best practices that balance security with usability.
Ultimately, the choice between enabling Indexes or using rewrite rules depends on your threat model. If you prioritize hiding your file structure, rely on rewrite rules. If you need directory listings for debugging and accept the risk of exposing files when no index exists, re-enabling Options +Indexes is the direct fix.