问题描述
我正在尝试使用 Vagrant 建立一个简单的开发环境.基本盒子(我创建的)有 CentOS 6.5 64bit 和 Apache 和 MySQL.
I'm trying to set up a simple dev environment with Vagrant. The base box (that I created) has CentOS 6.5 64bit with Apache and MySQL.
问题是,在我重新加载 VM 后,httpd 服务没有在启动时启动(vagrant reload
或 vagrant halt
然后 up
).
The issue is, the httpd service doesn't start on boot after I reload the VM (vagrant reload
or vagrant halt
then up
).
仅当我运行一个更改 DocumentRoot
的配置脚本并且仅在我第一次停止机器之后才会出现此问题.
The problem only occurs when I run a provision script that alters the DocumentRoot
and only after the first time I halt the machine.
httpd 在第 2、3、4 和 5 级的 chkconfig
上
httpd is on chkconfig
on levels 2, 3, 4 and 5
没有错误写入error_log(在/etc/httpd/logs
上).
There are no errors written to the error_log (on /etc/httpd/logs
).
如果我 ssh 进入机器并手动启动服务,它启动没有问题.
If I ssh into the machine and start the service manually, it starts with no problem.
我在使用其他 CentOS 机器时遇到了同样的问题(例如 vagrantcloud.com 上的 chef/centos-6.5
),这就是我自己创建一个的原因.
I had the same issue with other CentOS boxes (like the chef/centos-6.5
available on vagrantcloud.com), that's why I created one myself.
其他服务,如mysql,启动正常,所以这是apache特有的问题.
Other services, like mysql, start fine, so it's a problem specific to apache.
- httpd 总是在第一次启动时启动,即使使用配置脚本(比如在 vagrant destroy 之后)
- httpd 总是在我不运行配置脚本时启动(但我需要它来设置 DocumentRoot)
- httpd 在第一次停止后没有启动,配置脚本与 DocumentRoot 混淆(不确定这是否是问题所在).
这是我的 Vagrantfile:
This is my 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
我尝试使用所有者/组 root
和 apache
创建 vagrant 文件夹.两者都有同样的问题(与所有者 vagrant
一样).
I tried to create the vagrant folder with owner/group root
and apache
. Same problem with both (as with owner vagrant
).
这些是我尝试过的配置脚本 (bootstrap.sh).我希望他们做的唯一一件事就是将 DocumentRoot 更改为 vagrant 文件夹.都没有用.
These are the provision scripts (bootstrap.sh) that I tried. The only thing that I want them to do is to change the DocumentRoot to the vagrant folder. Neither worked.
试试 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
来强制重启服务,但这应该是不必要的步骤.
The httpd.conf on the second try is equal to the default one, except for the DocumentRoot path. This second alternative allows me to do vagrant up --provision
to force the restart of the service, but that should be an unnecessary step.
我还能尝试什么来解决这个问题?谢谢.
What else can I try to solve this? Thank you.
推荐答案
显然问题是由于 Apache 尝试启动时没有挂载 vagrant 文件夹.虽然我还是不明白为什么没有报错.
Apparently the problem was due to the vagrant folder not being mounted when Apache tries to start. Although I still don't understand why no error is thrown.
我通过创建一个 Upstart 脚本(在文件夹 /etc/init
上)解决了这个问题,以便在 vagrant 挂载其文件夹后启动服务(它发出一个名为 vagrant-mounted代码>)
I solved it by creating an Upstart script (on the folder /etc/init
) to start the service after vagrant mounts its folder (it emits an event called vagrant-mounted
)
这是我使用的脚本(文件名 httpd.conf
但我认为没有必要).
This is the script I used (with the filename httpd.conf
but I don't think that's necessary).
# start apache on vagrant mounted
start on vagrant-mounted
exec sudo service httpd start
Upstart 可以做得更多,但这可以解决问题.
Upstart can do much more but this solves it.
这篇关于Vagrant 重新加载后 Apache 不启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!