Introduction
Apache is an open-source web server known for its reliability, stability, and ease of use. Despite the simplicity of Apache's setup procedure, the users may encounter errors such as "403 Forbidden", which usually point to server misconfiguration.
This tutorial lists potential causes of the Apache "403 Forbidden" error and provides troubleshooting steps to fix it on your system.
Prerequisites
- Command-line access.
- A user account with sudo privileges.
- Apache web server installed.
What Is Apache 403 Forbidden Error?
The Apache "403 Forbidden" error appears when a user tries to load a web page with restricted access. Different browsers and websites display the "403 Forbidden" error message in different ways, including the following:
- 403 Forbidden
- Forbidden
- Error 403
- HTTP Error 403.14 - Forbidden
- HTTP 403
- Forbidden: You don't have permission to access the site using this server
- Error 403 - Forbidden
- HTTP Error 403 - Forbidden
Below is an example of the "403 Forbidden" error in Firefox:
What Causes Apache 403 Forbidden Error?
There are several potential reasons why the Apache "Forbidden 403" error prevents users from accessing website files:
- A permission error in the webroot directory.
- Wrong webroot directory ownership.
- Misconfigured or missing directives in the Apache configuration files.
- Default directory index issues.
- Problems with the .htaccess file.
How to Fix Apache 403 Forbidden Error?
Since multiple reasons trigger the "403 Forbidden" error, the best approach is to eliminate them one by one. The following sections present five methods to troubleshoot and fix the Apache "403 Forbidden" error.
Method 1: Set Correct File Permissions
Incorrect file permissions may prevent Apache from serving the web page and cause the "403 Forbidden" error. Execute the following commands to ensure the correct permissions for the webroot directory and files:
1. Enforce the correct directory permissions:
sudo find [path-to-webroot] -type d -exec chmod 2750 {} \+
For example, to set the permissions to the default webroot location, /var/www, enter the find command below:
sudo find /var/www -type d -exec chmod 2750 {} \+
2. Use the find
command again to adjust file permissions:
sudo find /var/www -type f -exec chmod 640 {} \+
The chmod command sets the execute permission for the webroot directory and read permission for the index file, e.g., index.html.
Method 2: Change Directory Ownership
If the first method has not resolved the error, try changing the directory ownership with the command below:
sudo chown -R [user]:[group] [path-to-webroot]
Replace [user] with a user account with root privileges on the web server and [group] with www-data or apache, depending on which group exists on the system.
Restart the Apache web server so the changes take effect:
sudo systemctl restart apache2
Note: On Rocky Linux, Almalinux, and other RHEL-based distributions, the Apache package, the service, and the related configuration directories and files are called httpd. For this tutorial to work on these distros, replace every mention of apache2 with httpd.
Method 3: Configure Apache Directives
If setting up the correct permissions and ownership has not eliminated the error, check the require directive in the main Apache configuration file. If incorrectly set, the directive may prevent Apache from serving the web pages.
Proceed with the steps below to perform the necessary edits to the configuration file:
1. Open the configuration file with:
sudo nano /etc/apache2/apache2.conf
2. Find the section with directives that configure server directories.
3. If the final line in the <Directory /var/www/>
section contains the Require all denied directive, change it to Require all granted.
4. Save the file and exit.
5. Restart the Apache web server for the changes to take effect:
sudo systemctl restart apache2
Note: If you have difficulties restarting the Apache service, read How to Restart Apache on Ubuntu.
Method 4: Add Default Directory Index
When a user visits a URL that requests a directory, the web server searches the directory for the index file. If the file or similar files are not present and directory index listings are disabled, the web server displays the "403 Forbidden" error message.
To fix the issue, add a default directory index by following the steps below:
1. Access the website's main configuration file by using:
sudo nano /etc/apache2/sites-available/[domain].conf
For example, to see the configuration for the test.local domain, type:
sudo nano /etc/apache2/sites-available/test.local.conf
Note: While the website's configuration file is usually named after the website's domain, this is not a hard rule. If you cannot find the CONF file with the domain name, go through the other files in the sites-available directory and locate the correct one for the website.
2. Find the DirectoryIndex directive and ensure that the index file in the website's webroot directory is on the list. If the file is not added to the directive, add it by using the following syntax:
DirectoryIndex [primary-index-file] [secondary-index-file] [...]
Note: If the directive does not exist, you can add it using the above syntax.
3. Save the file and exit.
4. Restart Apache:
sudo systemctl restart apache2
Method 5: Check .htaccess File
If the web server configuration contains a .htaccess file, some of the file's directives may be causing the "403 Forbidden" error. Since the content of a .htaccess file largely depends on the specific website, it is difficult to provide a single way to troubleshoot the reason for the error.
However, try to identify the problem by using the procedure described below:
1. Open the file and comment out all the lines by adding the (#) symbol at the beginning of each line.
2. Save the file and exit.
3. Restart Apache:
sudo systemctl restart apache2
4. Check if the error has disappeared.
5. If the error is gone, open the .htaccess file again and start uncommenting lines one by one, testing the website each time to see when the error returns.
Once you identify the problematic directive, fix it or remove it from the file.
Conclusion
After following this tutorial, you should be able to determine the cause and fix the Apache "403 Forbidden" error. The article listed five troubleshooting methods involving system permissions, directory ownership, and Apache configuration directives.
To learn more about the "403 Forbidden" error, read 403 Forbidden Error - What Is It and How to Fix It.