想学习下laravel才了解到这个,所以着手研究了一下。
电脑是wind7 64位系统(最好安装git,在git bash下面执行下面的命令)。
1.准备安装visual box和vagrant软件(建议下载最新版本的,因为一般刚下载的都是新的,如果你的版本过久可能会出现问题)
准备好了就开始吧!
vagrant部分
- 方案1
$ vagrant.exe box add laravel/homestead
执行上面的命令,一般国内有墙导致下载巨慢,估计要几天的节奏就算了(我这里没墙都下载了几个小时,其实也才不到1G),网一般的话就放弃方案1吧。
我是使用的方案1,下面会和方案2进行比对。
$ vagrant.exe box list laravel/homestead (virtualbox, 2.0.0)
下载完了,看下list,看到下载到了最新版的。
- 方案2
去网上找到这个.box文件,去这里(https://atlas.hashicorp.com/boxes/search)搜索你要的。找到后,去问万能的网友找资源即可。
下载好了之后文件最好重命名一下,我的是homestead.box。有了该文件可以通过添加本地文件来添加。
首先切到box文件所在目录,
$ ls homestead.box
执行如下命令,因为我已经在方案1里面安装过一个laravel/homestead,不能重名,所以换个名字执行
$ vagrant.exe box add laravel/homestead2 homestead.box ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'laravel/homestead2' (v0) for provider: box: Unpacking necessary files from: file://E:/laravel/box/home/homestead.box box: ==> box: Successfully added box 'laravel/homestead2' (v0) for 'virtualbox'!
看到提示成功了,然后再list一下:
$ vagrant.exe box list laravel/homestead (virtualbox, 2.0.0) laravel/homestead2 (virtualbox, 0)
这时候发现一个问题就是,虽然加成功了,但是发现后面没版本号,我保证下载的也是2.0.0版本的。看来肯定是有地方不对。
本来是感觉可以通过在add的时候加参数去实现把版本号带上去,后来没能实现,通过查看文档(https://www.vagrantup.com/docs/getting-started/boxes.html),发现可以通过json文件配置虚拟机提供者和版本等信息。
官网json实例:
{ "name": "hashicorp/precise64", "description": "This box contains Ubuntu 12.04 LTS 64-bit.", "versions": [ { "version": "0.1.0", "providers": [ { "name": "virtualbox", "url": "http://somewhere.com/precise64_010_virtualbox.box", "checksum_type": "sha1", "checksum": "foo" } ] } ] }
可以看到通过上面可以配置多个版本。里面有很多信息可以配置,我们只需要配置自己需要的即可。
配置前,先把之前的删掉吧,顺便学习下删除命令
$ vagrant.exe box remove laravel/homestead2 Removing box 'laravel/homestead2' (v0) with provider 'virtualbox'...
配置的json文件如下:
{ "name": "laravel/homestead2", "versions": [{ "version": "2.0.0", "providers": [{ "name": "virtualbox", "url": "file://homestead.box" }] }] }
最好将该文件与.box文件放到同一个目录(window的目录老出问题,这样避免使用绝对路径)
文件命名是随便的,这里起名为homestead.json。保存。
现在的目录结构:
$ ls homestead.box homestead.json
现在执行添加这个json文件即可,里面规定了.box文件,变得更简洁了。
$ vagrant.exe box add homestead.json ==> box: Loading metadata for box 'homestead.json' box: URL: file://E:/laravel/box/home/homestead.json ==> box: Adding box 'laravel/homestead2' (v2.0.0) for provider: virtualbox box: Unpacking necessary files from: file://homestead.box box: ==> box: Successfully added box 'laravel/homestead2' (v2.0.0) for 'virtualbox'!
现在已经看到成功加进去了,而且版本号也成功的加上了。
再次list一下,看看加进去的box
$ vagrant.exe box list laravel/homestead (virtualbox, 2.0.0) laravel/homestead2 (virtualbox, 2.0.0)
添加box到此圆满结束。
——————————————分割线——————————————
homestead部分
1.下载homestead
$ git clone https://github.com/laravel/homestead.git Homestead Cloning into 'Homestead'... remote: Counting objects: 2134, done. remote: Compressing objects: 100% (26/26), done. remote: Total 2134 (delta 5), reused 0 (delta 0), pack-reused 2107 Receiving objects: 100% (2134/2134), 381.71 KiB | 310.00 KiB/s, done. Resolving deltas: 100% (1227/1227), done.
进入Homestead文件夹查看下目录
$ cd Homestead && ls CHANGELOG.md Vagrantfile composer.json init.bat phpunit.xml.dist resources/ src/ LICENSE.txt bin/ composer.lock init.sh* readme.md scripts/ tests/
这里可以参考laravel的官方文档,有些不用说那么详细了。
初始化,生成Homestead.yaml文件。
$ ./init.sh Homestead initialized!
查看目录,可以看到生成了我们要的文件(和上面对比生成了好几个文件还有文件夹的,关心的话可以自己对比下,我们主要是看.yaml文件):
$ ls CHANGELOG.md Vagrantfile bin/ init.bat readme.md src/ Homestead.yaml after.sh* composer.json init.sh* resources/ tests/ LICENSE.txt aliases composer.lock phpunit.xml.dist scripts/
ps:这里配置文件主要是放到.yaml文件里面的,但是其实vagrant启动的时候是看Vagrantfile这个文件的,在homestead里面,其实是Vagrantfile里面又引用了Homestead.yaml达到对vagrant虚拟机的配置的,有点儿语言基础的都可以简单的去了解下运行加载过程的。除了这个还有些默认的是用ruby写的配置,放在了./scripts/homestead.rb下面(后面会有个小问题,需要改下这里,可能是我自己环境问题)
下一步用ssh-keygen生成一下密钥:
$ ssh-keygen Generating public/private rsa key pair.
这里不用输入什么,直接enter,然后就过去了,最后会在用户目录生成一个.ssh文件夹
$ ls ~/.ssh id_rsa id_rsa.pub
开始启动吧:
$ vagrant.exe up Bringing machine 'homestead-7' up with 'virtualbox' provider... ==> homestead-72: Importing base box 'laravel/homestead2'... Progress: 70%
等progress走到100%就会接着往下走,可以看到名字是homestead-7,使用的box是laravel/homestead。
很遗憾我这里报错了:
$ vagrant.exe up Bringing machine 'homestead-7' up with 'virtualbox' provider... ==> homestead-72: Importing base box 'laravel/homestead2'... ==> homestead-72: Matching MAC address for NAT networking... ==> homestead-72: Checking if box 'laravel/homestead2' is up to date... ==> homestead-72: Setting the name of the VM: homestead-72 ==> homestead-72: Destroying VM and associated drives... C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.3/lib/vagrant/util/is_port_open.rb:21:in `initialize': The requested address is not valid in its context. - connect(2) for "0.0.0.0" port 8000 (Errno::EADDRNOTAVAIL)
。。。。。。。。。。。。。。。。。。。还有很长,就看这一段儿吧。
谷歌一下这个问题,有说是版本不对,有说是加个加个IP(127.0.0.1)的。这里找问题找了好久,考虑到公司里各种IP设置什么的,最后在./scripts/homestead.rb下关于网络配置的一段儿,加了点儿东西。
这段儿是这样的,红色部分是我加的:
# Default Port Forwarding
default_ports = {
80 => 8000,
443 => 44300,
3306 => 33060,
5432 => 54320,
8025 => 8025,
27017 => 27017
}
# Use Default Port Forwarding Unless Overridden
unless settings.has_key?("default_ports") && settings["default_ports"] == false
default_ports.each do |guest, host|
unless settings["ports"].any? { |mapping| mapping["guest"] == guest }
config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true, host_ip: '本机IP'
end
end
end
这是关于vagrant几种网络的设置的,有兴趣可以去(https://www.vagrantup.com/docs/networking/)了解下三种网络配置(公有,私有,转发)。
这里说到:
host_ip
(string) - The IP on the host you want to bind the forwarded port to. If not specified, it will be bound to every IP. By default, this is empty.
默认会绑定所有IP就是提示里面的0.0.0.0 ,改成只是我本地和虚拟机的转发。
改好了,再次vagrant up
$ vagrant.exe up Bringing machine 'homestead-7' up with 'virtualbox' provider... ==> homestead-7: Checking if box 'laravel/homestead' is up to date... ==> homestead-7: Clearing any previously set forwarded ports... ==> homestead-7: Clearing any previously set network interfaces... ==> homestead-7: Preparing network interfaces based on configuration... homestead-7: Adapter 1: nat homestead-7: Adapter 2: hostonly ==> homestead-7: Forwarding ports... homestead-7: 80 (guest) => 8000 (host) (adapter 1) homestead-7: 443 (guest) => 44300 (host) (adapter 1) homestead-7: 3306 (guest) => 33060 (host) (adapter 1) homestead-7: 5432 (guest) => 54320 (host) (adapter 1) homestead-7: 8025 (guest) => 8025 (host) (adapter 1) homestead-7: 27017 (guest) => 27017 (host) (adapter 1) homestead-7: 22 (guest) => 2222 (host) (adapter 1) ==> homestead-7: Running 'pre-boot' VM customizations... ==> homestead-7: Booting VM... ==> homestead-7: Waiting for machine to boot. This may take a few minutes... homestead-7: SSH address: 127.0.0.1:2222 homestead-7: SSH username: vagrant homestead-7: SSH auth method: private key homestead-7: Warning: Remote connection disconnect. Retrying... ==> homestead-7: Machine booted and ready! ==> homestead-7: Checking for guest additions in VM... ==> homestead-7: Setting hostname... ==> homestead-7: Configuring and enabling network interfaces... ==> homestead-7: Mounting shared folders... homestead-7: /vagrant => E:/laravel/Homestead homestead-7: /home/vagrant/Code => C:/Users/sundongdong/Code ==> homestead-7: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> homestead-7: flag to force provisioning. Provisioners marked to run always will still run.
好了,现在已经正常启动了。
使用ssh连接测试一下:
$ vagrant.exe ssh Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-51-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage 3 packages can be updated. 0 updates are security updates. Last login: Fri Mar 24 01:28:34 2017 from 10.0.2.2 vagrant@homestead:~$ ls Code vagrant@homestead:~$ ifconfig enp0s3 Link encap:Ethernet HWaddr 08:00:27:c3:0a:85 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fec3:a85/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:843 errors:0 dropped:0 overruns:0 frame:0 TX packets:620 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:91327 (91.3 KB) TX bytes:80499 (80.4 KB) enp0s8 Link encap:Ethernet HWaddr 08:00:27:29:63:ca inet addr:192.168.10.10 Bcast:192.168.10.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe29:63ca/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:16 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:1296 (1.2 KB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:74 errors:0 dropped:0 overruns:0 frame:0 TX packets:74 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:32145 (32.1 KB) TX bytes:32145 (32.1 KB)
测试通过。
关于.yaml配置文件的更改配置等问题,请参考laravel文档即可。