laravel homestead vagrant box安裝使用,問題,及相關命令

世有因果知因求果發表於2015-06-11

Vagrant is a tool that manages oracle virtual boxes

1.本地下載https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.2.6/providers/virtualbox.box下載該box作為base,

(https://atlas.hashicorp.com/boxes/search?utf8=%E2%9C%93&sort=&provider=&q=homestead可以用來查詢有哪些box)

https://atlas.hashicorp.com/antoniofrignani/boxes/laravel-homestead-settler-32/versions/0.1.1/providers/virtualbox.box

2.本地新增 vagrant box add laravel/homestead (path to downloaded file)(base box above!)

3.git clone https://github.com/laravel/homestead.git Homestead

4.執行init.sh將生成Homestead.yaml配置檔案(實際上在Homestead目錄將建立Vagrantfile,Homestead.yaml只是populate這個vagrantfile的指令碼,方便配置(相當於執行vagrant init)

5.配置相關引數

http://laravel.com/docs/5.1/homestead

6.vagrant up即可使用了(直接呼叫virtualbox的api將基於base box的vagrantfile代表的客製化環境(shared folder,network,memroy,forwarded ports etc)執行起來)

 https://phphub.org/topics/2

Homestead 包括以下兩個東西

  1. 一個 vagrant box 虛擬機器, 裡面軟體什麼都下載好了;
  2. Github 程式碼庫, 裡面裝載著 vagrant 的配置指令碼, 用來自動化配置網路, 埠對映, 等一些開發時候用到的配置;

 

 windows下如果出現解壓錯誤,那麼可以使用一下方法解決

The executable 'bsdtar' Vagrant is trying to run was not
found in the %PATH% variable. This is an error. Please verify
this software is installed and on the path.

解決方案是從sourceforge.net下載一個bsdtar.exe basic_bsdtar.exe from here   http://sourceforge.net/projects/mingw/files/MinGW/Extension/bsdtar/ and rename it to bsdtar.exe and place it in C:\HashiCorp\Vagrant\embedded\gnuwin32\bin\

在win7中,當vagrant up時出現如下錯誤:

 Unable to load R3 module D:\Program Files\Oracle\VirtualBox/VBoxDD.dll (VBoxDD):GetLastError=1790 (VERR_UNRESOLVED_ERROR)

則可能是因為

本目錄中的以下三個檔案是原始的未被破解的WIN7 64位系統主題檔案:
themeservice.dll
themeui.dll
uxtheme.dll這三個檔案被破解所導致的。解決方案是恢復它,參考:http://download.csdn.net/download/friendan/8199047

在windows下面,由於目錄名稱最長只有260字元,使用node gulp,grunt等模組時,往往出現Cannot mkdir: Protocol error錯誤

可能的解決方案參考:

http://stackoverflow.com/questions/30090691/configuring-homestead-to-work-for-windows-users/30094439#30094439

https://github.com/npm/npm/issues/3670

https://github.com/mitchellh/vagrant/pull/5495

關於UNC的知識點: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#short_vs._long_names

總結一下homestead vbox的UNC name配置解決方案:

1. comment out ~/.homestead/homestead.yaml中的資料夾共享:
# folders:
#    - map: D:\devenv\code
#      to: /home/vagrant/code
2. 在vagrantfile中將以下內容插入
# this should be inserted right before the "Homestead.configure()" line
    config.vm.provider "virtualbox" do |v, override|
        if Vagrant::Util::Platform.windows?
            override.vm.synced_folder "D:\devenv\code", "/home/vagrant/code", disabled: true

            v.customize ["sharedfolder", "add", :id, "--name", "code", "--hostpath", (("//?/D:/devenv/code").gsub("/","\\"))]

            override.vm.provision :shell, inline: "mkdir -p /home/vagrant/code"
            override.vm.provision :shell, inline: "mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` code /home/vagrant/code", run: "always"
        else
            override.vm.synced_folder "D:\devenv\code", "/home/vagrant/code"
        end

        v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/code", "1"]
    end
    # this should be inserted right before the "Homestead.configure()" line

 除了上面的使用windows UNC來解決node/bower的長檔名問題外,對於node我們還有一種解決方案,就是使用https://github.com/arifsetiawan/flatten 這個package,來對node_modules目錄進行扁平化,用法是在linux或者windows unc下徹底安裝node modules後,執行下面的命令:

flatten-packages PROJECT_DIRECTORY

命令完成後,就將老的node_modules全部扁平化,徹底解決長檔名的問題!!!!同時該命令將刪除老的垂直安排的module

homestead下node npm團隊使用的實踐探索

一般情況下,沒有必要每個團隊成員都完全安裝node,npm以及對應的node_modules,可行的措施是將node_modules本身作為一個repo,同時將node.exe檔案copy到專案的root folder(就是package.json的所在目錄),這樣團隊成員就不用安裝node以及對應的package了,又能正常做build!!

windows下除了檔名長度問題外,預設如果不是以administrator執行virtualbox的話還有無法建立symlink的問題,所以一般如果使用npm,bower自動化你的測試開發流程時,可能會由於無法在windows/virtualbox之間的共享的資料夾中建立symlink而導致一些煩人的問題,一個可行的workaround是把node module, bower module等放到非windows共享資料夾中, bower可以通過.bowerc指定bower 模組安裝位置, package.json以及node_modules可以放到非win目錄的一個wrapper目錄中。當然,也可以通過將執行vagrant up命令的命令列以administrator身份啟動也能夠解決windows環境下在homestead virtualbox中不能建立symbol link的問題!!!也就是說在win10下是可以建立symlink的!

 

 在windows作業系統中,可能需要再bios中設定intel CPU的虛擬化功能使能

vagrant up過程中,可能出現: default: Warning: Connection timeout. Retrying..., 這時可能的解決方案是:

在vagrantfile中增加以下內容,使能gui,檢查是否是停留在了grub那裡:

config.vm.provider "virtualbox" do |v|
    v.gui = true
end
還有一種解決方案是修改grub資訊,給一個timeout超時。 http://segmentfault.com/a/1190000000266564出現這種錯誤,另外一個可能是你的vt沒有開啟,在bios中設定虛擬化有可能解決問題。還有一種方案是選擇32bit的系統,具體參考: http://laravel.io/forum/05-18-2014-trouble-getting-homestead-to-start-in-windows?page=1

用 VirtualBox 皮膚執行該系統,在 GRUB 介面回車登入系統,使用者名稱:vagrant,密碼:vagrant,編輯/etc/grub.d/00_header,找到:

if [ "\${recordfail}" = 1 ]; then
  set timeout=-1

將 -1 改成 10 即可:

if [ "\${recordfail}" = 1 ]; then
  set timeout=10

再執行 $ update-grub 更新 GRUB,關機後再用 $ vagrant up 啟動就能正常啟動了。

 

 

在windows下面如果你做好了配置,hosts檔案也正確,但是一直出現no input files這個錯誤,可以嘗試vagrant up --provision,如果確實已經up狀態了,

可以只執行: vagrant provision就可以了。注意只要.yaml配置檔案修改了,最好都執行vagrant reload --provision

參考: https://github.com/mitchellh/vagrant/issues/3869#issuecomment-48058728 (檔案本身在網盤有備份)

當你發現虛擬機器開發環境客製化滿意後,你可以做的事情是將該虛擬機器打包成另外一個box,以便提供給別人使用,提供一套統一的開發環境,確實不錯哦:

vagrant package --output vagrant_example.box (在你的vagrantfile所在目錄) 或者:
vagrant package xxpackage --output xxpackage.box --vagrantfile Vagrantfile

http://lovelace.blog.51cto.com/1028430/1423343

 browsersync的watch/reload任務監聽3000埠,但是我們需要增加一個forwarded port在vagrantfile中,但是有時不工作,那麼可以手工在virtualbox中新增埠轉發就可以了。

 常用命令:

vagrant box list

vagrant init xxBox :建立vagrantfile,並且將box資訊更新為xxBox

vagrant up   (在有vagrantfile的目錄執行)

vagrant ssh-config  :在你的vagrantfile所在目錄檢查該vagrant box的ssh配置資訊

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile "c:/Documents and Settings/zhang/devenvironment/Homestead/.vagrant/machines/default/v
irtualbox/private_key"
  IdentitiesOnly yes
  LogLevel FATAL

為工作方便建立一個ssh vagrant的alias,該命令等同於在homestead目錄下執行vagrant ssh

alias vm="ssh vagrant@127.0.0.1 -p 2222 -i ~/devenvironment/Homestead/.vagrant/machines/default/virt
ualbox/private_key"

同時,一旦登陸到系統後執行一個.postlogin.sh(cd xxx/yyy)目錄,這樣就很方便了(source呼叫上述指令碼)

在homestead下也可以非常方便地建立新的sites:注意: 如果出現serve command not found錯誤的話,可以手工 source ~/.bashrc,也就是說上述serve命令就在.bashrc檔案中定義的!!

vagrant@homestead:~$ serve trace.dev /home/vagrant/code/xdebug-trace-gui
dos2unix: converting file /vagrant/scripts/serve.sh to Unix format ...
nginx stop/waiting
nginx start/running, process 2646
php5-fpm stop/waiting
php5-fpm start/running, process 2662

 console下彩色顯示

alias ls='ls --color=always'

相關文章