Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant - start apache at boot

Tags:

vagrant

apache

I have just create a new box (based on ubuntu precise32) with my own software to install and everything works great eccept apache that do not start at boot.

After the vagrant up I have to log into my box and start it every time.

I have tried to make it start at boot with:

sudo update-rc.d apache2 defaults

but I get:

System start/stop links for /etc/init.d/apache2 already exist.

So it should start... This is my Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
    config.vm.define :stilogo do |stilogo_config|
        stilogo_config.vm.box = "precise32"
        stilogo_config.vm.box_url = "http://files.vagrantup.com/precise32.box"
        stilogo_config.ssh.forward_agent = true

        #Do alla macchina un IP statico
        #stilogo_config.vm.network :private_network, ip: "192.168.56.101"

        stilogo_config.vm.network :forwarded_port, guest: 80, host: 8888, auto_correct: true
        stilogo_config.vm.network :forwarded_port, guest: 3306, host: 8889, auto_correct: true
        stilogo_config.vm.network :forwarded_port, guest: 5432, host: 5433, auto_correct: true
        stilogo_config.vm.hostname = "stilogo"
        stilogo_config.vm.synced_folder "www", "/var/www", {:mount_options => ['dmode=777','fmode=777']}
        stilogo_config.vm.provision :shell, :inline => "echo \"Europe/Rome\" | sudo tee /etc/timezone && dpkg-reconfigure --frontend noninteractive tzdata"

        stilogo_config.vm.provider :virtualbox do |v|
            v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
            v.customize ["modifyvm", :id, "--memory", "512"]
        end

        #Installo il software che mi serve
        stilogo_config.vm.provision :puppet do |puppet|
            puppet.manifests_path = "puppet/manifests"
            puppet.manifest_file = "phpbase.pp"
            puppet.module_path = "puppet/modules"
            #puppet.options = "--verbose --debug"
        end
    end
end

and this the apache installation:

class apache 
{      
    package 
    { 
        "apache2":
            ensure  => present,
            require => [Exec['apt-get update'], Package['php5'], Package['php5-dev'], Package['php5-cli']]
    }

    service 
    { 
        "apache2":
            ensure      => running,
            enable      => true,
            require     => Package['apache2'],
            subscribe   => [
                File["/etc/apache2/mods-enabled/rewrite.load"],
                File["/etc/apache2/sites-available/000-default.conf"],
                File["/etc/apache2/conf-enabled/phpmyadmin.conf"]
            ],
    }

    file 
    { 
        "/etc/apache2/mods-enabled/rewrite.load":
            ensure  => link,
            target  => "/etc/apache2/mods-available/rewrite.load",
            require => Package['apache2'],
    }

    file 
    { 
        "/etc/apache2/sites-available/000-default.conf":
            ensure  => present,
            owner => root, group => root,
            source  => "/vagrant/puppet/templates/vhost",
            require => Package['apache2'],
    }

    exec 
    { 
        'echo "ServerName localhost" | sudo tee /etc/apache2/conf-enabled/fqdn.conf':
            require => Package['apache2'],
    }
}

This is the log after the vagrant up and the (manual) apache start

[Fri Dec 27 15:26:33.220448 2013] [mpm_prefork:notice] [pid 1379] AH00169: caught SIGTERM, shutting down
PHP Warning:  Module 'memcached' already loaded in Unknown on line 0
[Fri Dec 27 15:27:55.750639 2013] [mpm_prefork:notice] [pid 1105] AH00163: Apache/2.4.6 (Ubuntu) PHP/5.5.7-1+sury.org~precise+1 configured -- resuming normal operations
[Fri Dec 27 15:27:55.750700 2013] [core:notice] [pid 1105] AH00094: Command line: '/usr/sbin/apache2'

what am I missing?

like image 704
Christian Giupponi Avatar asked Dec 27 '13 14:12

Christian Giupponi


2 Answers

What I expect is that your apache config somehow references the files in the folder mounted by vagrant (either in /vagrant dir or those defined by config.vm.synced_folder). At the startup those folders are not yet accessible because vagrant mounts them after booting machine.

I asked the similar question here: Apache fails to start on Vagrant . If that's the case, then maybe I got an answer there.

like image 121
Ruslan Bes Avatar answered Sep 20 '22 02:09

Ruslan Bes


First do:

sudo update-rc.d -f apache2 remove
sudo update-rc.d apache2 defaults

as pointed out by cocheese.

Check your runlevel:

runlevel

it should be "N 2". So check if a symlink is correct:

cd /etc/rc2.d/ && ls -al *apache2*

If your output is similar to this:

lrwxrwxrwx 1 root root 17 Sep 18 22:34 S18apache2 -> ../init.d/apache2

you are fine. "S" is important and "18" can be lower or higher on your system, depending on system dependencies.

If you see your apache2-init script by hitting this (replace "18" with yours):

cat /etc/rc2.d/S18apache2

your rc.d is fine.

If you restart your vm (e.g. by ´vagrant halt´ and ´vagrant up´) and your apache2 is still not up and running you do have a error while booting.

Just add this to your vagrantfile to leave headless mode and see the gui:

config.vm.provider :virtualbox do |vb|
  vb.gui = true
end

Or install bootlogd to log the service messages to /var/log/boot while your vm is booting.

apt-get install bootlogd

With a:

cat /var/log/boot 

you may see your apache2 is failing like so:

[...]
Thu Sep 18 23:16:12 2014: [....] Starting web server: apache2Warning: DocumentRoot [/vagrant/public] does not exist
Thu Sep 18 23:16:12 2014: Action 'start' failed.
Thu Sep 18 23:16:12 2014: The Apache error log may have more information.
[...]

You can follow Ruslan Bes recommandation to solve that issue (link in his post).

Symptom fixing like so in your vagrantfile:

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

Or let your vm re-act on the vagrant mount event: a) using upstart wrapper-script b) using a udev rule

http://razius.com/articles/launching-services-after-vagrant-mount/

Or you change your vhost to a dir that exists when entering runlevel 2. And mount /vagrant to that dir on your vm as a inline shell in your vagrantfile.

like image 34
DanielaWaranie Avatar answered Sep 18 '22 02:09

DanielaWaranie