想學習下laravel才瞭解到這個,因此着手研究了一下。php
電腦是wind7 64位系統(最好安裝git,在git bash下面執行下面的命令)。html
1.準備安裝visual box和vagrant軟件(建議下載最新版本的,由於通常剛下載的都是新的,若是你的版本太久可能會出現問題)laravel
準備好了就開始吧!git
$ vagrant.exe box add laravel/homestead
執行上面的命令,通常國內有牆致使下載巨慢,估計要幾天的節奏就算了(我這裏沒牆都下載了幾個小時,其實也纔不到1G),網通常的話就放棄方案1吧。github
我是使用的方案1,下面會和方案2進行比對。json
$ vagrant.exe box list laravel/homestead (virtualbox, 2.0.0)
下載完了,看下list,看到下載到了最新版的。ubuntu
去網上找到這個.box文件,去這裏(https://atlas.hashicorp.com/boxes/search)搜索你要的。找到後,去問萬能的網友找資源便可。ruby
下載好了以後文件最好重命名一下,個人是homestead.box。有了該文件能夠經過添加本地文件來添加。bash
首先切到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到此圓滿結束。
——————————————分割線——————————————
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文檔便可。