Vagrant - 启动时启动apache

时间:2013-12-27 14:19:54

标签: apache vagrant

我刚刚创建了一个新的盒子(基于ubuntu precise32),我自己的软件可以安装,所有的东西都非常好用,不会在启动时启动apache。

vagrant up之后我必须登录我的方框并每次都启动它。

我试图让它在启动时启动:

sudo update-rc.d apache2 defaults

但我明白了:

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

所以它应该开始...... 这是我的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

这就是apache安装:

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'],
    }
}

这是vagrant u p和(手动)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'

我错过了什么?

4 个答案:

答案 0 :(得分:6)

我期望您的apache配置以某种方式引用vagrant安装的文件夹中的文件(在/vagrant dir中或config.vm.synced_folder定义的文件中)。在启动时,这些文件夹尚不可访问,因为vagrant在引导机器之后安装它们。

我在这里问了类似的问题:Apache fails to start on Vagrant。如果是这样,那么我可能会在那里得到答案。

答案 1 :(得分:1)

首先:

sudo update-rc.d -f apache2 remove
sudo update-rc.d apache2 defaults
正如cocheese所指出的那样。

检查你的运行级别:

runlevel

它应该是“N 2”。因此,请检查符号链接是否正确:

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

如果您的输出与此类似:

lrwxrwxrwx 1 root root 17 Sep 18 22:34 S18apache2 -> ../init.d/apache2
你很好。 “S”很重要,系统中“18”可以更低或更高,具体取决于系统依赖性。

如果你看到你的apache2-init脚本(用你的替换“18”):

cat /etc/rc2.d/S18apache2

你的rc.d很好。

如果你重新启动你的虚拟机(例如通过'vagrant halt'和'vagrant up')并且你的apache2仍未启动并且正在运行,那么你在启动时就会出错。

只需将此添加到您的vagrant文​​件中即可离开无头模式并查看gui:

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

或者在vm启动时安装bootlogd以将服务消息记录到/ var / log / boot。

apt-get install bootlogd

使用:

cat /var/log/boot 

你可能会看到你的apache2失败了:

[...]
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.
[...]

您可以按照Ruslan Bes建议解决该问题(链接在他的帖子中)。

症状修复就像在你的vagrantfile中一样:

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

或者让你的vm重新参与vagrant mount事件: a)使用upstart包装器脚本 b)使用udev规则

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

或者您将vhost更改为在进入运行级别2时存在的目录。并在您的vm中将/ vagrant作为内联shell安装到该dir上。

答案 2 :(得分:0)

也许这会有所帮助

  1. sudo update-rc.d -f apache2 remove
  2. sudo update-rc.d apache2 defaults
  3. 然后重新启动

答案 3 :(得分:0)

只需在要挂载的文件夹中创建文件index.html

file { "/var/www/symfony/index.html":
    source => "/vagrant/provision/templates/index.html",
    owner => "www-data", group => "www-data",
    backup => true,
}

此处/var/www/symfony/将使用/vagrant

中的文件夹进行装载