Maaz Zindani

Loading the index…

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

Apache returns 403 Forbidden for a new VirtualHost

Error403 Forbidden

Short answer

Apache 2.4 denies a directory unless a Require rule allows it. A new VirtualHost whose DocumentRoot sits outside the default web folder has no allowing rule, so every request gets 403. Add a Directory block for the exact path with Require all granted, or fix a rule that uses the old Order and Allow syntax.

  • 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

    Read the last lines of the Apache error log and print the config that applies to your site. The message and the Directory block show which cause below fits.

    sudo tail -n 20 /var/log/apache2/error.log
    sudo apache2ctl -S
  2. 2Match what you see, then jump to its fix

    1. 1The DocumentRoot is outside /var/www, and the site file has no Directory block for itNo Directory block grants access to the DocumentRootFix
    2. 2The Directory block exists but its path goes through a symbolic linkThe Directory path is a symlink, not the real pathFix
    3. 3The Directory block has Order allow,deny or Allow from all, copied from an Apache 2.2 guideThe site still uses the old Order, Allow and Deny rulesFix

What does the error look like?

  • 403 Forbidden
  • You don't have permission to access this resource.

Cause 1 of 3

No Directory block grants access to the DocumentRoot

1Check

Apache's documentation recommends denying the filesystem root with Require all denied and granting access to specific directories. A DocumentRoot elsewhere inherits the denial.

grep -n "DocumentRoot" /etc/apache2/sites-enabled/*.conf
grep -n -A3 "<Directory" /etc/apache2/apache2.conf

2Fix

Add a Directory block for the exact DocumentRoot path with Require all granted, then reload.

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot "/srv/example/public"
    <Directory "/srv/example/public">
        Require all granted
    </Directory>
</VirtualHost>

3Verify

Test the syntax, reload and request the site.

sudo apache2ctl configtest && sudo systemctl reload apache2
curl -I http://example.com/

Confirmed by httpd.apache.org

Did this fix it?

Cause 2 of 3

The Directory path is a symlink, not the real path

1Check

Directory sections match the real filesystem path. They do not apply to files reached through a different path, such as a symlink.

readlink -f /var/www/site
grep -n "<Directory" /etc/apache2/sites-enabled/*.conf

2Fix

Write the Directory path as the real path that readlink -f printed.

<Directory "/srv/example/public">
    Require all granted
</Directory>

3Verify

Run the config test, reload and request the site again.

sudo apache2ctl configtest && sudo systemctl reload apache2

Confirmed by httpd.apache.org

Did this fix it?

Cause 3 of 3

The site still uses the old Order, Allow and Deny rules

1Check

Order, Allow and Deny come from mod_access_compat. They are deprecated and will be removed, and the documentation tells you to avoid guides that use them.

grep -rn -E 'Order |Allow from|Deny from' /etc/apache2/sites-enabled/

2Fix

Replace them with a Require rule.

# remove these
#   Order allow,deny
#   Allow from all
# use this
Require all granted

3Verify

Run the config test, reload and request the site.

sudo apache2ctl configtest && sudo systemctl reload apache2

Confirmed by httpd.apache.org

Did this fix it?

How do I stop it coming back?

  • Write a Directory block with Require for every DocumentRoot you create, and test it with apache2ctl configtest.
  • Use the real path in Directory blocks, not a symlinked one.
  • Do not copy Order, Allow or Deny lines from old guides.

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.