Windows上使用Vagrant打造Laravel Homestead可協(xié)同跨平臺開發(fā)環(huán)境

前言

大家對VMware或者VirtualBox一定不會(huì)陌生居夹,虛擬化的好處自然深入人心荆忍,而現(xiàn)在我們可以通過Vagrant搭建一套類似Laravel Homestead完整開發(fā)環(huán)境,這樣極大的減少了架設(shè)開發(fā)環(huán)境的時(shí)間囊蓝,同時(shí)還支持在Windows/Mac/Linux不同平臺上分享定制包饿悬,統(tǒng)一團(tuán)隊(duì)之間的開發(fā)環(huán)境提高工作效率,而Docker的出現(xiàn)也讓未來更值得期待聚霜。

用Vagrant為自己打造一個(gè)奇妙的跨平臺開發(fā)環(huán)境


更新歷史

2015年07月18日 - 初稿

閱讀原文 - http://wsgzao.github.io/post/vagrant/

擴(kuò)展閱讀

Vagrant - https://www.vagrantup.com/
Laravel Homestead - http://laravel.com/docs/5.1/homestead
在windows下進(jìn)行l(wèi)inux開發(fā):利用Vagrant+virtualbox - http://blog.star7th.com/2015/06/1538.html
在 Mac/win7 下上使用 Vagrant 打造本地開發(fā)環(huán)境 - http://segmentfault.com/a/1190000002645737


環(huán)境準(zhǔn)備

  1. Git(非必需)
  2. PHP(非必需)
  3. Laravel(非必需)
  4. Composer(非必需)
  5. Vagrant
  6. VirtualBox

如果大家有需要離線安裝歡迎直接留言回復(fù)哈

安裝git

1.下載GitHub for Windows

https://windows.github.com/

安裝php

建議大家盡量安裝當(dāng)前最新版本的 PHP

1.下載PHP

http://windows.php.net/download/

2.解壓目錄

我的路徑D:\php

3.添加環(huán)境變量

右鍵計(jì)算機(jī)->高級系統(tǒng)設(shè)置->環(huán)境變量->系統(tǒng)變量->PATH

C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\nodejs\;D:\php;C:\ProgramData\ComposerSetup\bin

4.設(shè)置php.ini

進(jìn)入 PHP 安裝目錄(例如 D:\php)狡恬。找到 php.ini-development 文件并復(fù)制一份到當(dāng)前目錄,重命名為 php.ini蝎宇,修改以下配置
去掉extension=php_mbstring.dll 前面的分號(888 行左右)
去掉extension=php_openssl.dll前面的分號(893 行左右)
去掉extension_dir = "ext"前面的分號(736 行左右)

5.使環(huán)境變量生效

重啟explorer.exe

安裝Laravel

1.下載Laravel

http://www.golaravel.com/download/

2.解壓目錄

我的路徑D:\laravel-v5.1.4

3.啟動(dòng)Laravel

d:
cd laravel-v5.1.4
D:\laravel-v5.1.4>php artisan serve
Laravel development server started on http://localhost:8000/

在瀏覽器中訪問http://localhost:8000/

artisanserve 命令還支持兩個(gè)參數(shù):

host 設(shè)置主機(jī)地址
port 設(shè)置 web server 監(jiān)聽的端口號
例如:php artisan serve --port=8888

安裝Composer

1.下載Composer-Setup.exe

https://getcomposer.org/doc/00-intro.md#installation-windows

2.配置Composer

Loading composer repositories with package information
Installing dependencies (including require-dev)
SSL certificate problem, verify that the CA cert is OK. Details: 
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed”

如果安裝過程提示報(bào)錯(cuò)為缺少CA證書弟劲,下載cacert.pem到自定義路徑
http://curl.haxx.se/docs/caextract.html

然后修改php.ini文件(1983行左右)

openssl.cafile=D:\php\verify\cacert.pem

3.測試Composer

composer -V

Composer version 1.0-dev (d79427f1a7b15e8f4d46ce8124a4d0c58ba1479c) 2015-07-04 1
1:22:58

安裝Vagrant

1.下載Vagrant

https://www.vagrantup.com/downloads.html

2.離線下載虛擬鏡像

https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.0.0/centos-6.6-x86_64.box

上面給出的是centos-6.6鏡像下載鏈接,要下載其他鏡像請?jiān)L問官網(wǎng)
http://www.vagrantbox.es/

安裝VirtualBox

BIOS里面開啟CPU硬件虛擬化支持VT(Virtualization Technology)

1.下載VirtualBox

https://www.virtualbox.org/wiki/Downloads

2.導(dǎo)入鏡像

設(shè)置VirtualBox目錄并拷貝鏡像centos-6.6-x86_64.box
E:\VirtualBox\centos-6.6-x86_64.box

3.命令初始化Vagrant

#切換VirtualBox目錄
e:
cd .\VirtualBox

#輸入命令初始化
E:\VirtualBox> vagrant init centos6.6
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

#執(zhí)行添加命令
E:\VirtualBox> vagrant box add centos6.6 centos-6.6-x86_64.box
==> box: Adding box 'centos6.6' (v0) for provider:
    box: Downloading: file://E:/VirtualBox/centos-6.6-x86_64.box
    box: Progress: 100% (Rate: 670M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos6.6' (v0) for 'virtualbox'!

#檢查是否導(dǎo)入成功
E:\VirtualBox> vagrant box list
centos6.6 (virtualbox, 0)

Vagrant配置

詳細(xì)配置文檔可以參考官方手冊 - https://docs.vagrantup.com/v2/

啟動(dòng)Vagrant

通過Shell進(jìn)入目錄E:\VirtualBox后執(zhí)行命令

 vagrant up

順利啟動(dòng)的完整過程如下所示

E:\VirtualBox>vagrant upBringing machine 'default' up with 'virtualbox' provider...==> default: Importing base box 'centos6.6'...==> default: Matching MAC address for NAT networking...==> default: Setting the name of the VM: VirtualBox_default_1437213832296_68434==> default: Clearing any previously set forwarded ports...==> default: Clearing any previously set network interfaces...==> default: Preparing network interfaces based on configuration...    default: Adapter 1: nat==> default: Forwarding ports...    default: 22 => 2222 (adapter 1)==> default: Booting VM...==> default: Waiting for machine to boot. This may take a few minutes...    default: SSH address: 127.0.0.1:2222    default: SSH username: vagrant    default: SSH auth method: private key    default: Warning: Connection timeout. Retrying...    default:    default: Vagrant insecure key detected. Vagrant will automatically replace    default: this with a newly generated keypair for better security.    default:    default: Inserting generated public key within guest...    default: Removing insecure key from the guest if it's present...    default: Key inserted! Disconnecting and reconnecting using new SSH key...==> default: Machine booted and ready!==> default: Checking for guest additions in VM...    default: The guest additions on this VM do not match the installed version of    default: VirtualBox! In most cases this is fine, but in rare cases it can    default: prevent things such as shared folders from working properly. If you see    default: shared folder errors, please make sure the guest additions within the    default: virtual machine match the version of VirtualBox you have installed on    default: your host and reload your VM.    default:    default: Guest Additions Version: 4.3.28    default: VirtualBox Version: 5.0==> default: Mounting shared folders...    default: /vagrant => E:/VirtualBox

虛擬機(jī)啟動(dòng)之后則可以通過 vagrant ssh 聯(lián)入虛擬機(jī)進(jìn)行進(jìn)一步的環(huán)境配置姥芥,或者軟件安裝相關(guān)的工作兔乞,在Windows系統(tǒng)下,并不能直接通過vagrant ssh連到虛擬機(jī),需要使用SecureCRT/Putty/Xshell等第三方工具進(jìn)行連接庸追。連接地址127.0.0.1霍骄,端口2222。登錄的帳號root的密碼為vagrant

E:\VirtualBox> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: bridged
==> default: Forwarding ports...
    default: 80 => 8080 (adapter 1)
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in the
'poweroff' state. Please verify everything is configured
properly and try again.

If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve.
For example, if you're using VirtualBox, run `vagrant up` while the
VirtualBox GUI is open.

如果有報(bào)上述錯(cuò)誤锚国,并且運(yùn)行Virtualbox去安裝系統(tǒng)時(shí)出錯(cuò):Failed to open a session for the virtual machine腕巡,Unable to load R3 module C:\Program Files\Oracle\VirtualBox/VBoxDD.DLL (VBoxDD): GetLastError=1790 (VERR_UNRESOLVED_ERROR).玄坦,需要使用UniversalThemePatcher還原未破解的themeservice.dll themeui.dll uxtheme.dll文件

已經(jīng)打包好的下載鏈接 - http://pan.baidu.com/s/1c0HGj2g

==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...

如果報(bào)default: Warning: Connection timeout. Retrying...血筑,建議打編輯Vagrantfile打開VirtualBox圖形化界面vb.gui = true進(jìn)一步分析錯(cuò)誤代碼和原因。

導(dǎo)出box

通過Shell進(jìn)入目錄E:\VirtualBox后執(zhí)行命令

vagrant package

vagrant package --output NAME --vagrantfile FILE

#可選參數(shù):
--output NAME : (可選)設(shè)置通過NAME來指定輸出的文件名
--vagrantfile FILE:(可選)可以將Vagrantfile直接封進(jìn)box中

完成后會(huì)在當(dāng)前目錄就會(huì)生成package.box煎楣,可以在家或者團(tuán)隊(duì)成員共享開發(fā)環(huán)境保持一致性

其它命令

vagrant up (啟動(dòng)虛擬機(jī))
vagrant halt (關(guān)閉虛擬機(jī)——對應(yīng)就是關(guān)機(jī))
vagrant suspend (暫停虛擬機(jī)——只是暫停豺总,虛擬機(jī)內(nèi)存等信息將以狀態(tài)文件的方式保存在本地,可以執(zhí)行恢復(fù)操作后繼續(xù)使用)
vagrant resume (恢復(fù)虛擬機(jī) —— 與前面的暫停相對應(yīng))
vagrant box remove centos6.6 (移除box择懂,其中centos6.6是box名)
vagrant destroy (刪除虛擬機(jī)喻喳,刪除后在當(dāng)前虛擬機(jī)所做進(jìn)行的除開Vagrantfile中的配置都不會(huì)保留)

Laravel Homestead

細(xì)節(jié)部分可參考官方文檔 - http://laravel.com/docs/5.1/homestead

1.下載安裝包

vagrant box add laravel/homestead

E:\Homestead>vagrant box add laravel/homestead
==> box: Loading metadata for box 'laravel/homestead'
    box: URL: https://atlas.hashicorp.com/laravel/homestead
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) virtualbox
2) vmware_desktop

Enter your choice: 1
==> box: Adding box 'laravel/homestead' (v0.2.7) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/0.2.7/providers/virtualbox.box
    box: Progress: 0% (Rate: 9d/s, Estimated time remaining: 0:05:30)11))

由于國內(nèi)網(wǎng)絡(luò)環(huán)境問題建議離線下載后手動(dòng)導(dǎo)入

#輸入命令初始化
E:\Homestead>vagrant init laravel
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

#執(zhí)行添加命令
E:\Homestead>vagrant box add laravel laravel.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'laravel' (v0) for provider:
    box: Unpacking necessary files from: file://E:/Homestead/laravel.box
    box: Progress: 100% (Rate: 141M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'laravel' (v0) for 'virtualbox'!

#檢查是否導(dǎo)入成功
E:\Homestead>vagrant box list
centos6.6 (virtualbox, 0)
laravel   (virtualbox, 0)

#啟動(dòng)Lavarel Homestead
E:\Homestead>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'laravel'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: Homestead_default_1437217549272_56101
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.14
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => E:/Homestead

登錄帳戶vagrant/vagrant,開始全新的Laravel Homestead體驗(yàn)之旅吧困曙。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末表伦,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子慷丽,更是在濱河造成了極大的恐慌蹦哼,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件要糊,死亡現(xiàn)場離奇詭異纲熏,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)锄俄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門局劲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人奶赠,你說我怎么就攤上這事鱼填。” “怎么了毅戈?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵剔氏,是天一觀的道長。 經(jīng)常有香客問我竹祷,道長谈跛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任塑陵,我火速辦了婚禮感憾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己阻桅,他們只是感情好凉倚,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著嫂沉,像睡著了一般稽寒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上趟章,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天杏糙,我揣著相機(jī)與錄音,去河邊找鬼蚓土。 笑死宏侍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蜀漆。 我是一名探鬼主播谅河,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼确丢!你這毒婦竟也來了绷耍?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤鲜侥,失蹤者是張志新(化名)和其女友劉穎褂始,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體剃毒,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡病袄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了赘阀。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片益缠。...
    茶點(diǎn)故事閱讀 39,902評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖基公,靈堂內(nèi)的尸體忽然破棺而出幅慌,到底是詐尸還是另有隱情,我是刑警寧澤轰豆,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布胰伍,位于F島的核電站,受9級特大地震影響酸休,放射性物質(zhì)發(fā)生泄漏骂租。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一斑司、第九天 我趴在偏房一處隱蔽的房頂上張望渗饮。 院中可真熱鬧,春花似錦、人聲如沸互站。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽胡桃。三九已至踩叭,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間翠胰,已是汗流浹背容贝。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留亡容,地道東北人嗤疯。 一個(gè)月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓冤今,卻偏偏與公主長得像闺兢,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子戏罢,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容