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


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'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM