剛開始做開發的時候的都是把開發環境 配置在 自己的電腦上,隨著后面我們接觸的東西越來越多,慢慢的電腦上都是各種環境了,php,java,python,nodejs等等,非常麻煩,并且經常由于某種原因就把電腦重裝了,ORZ,所以環境都要重來。所以打造一個屬于自己并且可以移動的環境是非常重要的。
vagrant就是一款構建虛擬開發環境的工具,支持window,linux,mac,總有一款適合你。并且vagrant 可以把配置好的環境打包成一個box,分享給其他人直接使用,非常方便
系統要求:(本人mac系統)
機器支持虛擬化,有時候需要調整bios開啟cpu虛擬化。
第一步:安裝virtualbox
具體安裝包請直接從官網下載:https://www.virtualbox.org/wiki/Downloads?
第二步:? 安裝 vagrant???????? 具體安裝包請直接從官網下載:
$ vagrant -v
Vagrant 1.6.3
第三步:vagrant 基本命令
官網文檔:https://www.vagrantup.com/docs/getting-started/index.html?
??? 1.vagrant init? ? ? ? 初始化vagrantfile
??? 2.vagrant add box? ? 添加box,自動幫你生成vagrantfile
??? 3.vagrant halt? ? ? ? 關閉虛擬機
??? 4.vagrant destroy? ? 銷毀虛擬機
??? 5.vagrant ssh? ? ? ? 連接虛擬機
??? 6.vagrant reload? ? ? 重新加載vagarntfile文件
??? 7.vagrant suspend? ? 暫時掛起虛擬機
??? 8.vagrant status? ? ? 查看虛擬機運行狀態
第四步:示例演示
4.1 下載box
下面是ubuntu的兩個官方地址:
Ubuntu precise 32 VirtualBox http://files.vagrantup.com/precise32.box?
Ubuntu precise 64 VirtualBox http://files.vagrantup.com/precise64.box?
如果你要其他系統的鏡像,可以來這里下載:http://www.vagrantbox.es/?
????? 1.$ mkdir -p? ~/vagrant/boxes
????? 2.$ cd ~/vagrant/boxes && wget
????? 3.$ vagrant box add ubuntu64 precise64.box //添加box
4.2 新建一個lnmp(linux + nginx + mysql + php-fpm)虛擬機
????? 1.$ cd ~/vagrant && mkdir lnmp
????? 2.$ cd lnmp
????? 3.$ vagrant init ubuntu64 //初始化
????? 4.$ vagrant up //啟動虛擬機lnmp
????? 5.$ vagrant ssh //進入虛擬機lnmp
這個虛擬機就新建好了,是不是很快!!熟悉linux的就可以修改源,安裝軟件了
????? 1.$ sudo apt-get install? keychain nginx mysql-server mysql-client? php5-cli php5-fpm php5-gd php5-mysql
第五步:個性化配置Vagrantfile
在進行 vagrant init 之后我們會發現對應的目錄下有個Vagrantfile文件,直接上截圖
第六步:導出自己的box
???? 1.$ cd ~/VirtualBox\ VMs/phponubuntu64
???? 2.$ vagrant package? --output lamp5_5onubuntu64.box --base phponubuntu64
注意事項
問題一)使用 Apache/Nginx 時會出現諸如圖片修改后但頁面刷新仍然是舊文件的情況,是由于靜態文件緩存造成的。需要對虛擬機里的 Apache/Nginx 配置文件進行修改:
??????? #nginx配置
??????? sendfile off
??????? #apache 配置
??????? EnableSendFile off
問題二:啟動多個虛擬機提示錯誤“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 代碼也感覺被緩存起來了,請關閉apc,opcode(從php5.5及以后版本默認開啟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.
類似如上的錯誤日志,解決方案是,設置=>網絡=>高級=>選中 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