Apache returns 403 Forbidden for a new VirtualHost
403 ForbiddenShort 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.
Fix it fast
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 -S2Match what you see, then jump to its fix
- 1
The DocumentRoot is outside /var/www, and the site file has no Directory block for itNo Directory block grants access to the DocumentRootFix - 2
The Directory block exists but its path goes through a symbolic linkThe Directory path is a symlink, not the real pathFix - 3
The 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
- 1
What does the error look like?
403 ForbiddenYou 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.conf2Fix
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
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/*.conf2Fix
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 apache2Confirmed by httpd.apache.org
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 granted3Verify
Run the config test, reload and request the site.
sudo apache2ctl configtest && sudo systemctl reload apache2Confirmed by httpd.apache.org
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 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 on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
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 fix