Vagrantfile里面若是使用了hostmanager这个插件,就必需要配置github_token,不然没法启动虚拟机。php
这个插件可让咱们使用y2aa.dev这样的域名访问咱们的项目,而是不使用192.168.xx.xx之类的ip。mysql
vagrant-local.yml配置,填上你的github token,并把时区改成Asia/Shanghai:,以下:nginx
# Your personal GitHub token github_token: 'xxxxxxxxxxx' # Read more: https://github.com/blog/1509-personal-api-tokens # You can generate it here: https://github.com/settings/tokens # Guest OS timezone timezone: Asia/Shanghai # Are we need check box updates for every 'vagrant up'? box_check_update: false # Virtual machine name machine_name: y2aa # Virtual machine IP ip: 192.168.100.200 # Virtual machine CPU cores number cpus: 1 # Virtual machine RAM memory: 1024
启动完成以后,在浏览器输入http://y2aa.dev
git
你能够看到启动过程作了不少工做,配置网络、端口映射、安装/重启nginx、mysql、php等,这些都是经过Vagrantfile这个文件来实现的,让咱们来看看这个文件!github
require 'yaml' require 'fileutils' #这里配置前、后台的域名 domains = { frontend: 'y2aa.dev', backend: 'y2aa.dev/admin' } #配置文件的路径 config = { local: './vagrant/config/vagrant-local.yml', example: './vagrant/config/vagrant-local.example.yml' } # copy config from example if local config not exists FileUtils.cp config[:example], config[:local] unless File.exist?(config[:local]) # read config options = YAML.load_file config[:local] # check github token if options['github_token'].nil? || options['github_token'].to_s.length != 40 puts "You must place REAL GitHub token into configuration:\n/yii2-app-advancded/vagrant/config/vagrant-local.yml" exit end # vagrant configurate Vagrant.configure(2) do |config| # select the box.使用ubuntu14.04 64-bit config.vm.box = 'ubuntu/trusty64' # should we ask about box updates? config.vm.box_check_update = options['box_check_update'] config.vm.provider 'virtualbox' do |vb| # machine cpus count vb.cpus = options['cpus'] # machine memory size vb.memory = options['memory'] # machine name (for VirtualBox UI) vb.name = options['machine_name'] end # machine name (for vagrant console) config.vm.define options['machine_name'] # machine name (for guest machine console) config.vm.hostname = options['machine_name'] # network settings config.vm.network 'private_network', ip: options['ip'] # sync: folder 'yii2-app-advanced' (host machine) -> folder '/app' (guest machine) #把当前目录同步到虚拟机的 /app 目录下面 config.vm.synced_folder './', '/app', owner: 'vagrant', group: 'vagrant' # disable folder '/vagrant' (guest machine) config.vm.synced_folder '.', '/vagrant', disabled: true # hosts settings (host machine) # 使用hostmanager,把咱们的先后台的域名绑定到虚拟机的ip上 config.vm.provision :hostmanager config.hostmanager.enabled = true config.hostmanager.manage_host = true config.hostmanager.ignore_private_ip = false config.hostmanager.include_offline = true config.hostmanager.aliases = domains.values # provisioners # 集成预安装,run: 'always' 设置成每次执行vagrant up启动环境都执行一次这个脚本,不然只在初次运行时执行。 config.vm.provision 'shell', path: './vagrant/provision/once-as-root.sh', args: [options['timezone']] config.vm.provision 'shell', path: './vagrant/provision/once-as-vagrant.sh', args: [options['github_token']], privileged: false config.vm.provision 'shell', path: './vagrant/provision/always-as-root.sh', run: 'always' # post-install message (vagrant console) config.vm.post_up_message = "Frontend URL: http://#{domains[:frontend]}\nBackend URL: http://#{domains[:backend]}" end
看来想要一些特殊服务,只能去修改./vagrant/provision/下面的三个脚本了,固然你也能够添加本身的脚本。sql
首先来看once-as-root.sh,以root身份运行一次,只在初次启动环境时执行,除非执行了"vagrant reload --provision"命令。这个脚本将会安装nginx、php、mysql,并进行配置。不懂shell命令也不要紧,咱们只要作一点点修改:shell
找到60行:数据库
ln -s /app/vagrant/nginx/app.conf /etc/nginx/sites-enabled/app.conf
注意上面的路径都是虚拟机里面的路径。这个命令把 ./vagrant/nginx/app.conf软连接到了nginx的配置文件目录下面,因此咱们只要修改./vagrant/nginx/app.conf就能够应用到虚拟机里面。ubuntu
再找到64行:api
info "Initailize databases for MySQL" #mysql -uroot <<< "CREATE DATABASE yii2advanced" #mysql -uroot <<< "CREATE DATABASE yii2_advanced_tests" mysql -uroot <<< "CREATE DATABASE IF NOT EXISTS Your_db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_bin" mysql -uroot -DYour_db_name < /app/vagrant/mysql/your.sql echo "Done!"
把原来的建立数据库的命令注释掉,而后加上你本身的,你须要在 ./vagrant 目录下面建一个名为mysql的目录,用来存放sql文件。固然,有时候你不但愿sql文件加入版本控制里面,你能够排除它。或者大家团队共享一个局域网里面的数据库,这时候就不用再建立数据库了,甚至连安装mysql均可以省了。若是不须要mysql,能够把相关的命令都注释掉。
其它两个脚本你也能够根据须要修改。
5.若是修改了Vagrantfile或 vagrant目录下面的脚本和配置而又不是初次运行,你须要执行一下命令:
vagrant reload --provision
不少博客都有介绍"vagrant package"命令打包配置好的虚拟机分发给其余人,我试了一下,打包的以后的大小大概500M。我不建议打包,一个是咱们初始化的时候绑定了ip,还有就是若是更改了配置,又要从新打包分发。其实咱们只要执行一下第5步的命令就可使更改应用到虚拟机。