Apache on Ubuntu returns 404 Not Found for my site
Not FoundShort answer
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file. Apache appends the URL path to the DocumentRoot of the virtual host that answered. Either the file is outside that folder, or the host you called is served by a different virtual host.
Fix it fast
1Find out which cause you have
List the virtual hosts Apache has loaded and which one is the default. This shows which DocumentRoot answers your host name.
sudo apache2ctl -S2Match what you see, then jump to its fix
- 1
The URL path appended to DocumentRoot points at a file that does not existThe file is not inside the DocumentRootFix - 2
apache2ctl -S does not list your ServerNameThe new virtual host is not enabledFix - 3
Your site name returns the default site's content or a 404 from the default siteThe host name you call is not the ServerNameFix
- 1
What does the error look like?
Not FoundThe requested URL was not found on this server.
Cause 1 of 3
The file is not inside the DocumentRoot
1Check
Apache serves the requested path by adding it to DocumentRoot, so a request for /about.html maps to DocumentRoot plus /about.html. Read the DocumentRoot of your site and list that folder.
grep -R "DocumentRoot" /etc/apache2/sites-enabled/
ls -l /var/www/example.com/2Fix
Move the files into the DocumentRoot folder, or point DocumentRoot at the folder that holds them.
<VirtualHost *:80>
ServerName example.com
DocumentRoot "/var/www/example.com"
</VirtualHost>3Verify
Reload Apache and request the file.
sudo apache2ctl configtest && sudo apache2ctl graceful
curl -I http://example.com/about.htmlConfirmed by httpd.apache.org
Cause 2 of 3
The new virtual host is not enabled
1Check
On Ubuntu a site is active only when a symlink to its file exists in sites-enabled. The first virtual host for an address and port answers every request that matches none of the others.
ls -l /etc/apache2/sites-enabled/2Fix
Enable the site with a2ensite, then reload Apache.
sudo a2ensite example.com.conf
sudo apache2ctl graceful3Verify
apache2ctl -S now lists the name and the DocumentRoot.
sudo apache2ctl -SConfirmed by manpages.ubuntu.com
Cause 3 of 3
The host name you call is not the ServerName
1Check
Name-based virtual hosts are chosen by the Host header. A request for a name with no matching ServerName falls to the first virtual host. Test with the header set explicitly.
curl -I -H "Host: example.com" http://127.0.0.1/2Fix
Set ServerName to the name in the address bar, and for local testing make the name resolve to your machine in the hosts file.
# /etc/hosts
127.0.0.1 example.com3Verify
The curl test with the Host header returns 200.
curl -I -H "Host: example.com" http://127.0.0.1/Confirmed by httpd.apache.org
How do I stop it coming back?
- Run apachectl configtest before every reload.
- Keep one folder per site and write its path in the virtual host once.
- After adding a site, check it with apache2ctl -S.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Servers
dnf cannot install MySQL on Amazon Linux 2023
dnf cannot find a mysql-server package on Amazon Linux 2023
Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.
3 causes, each with a fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
Apache warns that a ${VAR} config variable is not defined
${VAR} left unchanged in the configuration, with a warning in the error log
Apache expands ${VAR} from Define or from the shell environment that exists when the server starts.
3 causes, each with a fixServers
Apache refuses to start: DefaultRuntimeDir must be a valid directory
DefaultRuntimeDir must be a valid directory, absolute or relative to ServerRoot
DefaultRuntimeDir is where Apache creates run-time files such as shared memory and locks.
2 causes, each with a fix