Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JRCSalter's avatar

Not sure why my docker setup sends a 404 error when visiting localhost

So, I have been attempting to make a minimal Docker container for Apache and PHP. So far, I've done pretty well, and thought everything was going alright. I had it set up so that all 404 errors would be rerouted back to the index file so I could manually handle them. It seemed to be working, and it was. But it was also sending a 404 error when loading localhost. And because I intended for that to load the index file as well, I didn't notice it until I randomly saw the REDIRECT_STATUS in the $_SERVER array, so I'm not sure what part of my set up is causing this problem.

https://github.com/JRCSalter/jmagic/tree/dev

Edit:

So, if I go to localhost/index.php, it does not generate a 404 error. Not sure why this is.

Edit 2:

Fixed it.

So as this was a minimal install, I only loaded what I needed to get the Docker container up and running, so I removed a load of stuff that is probably important, such as some modules.

Added the following in my Apache config:

LoadModule dir_module modules/mod_dir.so

<IfModule dir_module>
    DirectoryIndex index.php index.html 
</IfModule>

And now it works.

0 likes
3 replies
JRCSalter's avatar

@tisuchi I'm sure I could do that; however, I'm learning how to use Docker, so thanks, but I don't think that will help much.

LaryAI's avatar
Level 58

It seems like the original poster has already found a solution to the problem, but for the sake of clarity and for anyone else who might encounter a similar issue, I'll provide an explanation and a solution.

The issue was that when visiting localhost without specifying a file (like index.php), Apache was not defaulting to serve the index.php file, and thus was returning a 404 error. This is because Apache needs to be told what file to serve when a directory is requested without a file name.

The solution, as the original poster discovered, is to ensure that the mod_dir module is loaded and to set DirectoryIndex to the desired default file(s) in the Apache configuration. This tells Apache to look for index.php or index.html when a directory is accessed without a file name.

Here's the relevant configuration:

LoadModule dir_module modules/mod_dir.so

<IfModule dir_module>
    DirectoryIndex index.php index.html 
</IfModule>

This configuration can be placed in the main Apache configuration file (httpd.conf or apache2.conf), or in a virtual host file, depending on your setup.

For anyone setting up a Docker container with Apache and PHP, make sure to include this configuration to avoid the 404 error when accessing the root URL (localhost or localhost/).

Please or to participate in this conversation.