Vagrant 403 Forbidden

2019-03-25 16:00发布

问题:

I've been running Vagrant successfully for about a week. Last night I ran vagrant reload and now I can no longer access my sites.

  • VirtualBox version 4.2.16
  • Vagrant version 1.2.7
  • My Vagrantfile and bootstrap.sh: https://github.com/kriskd/vagrant-settings
  • Running on Mac

My files live at /vagrant/Sites. At first my "welcome page" which lives at /vagrant/Sites rendered at

http://localhost:4567/ 

All my projects are folders under Sites. For example, /vagrant/Sites/test won't render index.html. I get the following

Forbidden

You don't have permission to access / on this server. Apache/2.4.6 (Ubuntu) Server at localhost Port 4567

The vhost looks like:

<VirtualHost *:80>
  DocumentRoot "/vagrant/Sites/test"
  ServerName test
  <Directory "/vagrant/Sites/test">
    AllowOverride All
  </Directory>
</VirtualHost>

The vhosts are owned by root. My project files are owned by vagrant and chmod'ed 0777.

After no success, I did a full vagrant destroy followed by vagrant up and then the localhost host welcome page stopped rendering as well with the forbidden error.

回答1:

My hunch is that this is not a vagrant issue at all but solely an Apache configuration glitch. There are a few things I can think to check.

First, obviously, is to confirm that the user that apache is running under has read and execute permissions for the DocumentRoot folder.

Since you mentioned Apache 2.4, there have been changes in the configs from 2.2. Make sure your Allow from all statements now read Require all granted. (If you were still on 2.2, you'd want to make sure they said Allow from all instead of Deny from all.) In either case, you can set this in each <VirtualHost> individually, or set a default in your <Directory /> block of the main httpd.conf file.

Getting more obscure, you could check for selinux, although I'm pretty sure this isn't present in Ubuntu by default. (It is in CentOS, for example.)



回答2:

This is solved and in the end came down to some very simple things.

  • Use "Require All granted" instead of "Allow from All"
  • Put each websites' content at the same level namely /vagrant/Sites/default, /vagrant/Sites/test, /vagrant/Sites/real-site
  • Add .conf extension to vhost names such as test.conf and real-site.conf
  • Add AllowOverride All to vhosts to respect sites' .htaccess file (I realize that was in my original post, it got lost as I tried to solve this)

All very basic things that eluded me for a better part of a week. I hope this can help someone else.



回答3:

I had the same problem when changing the DocumentRoot.

Since you've changed your DocumentRoot to "/any/path/foo/bar", make sure you have the permissions set on "apache2.conf" for this path.

Search for:

<Directory /any/path/foo/bar>

in apache2.conf

And add a new block like this:

<Directory /any/path/foo/bar>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

And my guess is that everything is gonna be fine!



回答4:

cd /etc/apache2/sites-available
for file in `ls *`; do sed 's/\(.*<\/Directory.*>\)/Require\ all\ granted\n\1/' $file > $file.new;mv $file.new $file ; done;

because it worked before, I would not waste time on fix file by file