DEV Community

Seong Bae
Seong Bae

Posted on

How I fixed 403 error in Laravel

Recently, I had to set up a development environment on a new computer running on Ubuntu 24. I've done this thousand times and the business went as usual - installing LAMP, git, composer, etc. Then I downloaded an existing Laravel project from github to continue working on it. After setting up the project, when I went to url in browser, I saw "403 forbidden access" error. I've tried everything but what fixed the issue at was definitely not what I expected!

First, I double-checked my apache configuration file for any errors or wrong directives used:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName mysite.test
    DocumentRoot /home/seongbae/projects/mysite/public

    <Directory "/home/seongbae/projects/mysite/public">
        AllowOverride All
        Require all granted
        Options FollowSymLinks
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enter fullscreen mode Exit fullscreen mode

Everything looked good. All my other Laravel projects had similar apache configuration settings.

Next, I was curious if this has anything to do with apache rewrite module not being enabled. By default, on a fresh install of LAMP on Ubuntu, the rewrite module was not enabled. So I enabled it as following:

cd /etc/apache2/mods-available
a2enmod rewrite
service apache2 restart
Enter fullscreen mode Exit fullscreen mode

That did not fix either.

Someone mentioned that the project should have .htaccess file that looks like following:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Enter fullscreen mode Exit fullscreen mode

That didn't fix.

Then I thought this has to be some type of permission issue...most likely the www-data user is not able to access the project folder which is located at /home/seongbae/projects folder. To test this theory, I created a Laravel project under apache root folder at /var/www/html. It worked! So I narrowed down the issue to incorrect permission setup.

So the next step I tried was following:

cd /home/seongbae/projects/mysite
sudo chown -R $USER:www-data .
sudo find . -type f -exec chmod 664 {} \;   
sudo find . -type d -exec chmod 775 {} \;
sudo chmod -R ug+rwx storage bootstrap/cache
Enter fullscreen mode Exit fullscreen mode

Above basically gives www-data access to the site folder and changes permission for all folders and files so that www-data can access them. I also made sure that www-data can write to the storage and cache folders.

When the above didn't fix the issue, I was starting to pull my hair. I know I've set up new installation of Ubuntu thousands of times before and I've come across this issue. I've fixed it in the past... but I just could not remember how I fixed it. I must be getting old. Then I thought that if I ever fix this again, I must write a blog post for my future self.

Then I vaguely remembered that www-data must have access to parent folders as well.

/home/seongbae/projects/mysite
/home/seongbae/projects/
/home/seongbae
Enter fullscreen mode Exit fullscreen mode

So one at a time, I changed the group for each of above parent folder:

sudo chgrp www-data folder
Enter fullscreen mode Exit fullscreen mode

And when I changed group for all the above 3 parent folders, the 403 forbidden error went away!

I don't know why all the discussions and solutions I found on the net through Google did not address fixing the permission of parent folders. Hopefully this will help someone....and also future me from wasting too much time trying to fix the forbidden issue.

Top comments (0)