Vagrant重启后Apache无法启动

14

我正在尝试使用Vagrant设置一个简单的开发环境。基础镜像(由我创建)带有CentOS 6.5 64位、Apache和MySQL。

问题是,当我重新加载虚拟机(vagrant reloadvagrant halt然后up)后,httpd服务不会在启动时自动启动。

该问题仅在运行更改DocumentRoot的provision脚本后第一次关闭机器后出现。

更多信息:

httpd在2、3、4和5级上都处于chkconfig状态。

错误日志(在/etc/httpd/logs中)没有写入任何错误信息。

如果我通过ssh手动启动服务,则可以正常启动。

我在其他CentOS盒子上也遇到了同样的问题(例如在vagrantcloud.com上可用的chef/centos-6.5),这就是为什么我创建了自己的盒子。

其他服务(如mysql)都能够正常启动,因此这是特定于apache的问题。

总结:

  • httpd始终会在第一次启动时启动,即使有provision脚本(比如在vagrant destroy之后)
  • 当我不运行provision脚本时,httpd始终会启动(但我需要它来设置DocumentRoot)
  • 在更改DocumentRoot的provision脚本后第一次关闭机器后,httpd将不会启动(不确定是否是该问题的原因)。

这是我的Vagrantfile:

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

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "centos64_lamp"
  config.vm.box_url = "<url>/centos64_lamp.box"
  config.vm.hostname = "machine.dev"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.synced_folder ".", "/vagrant", owner: "root", group: "root"
  config.vm.provision :shell, :path => "vagrant_files/bootstrap.sh"

end

我尝试创建所有者/组为rootapache的vagrant文件夹。与拥有者vagrant相同的问题。

这些是我尝试的配置脚本(bootstrap.sh)。唯一想要它们做的是将DocumentRoot更改为vagrant文件夹。但都没有起作用。

尝试1

#!/usr/bin/env bash

sudo rm -rf /var/www/html
sudo ln -fs /vagrant/app/webroot /var/www/html

尝试2

#!/usr/bin/env bash

sudo cp /vagrant/vagrant_files/httpd.conf /etc/httpd/conf
sudo service httpd restart

第二次尝试的httpd.conf与默认配置相同,除了DocumentRoot路径不同。这个第二个选择可以让我使用vagrant up --provision强制重启服务,但这应该是一个不必要的步骤。

还有什么其他方法可以尝试解决这个问题吗?谢谢。

4个回答

17

显然,问题是由于在Apache尝试启动时未装载vagrant文件夹所致。虽然我仍然不明白为什么没有错误被抛出。

我通过创建一个Upstart脚本(位于文件夹/etc/init中),以便在vagrant挂载其文件夹后启动服务(它会发出名为vagrant-mounted的事件)来解决它。

这是我使用的脚本(文件名为httpd.conf,但我认为这并非必需)。

# start apache on vagrant mounted

start on vagrant-mounted

exec sudo service httpd start

Upstart可以做更多的事情,但这个解决方案很好。


我放弃了使用 sudo,而是在基于Ubuntu的系统中使用 exec service apache2 restart - Erik

3

首先,检查是否应该启动特定运行级别(至少2-5级)的httpd(您已经执行了此操作):

chkconfig | grep httpd

在这种情况下,可能与您的DocumentRoot或其符号链接指向 Vagrant同步文件夹有关,因此在服务启动期间尚不可用。
解决方法是在您的shell配置脚本末尾添加service start httpd命令,例如:
service httpd status || service httpd start

为了修复它。
为了更加可靠的解决方案,将其添加到陷阱函数中(对于Bash脚本),例如:
trap onerror 1 2 3 15 ERR

#--- onerror()
onerror() {
  service httpd status || service httpd start
}

这可能还不够,为了使其在停止和启动情况下都能正常运行,您需要将您的 shell 作为 always 在您的 Vagrantfile 中运行,例如:

config.vm.provision :shell, run: "always", :inline => "service httpd status || service httpd start"

或提供一个脚本,例如:
config.vm.provision :shell, run: "always", path: "scripts/check_vm_services.sh"

然后脚本可能看起来像这样:
#!/usr/bin/env bash
# Script to re-check VM state.
# Run each time when vagrant command is invoked.

# Check if httpd service is running.
echo Checking services...
service httpd status || service httpd start

或者检查:在Vagrant挂载后启动服务,它使用upstart事件,每次Vagrant挂载名为vagrant-mounted的同步文件夹时都会发出信号,因此我们可以修改依赖于Vagrant同步文件夹的服务的upstart配置文件,以监听并在发出vagrant-mounted事件后启动、检查和重启服务。


0

我的nginx在Vagrant reload或Vagrant up时无法启动,这是我的解决方案:

sudo cat > /etc/init/vagrant-mounted.conf << EOL
# start services on vagrant mounted

start on vagrant-mounted

exec sudo service php5-fpm restart
exec sudo service mysql restart
exec sudo service memcached restart
exec sudo service nginx restart
exec sudo nginx
EOL

0

我确认上述的解决方案完全可行。 我添加了一个名为vagrant-mounted.conf的文件在/etc/init目录下,其内容如下:

start on vagrant-mounted
exec sudo sh /etc/startup.sh

我已经添加了shell脚本/etc/startup.sh,用于手动启动httpd、mysqld和sendmail,但需要通过vagrant ssh登录后才能通过vagrant up进行操作...现在它是自动的。太好了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接