1. 安装 VirtualBox
虚拟机还是得依靠 VirtualBox 来搭建,免费小巧
下载地址:https://www.virtualbox.org/wiki/Downloads
* 虽然 Vagrant 也支持 VMware,不过 VMware 是收费的,对应的 Vagrant 版本也是收费的。
我下载的是:VirtualBox 5.1.6 for Windows hosts x86/amd64
2. 下载 Vagrant
下载地址:http://downloads.vagrantup.com/
3.下载contos box,可以来这里下载:http://www.vagrantbox.es/
经过上面的下载,我们
下载了virtual box vagrant centos 6.6 box 三个文件
4.安装 virtualbox , vagrant ,这个基本都是下一步,安装完成后要重启
安装上面的两个成功后,重启后。
4.1
window建+r ,打开命令行,
进入命令行模式,输入vagrant,看看是否安装成功
4.2 添加centos box
进入d盘,添加centos box
进入contos box文件所在的文件夹,我的是在d:\vagrant文件路径下,按照上面截图的命令行进入d盘,在通过cd vagrant 进入相应文件夹。
D:\vagrant\centos-6.6-x86_64.box
按照这个命令添加一个box
vagrant box add 名称 路径
譬如:
D:\vagrant\vagrant box add centos-6.6-x86_64 centos-6.6-x86_64.box
这里将 box 名称:centos-6.6-x86_64 对应 D:\vagrant\vagrant\centos-6.6-x86_64.box这个文件,后面可以使用centos-6.6-x86_64,使用后直接对应上面对应的文件。
通过vagrant box list 查看添加的列表
4.3创建虚拟机:
添加了 Box 以后,我们就可以用 Vagrant 基于这个 Box 去创建虚拟机了。先找个地方去创建一个目录,这个目录就是你的项目所在的目录,它会自动跟虚拟机上的某个目录同步,也就是在你电脑上的这个目录里面的文 件,你同样可以在虚拟机里的某个目录里面找到。比如我的目录在d:\myvagrant,我创建这个目录,然后再进入到这个目录,在命令行工具下面执行:
初始化:
启动 vagrant up命令,第一次会慢一些,因为要复制文件。
在上面,发现了拨错:
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.
If you look above, you should be able to see the error(s) that Vagrant had when attempting to connect to the machine. These errors are usually good hints as to what may be wrong.
If you're using a custom box, make sure that networking is properly working and you're able to connect to the machine. It is a common problem that networking isn't setup properly in these boxes. Verify that authentication configurations are also setup properly,as well.
If the box appears to be booting properly, you may want to increase the timeout ("config.vm.boot_timeout") value.
打开文件:D:\myvagrant\Vagrantfile
将Vagrantfile配置文件中vb.gui = true的注释去掉,下面将三行的代码的注释去掉了,也就是前面的 # 号,而不是一行,这里要注意,如果仅仅去掉vb.gui = true的注释,会报错。
config.vm.provider "virtualbox" do |vb|
# # Don't boot with headless mode
vb.gui = true
#
# # Use VBoxManage to customize the VM. For example to change memory:
# vb.customize ["modifyvm", :id, "--memory", "1024"]
end
然后关闭 vagrant halt ,重启,查看报错。
运行vagrant up 启动 virtualbox 后,GUI会给出提示:
VT-x/AMD-V硬件加速在您的系统中不可用。您的64-位虚拟机将无法检测到 64-位处理器,从而无法启动。
这是由于在BOIS中没有开启cpu虚拟化支持,重启F2或F10等进入BIOS设置Virtualization为Enable(我的Thinkpad是Security=>Virtualizatio设置为Enable);
电脑重启后,再次vagrant up启动虚拟机还是有一些问题,当时也没有记录下来错误信息,只记得解决方案是使用vagrant destroy将虚拟机从磁盘中删除,然后使用vagrant up命令重新创建。
重启后,换一个文件路径,我原来的是d:\myvagrant,我换到了另外一个路径d:\disk\va1
上面出现waining,是没有问题的,现在我们可以通过ssh直连了。
查看vagrant的状态 vagrant status
关闭VM vagrant halt
在启动 vagrant up, 第二次启动会比较快,因为不会复制文件,但是还是很慢,哈哈,多等下就好了。
ssh 连接
ip:127.0.0.1
端口:2222
用户名:vagrant
密码:vagrant
root的密码也是vagrant ,上面连接一定要注意,更改ssh的默认端口22 为 2222
ssh 登录后,我发现语言是德语的,日,使用root账号登录,更改语言
vim /etc/sysconfig/i18n
语言改成英语
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
重启一下linux
查看centos 版本:
[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.6 (Final)
[root@localhost ~]#
到此为止,初始环境搭配好了,下面需要安装 lnmp环境了。
具体参看下一节
vagrant 配置
Vagrant 初始化成功后,会在初始化的目录里生成一个 Vagrantfile 的配置文件,可以修改配置文件进行个性化的定制。
Vagrant 默认是使用端口映射方式将虚拟机的端口映射本地从而实现类似 http://localhost:80 这种访问方式,这种方式比较麻烦,新开和修改端口的时候都得编辑。
(
相比较而言,host-only 模式显得方便多了。打开 Vagrantfile,将下面这行的注释去掉(移除 #)并保存:
config.vm.network :private_network, ip: "192.168.33.10"
重启虚拟机,这样我们就能用 192.168.33.10 访问这台机器了,你可以把 IP 改成其他地址,只要不产生冲突就行。
)
注意:对于端口映射一定要配置,譬如:
config.vm.network “forwarded_port”, guest: 80, host: 80
不然,在开启nginx后,通过127.0.0.1是无法访问的,这个一定不要忘记
我暂时采用的是单个匹配的方式:如下:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "centos6.6"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 80, host: 80
config.vm.network "forwarded_port", guest: 2001, host: 2001
config.vm.network "forwarded_port", guest: 2030, host: 2030
config.vm.network "forwarded_port", guest: 2030, host: 2030
config.vm.network "forwarded_port", guest: 2020, host: 2020
config.vm.network "forwarded_port", guest: 2010, host: 2010
config.vm.network "forwarded_port", guest: 2000, host: 2000
config.vm.network "forwarded_port", guest: 1000, host: 1000
config.vm.network "forwarded_port", guest: 1010, host: 1010
#config.vm.network :private_network, ip: "192.168.111.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
6. 打包分发
当你配置好开发环境后,退出并关闭虚拟机。在终端里对开发环境进行打包:
vagrant package
打包完成后会在当前目录生成一个 package.box 的文件,将这个文件传给其他用户,其他用户只要添加这个 box 并用其初始化自己的开发目录就能得到一个一模一样的开发环境了。
这个box文件,如果给了其他人用,先通过下面的命令行添加
vagrant box add fecshop fecshop.box
然后,和上面一样,找一个新的文件夹,
vagrant init fecshop
执行成功后,启动
vagrant up
另外需要注意的是,需要把 Vagrantfile 这个里面的配置复制到新的地方,配置一致才可以。这样就完成了分发。
7. 常用命令
vagrant init # 初始化
vagrant up # 启动虚拟机
vagrant halt # 关闭虚拟机
vagrant reload # 重启虚拟机
vagrant ssh # SSH 至虚拟机
vagrant status # 查看虚拟机运行状态
vagrant destroy # 销毁当前虚拟机
更多内容请查阅官方文档 http://docs.vagrantup.com/v2/cli/index.html
8. 注意事项
使用 Apache/Nginx 时会出现诸如图片修改后但页面刷新仍然是旧文件的情况,是由于静态文件缓存造成的。需要对虚拟机里的 Apache/Nginx 配置文件进行修改:
# Apache 配置添加:
EnableSendfile off
# Nginx 配置添加:
sendfile off;
其他问题:
问题2: vagrant启动报错The following SSH command responded with a non-zero exit status.
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
ARPCHECK=no /sbin/ifup eth1 2> /dev/null
Stdout from the command:
Device eth1 does not seem to be present, delaying initialization.
Stderr from the command:
解决方案
虽然vagrant up启动报错,但是vagrant ssh还是能登陆虚拟机的,进入虚拟机后,执行如下命令
sudo rm -f /etc/udev/rules.d/70-persistent-net.rules
对, 问题就处在在持久网络设备udev规则(persistent network device udev rules)是被原VM设置好的,再用box生成新VM时,这些rules需要被更新。而这和Vagrantfile里对新VM设置private network的指令发生冲突。删除就好了。
vagrant reload 再次启动就OK。
问题3:在配置好vagrant和虚拟机中的nginx后,当你通过宿主机访问虚拟机中的web站点时,出现页面一直在加载,无法正常显示页面
(此块由程序员 Young 614168741 添加)
解决方法:因为centos7.0(含7.0)及以上版本会默认安装firewalld防火墙,firewalld有zone的概念默认在public区,只能指定外部连接进入,首先systemctl status firewalld 查看防火墙状态,active:running表示开启转态,然后我们firewall-cmd –zone=public –add-port=80/tcp –permanent,或者firewall-cmd –zone=public –add-port=8080/tcp –permanent(取决于你的Vagrantfile端口端口那一个是映射到虚拟机的80),执行完,firewall-cmd –zone=public –list-ports检查下public区上的端口,出现刚才添加的端口说明成功,再firewall-cmd –reload重启下firewalld,这个时候通过宿主机通过制定的配置的端口访问虚拟机站点应该就可以了