Cláudio Ribeiro
Cláudio Ribeiro

Reputation: 1699

Vagrant virtual hosts/shared folders not accessible

I currently have a vagrant box with CentOS 7. In my Vagrantfile I have the following configurations:

config.vm.box = "centos/7"
config.vm.provision :shell, path: "provision.sh"
config.vm.network "private_network", ip: "192.168.50.4"
# config.vm.synced_folder ".", "/vagrant"

I know that by default, vagrant shares the contents of the folder that contains the Vagrantfile. Those can be reached on the /vagrant folder inside the VM.

The code I want to reach is inside the same folder of the same folder as the Vagrantfile. I can reach it inside the VM on /vagrant/api/.

My goal is to be able to reach the index of the api inside my machine. I'm trying to create a virtual host for this effect.

On my provision file I have the following:

if [ $(grep -c 'api' /etc/httpd/conf/httpd.conf) -eq 0 ]; then
cat >> /etc/httpd/conf/httpd.conf <<EOM
<VirtualHost *:8081>    
    DocumentRoot "/vagrant/api/public"
    <Directory "/vagrant/api/public">
        Options +Indexes +FollowSymLinks
        DirectoryIndex index.php
        Order allow,deny
        Allow from all
        AllowOverride All
        Require all granted

        Header set Access-Control-Allow-Origin "*"
        Header always set Access-Control-Allow-Headers "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding"
        Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT, UPDATE"
        Header merge  Vary "Origin"
    </Directory> 

    ServerName vagrant.api.local:8081
    ServerAlias vagrant.api.local
    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</VirtualHost>
EOM
fi

service httpd restart;

And I added 192.168.50.4 vagrant.api.local to both /etc/hosts file (on my machine and on the VM).

Yet, when I try to access vagrant.api.local:8081 on the browser I get This site can’t be reached. vagrant.api.local refused to connect.

I can ping this url and get positive results, 0% packet loss.

Any idea on how can I load the /vagrant/api/public/index.php file on this url? What am I doing wrong?

Upvotes: 2

Views: 364

Answers (1)

Frederic Henri
Frederic Henri

Reputation: 53793

I will answer on this

My goal is to be able to reach the index of the api inside my machine

so you're correct by default, vagrant shares the content of your local folder containing the Vagrantfile with the /vagrant folder of the VM.

One thing though is that box an override the configuration of the Vagrantfile, and its the case of the centos/7 box.

If you look in your $HOME/.vagrant.d/boxes/centos-VAGRANTSLASH-7/<box_version>/virtualbox you will find the box Vagrantfile which declares

Vagrant.configure("2") do |config|
  config.vm.base_mac = "525400261060"
  config.vm.synced_folder ".", "/vagrant", type: "rsync"
end

As the folder is of type rsync its only one-time one-way sync from the machine running to the machine being started by Vagrant.

The easiest for you is to remove the rsync folder type and use default virtualbox sync folder mechanism which will provide (near) real-time bi-directional synchronization.

Upvotes: 2

Related Questions