Monday, 8 April 2019

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?



from Vagrant virtual hosts/shared folders not accessible

No comments:

Post a Comment