快速打造屬于自己的跨平臺(tái)開發(fā)環(huán)境 vagrant + virtualbox + box

剛開始做開發(fā)的時(shí)候的都是把開發(fā)環(huán)境 配置在 自己的電腦上,隨著后面我們接觸的東西越來越多,慢慢的電腦上都是各種環(huán)境了,php,java,python,nodejs等等,非常麻煩,并且經(jīng)常由于某種原因就把電腦重裝了,ORZ,所以環(huán)境都要重來。所以打造一個(gè)屬于自己并且可以移動(dòng)的環(huán)境是非常重要的。

vagrant就是一款構(gòu)建虛擬開發(fā)環(huán)境的工具,支持window,linux,mac,總有一款適合你。并且vagrant 可以把配置好的環(huán)境打包成一個(gè)box,分享給其他人直接使用,非常方便

系統(tǒng)要求:(本人mac系統(tǒng))

機(jī)器支持虛擬化,有時(shí)候需要調(diào)整bios開啟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? ? 銷毀虛擬機(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)的鏡像,可以來這里下載: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)

問題一)使用 Apache/Nginx 時(shí)會(huì)出現(xiàn)諸如圖片修改后但頁面刷新仍然是舊文件的情況,是由于靜態(tài)文件緩存造成的。需要對(duì)虛擬機(jī)里的 Apache/Nginx 配置文件進(jìn)行修改:

??????? #nginx配置

??????? sendfile off

??????? #apache 配置

??????? EnableSendFile off

問題二:?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'

問題三:如果遇到 php 代碼也感覺被緩存起來了,請(qǐng)關(guān)閉apc,opcode(從php5.5及以后版本默認(rèn)開啟opcode)

問題四: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.

類似如上的錯(cuò)誤日志,解決方案是,設(shè)置=>網(wǎng)絡(luò)=>高級(jí)=>選中 cable connected

問題五:GUI模式waiting for network configuration
??????? 1.waiting for network configuration

??????? 2.waiting an additional 60 seconds for network configuration

問題六:Warning: Authentication failure. Retrying...

那么把?https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub?? 公鑰放到authorized_keys 文件中,重新vagrant reload

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容