剛開(kāi)始做開(kāi)發(fā)的時(shí)候的都是把開(kāi)發(fā)環(huán)境 配置在 自己的電腦上,隨著后面我們接觸的東西越來(lái)越多惜论,慢慢的電腦上都是各種環(huán)境了许赃,php,java馆类,python混聊,nodejs等等,非常麻煩乾巧,并且經(jīng)常由于某種原因就把電腦重裝了句喜,ORZ,所以環(huán)境都要重來(lái)沟于。所以打造一個(gè)屬于自己并且可以移動(dòng)的環(huán)境是非常重要的咳胃。
vagrant就是一款構(gòu)建虛擬開(kāi)發(fā)環(huán)境的工具,支持window旷太,linux展懈,mac,總有一款適合你供璧。并且vagrant 可以把配置好的環(huán)境打包成一個(gè)box存崖,分享給其他人直接使用,非常方便
系統(tǒng)要求:(本人mac系統(tǒng))
機(jī)器支持虛擬化睡毒,有時(shí)候需要調(diào)整bios開(kāi)啟cpu虛擬化来惧。
第一步:安裝virtualbox
具體安裝包請(qǐng)直接從官網(wǎng)下載:https://www.virtualbox.org/wiki/Downloads?
第二步:? 安裝 vagrant???????? 具體安裝包請(qǐng)直接從官網(wǎng)下載:
$ vagrant -v
Vagrant 1.6.3
第三步:vagrant 基本命令
官網(wǎng)文檔:https://www.vagrantup.com/docs/getting-started/index.html?
??? 1.vagrant init? ? ? ? 初始化vagrantfile
??? 2.vagrant add box? ? 添加box,自動(dòng)幫你生成vagrantfile
??? 3.vagrant halt? ? ? ? 關(guān)閉虛擬機(jī)
??? 4.vagrant destroy? ? 銷(xiāo)毀虛擬機(jī)
??? 5.vagrant ssh? ? ? ? 連接虛擬機(jī)
??? 6.vagrant reload? ? ? 重新加載vagarntfile文件
??? 7.vagrant suspend? ? 暫時(shí)掛起虛擬機(jī)
??? 8.vagrant status? ? ? 查看虛擬機(jī)運(yùn)行狀態(tài)
第四步:示例演示
4.1 下載box
下面是ubuntu的兩個(gè)官方地址:
Ubuntu precise 32 VirtualBox http://files.vagrantup.com/precise32.box?
Ubuntu precise 64 VirtualBox http://files.vagrantup.com/precise64.box?
如果你要其他系統(tǒng)的鏡像演顾,可以來(lái)這里下載:http://www.vagrantbox.es/?
????? 1.$ mkdir -p? ~/vagrant/boxes
????? 2.$ cd ~/vagrant/boxes && wget
????? 3.$ vagrant box add ubuntu64 precise64.box //添加box
4.2 新建一個(gè)lnmp(linux + nginx + mysql + php-fpm)虛擬機(jī)
????? 1.$ cd ~/vagrant && mkdir lnmp
????? 2.$ cd lnmp
????? 3.$ vagrant init ubuntu64 //初始化
????? 4.$ vagrant up //啟動(dòng)虛擬機(jī)lnmp
????? 5.$ vagrant ssh //進(jìn)入虛擬機(jī)lnmp
這個(gè)虛擬機(jī)就新建好了供搀,是不是很快!钠至!熟悉linux的就可以修改源葛虐,安裝軟件了
????? 1.$ sudo apt-get install? keychain nginx mysql-server mysql-client? php5-cli php5-fpm php5-gd php5-mysql
第五步:個(gè)性化配置Vagrantfile
在進(jìn)行 vagrant init 之后我們會(huì)發(fā)現(xiàn)對(duì)應(yīng)的目錄下有個(gè)Vagrantfile文件,直接上截圖
第六步:導(dǎo)出自己的box
???? 1.$ cd ~/VirtualBox\ VMs/phponubuntu64
???? 2.$ vagrant package? --output lamp5_5onubuntu64.box --base phponubuntu64
注意事項(xiàng)
問(wèn)題一)使用 Apache/Nginx 時(shí)會(huì)出現(xiàn)諸如圖片修改后但頁(yè)面刷新仍然是舊文件的情況棉钧,是由于靜態(tài)文件緩存造成的挡闰。需要對(duì)虛擬機(jī)里的 Apache/Nginx 配置文件進(jìn)行修改:
??????? #nginx配置
??????? sendfile off
??????? #apache 配置
??????? EnableSendFile off
問(wèn)題二:?jiǎn)?dòng)多個(gè)虛擬機(jī)提示錯(cuò)誤“2222 端口 in use”
解決辦法:(參考文章:Vagrant SSH errors with multiple VMs: Port 2222 in use?)
???????? config.vm.network "forwarded_port", guest: 22, host: 2220 ,id: 'ssh'
問(wèn)題三:如果遇到 php 代碼也感覺(jué)被緩存起來(lái)了,請(qǐng)關(guān)閉apc掰盘,opcode(從php5.5及以后版本默認(rèn)開(kāi)啟opcode)
問(wèn)題四:config.vm.boot_timeout
??????? 1.Timed out while waiting for the machine to boot. This means that
??????? 2.Vagrant was unable to communicate with the guest machine within
??????? 3.the configured ("config.vm.boot_timeout" value) time period.
??????? 4.If you look above, you should be able to see the error(s) that
??????? 5.Vagrant had when attempting to connect to the machine. These errors
??????? 6.are usually good hints as to what may be wrong.
??????? 7.If you're using a custom box, make sure that networking is properly
??????? 8.working and you're able to connect to the machine. It is a common
??????? 9.problem that networking isn't setup properly in these boxes.
?????? 10.Verify that authentication configurations are also setup properly,
?????? 11.as well.
?????? 12.If the box appears to be booting properly, you may want to increase
?????? 13.the timeout ("config.vm.boot_timeout") value.
類(lèi)似如上的錯(cuò)誤日志,解決方案是摄悯,設(shè)置=>網(wǎng)絡(luò)=>高級(jí)=>選中 cable connected
問(wèn)題五:GUI模式waiting for network configuration
??????? 1.waiting for network configuration
??????? 2.waiting an additional 60 seconds for network configuration
問(wèn)題六:Warning: Authentication failure. Retrying...
那么把?https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub?? 公鑰放到authorized_keys 文件中,重新vagrant reload