Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache fails to start on Vagrant

In my Vagrant environment I have a guest Ubuntu Virtualbox with a LAMP with default settings.

I have my source code on the host machine in the same folder as my Vagrantfile. So on the guest Ubuntu I can access the files in the mounted /vagrant dir like this

/vagrant
  /mysite
    /index.php
  /Vagrantfile

Now in my Apache config I add a line

Alias /mysite /vagrant/mysite

After reloading config and restarting apache I can go to localhost:8558/mysite/index.php and it works.

The problem is that when I reload Virtualbox with vagrant reload it starts Apache service before mounting the /vagrant folder. So Apache can't find the aliased dir and fails to start. i have to start it manually then

My question is - is there a way to delay Apache start so that it starts after the mounting?

Update: As a workaround I added script to the crontab that starts apache 30 seconds after the boot as described here. But I wonder if there is a better solution.

like image 346
Ruslan Bes Avatar asked Feb 06 '14 10:02

Ruslan Bes


3 Answers

while upstart probably is a valid option, I had several issues using it with vagrant. I had to run several tasks that needed to be run as a privileged user, which I did not manage to get working with upstart.

Starting from version 1.6.0 (May 6, 2014), vagrant provides the option to run a specific provisioner every time, so also after booting a halted VM with vagrant up.

In your Vagrantfile, add:

# a file, eg after-boot.sh
config.vm.provision "shell", path: "after-boot.sh", run: "always"
# or just inline
config.vm.provision "shell", inline: "service apache2 restart", run: "always"

note the run: "always", this will force vagrant to run the provisioner always, obviously it works just as well with any other provisioning system like chef or puppet.

like image 178
Zauberfisch Avatar answered Oct 08 '22 21:10

Zauberfisch


I would like to add a little to Zauberfisch's answer (Apache fails to start on Vagrant)

What needed to happen was this command needed to be run as a superuser AKA 'Sudo' so this was the command that was needed:

`config.vm.provision "shell", inline: "sudo service apache2 restart", run: "always"`

The reason why this didn't work for you without the sudo appears to be that Vagrant tries to run the command without /usr/sbin in PATH. For me, this worked just as well:

`config.vm.provision "shell", inline: "/usr/sbin/service apache2 restart", run: "always"`
like image 26
Sweet Chilly Philly Avatar answered Oct 08 '22 23:10

Sweet Chilly Philly


If upstart is installed (as in Ubuntu), Vagrant emits "vagrant-mounted" event. See https://serverfault.com/a/568033/179583 to get the idea. In your script you can (re)start the Apache server.

Btw, I have a feeling that newer Apache versions just warn, but still start even if the doc root doesn't exist. The same with nginx.

like image 23
tmatilai Avatar answered Oct 08 '22 22:10

tmatilai