Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Apache on Ubuntu returns 404 Not Found for my site

ErrorNot Found

Short 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.

  • Applies to Apache 2.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 -S
  2. 2Match what you see, then jump to its fix

    1. 1The URL path appended to DocumentRoot points at a file that does not existThe file is not inside the DocumentRootFix
    2. 2apache2ctl -S does not list your ServerNameThe new virtual host is not enabledFix
    3. 3Your site name returns the default site's content or a 404 from the default siteThe host name you call is not the ServerNameFix

What does the error look like?

  • Not Found
  • The 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.html

Confirmed by httpd.apache.org

Did this fix it?

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 graceful

3Verify

apache2ctl -S now lists the name and the DocumentRoot.

sudo apache2ctl -S

Confirmed by manpages.ubuntu.com

Did this fix it?

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.com

3Verify

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

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.