LoginSignup
13
12

More than 5 years have passed since last update.

Laravel5.2のHomesteadを複数たててみた

Last updated at Posted at 2016-06-20

Homesteadを複数立てたくなったのでメモ。
※1Homesteadに複数Laravel作ってもいいけど・・・・環境わけるのにVMつかってるのにどうなの?とおもったまる

注)やってみてできた結果論です!元のファイルはバックアップとってください。

環境

Max OSX 10.11.2
Vagrant 1.8.1
VirtualBox 5.0.20
Homesteadは2016/5/30現在の最新

既存Homesteadの作業ディレクトリ:~/dev/Homestead/
追加するHomesteadの作業ディレクトリ:~/dev/Homestead-test/

.homestead のディレクトリ移動

自動的に~/.homestead/にファイル群がおかれるので、
~/dev/project/.homestead/に移動。

移動後、Vagrantfileを編集

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'json'
require 'yaml'

VAGRANTFILE_API_VERSION = "2"
confDir = $confDir ||= File.expand_path("~/dev/project/.homestead")

homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
aliasesPath = confDir + "/aliases"

require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    if File.exists? aliasesPath then
        config.vm.provision "file", source: aliasesPath, destination: "~/.bash_aliases"
    end

    if File.exists? homesteadYamlPath then
        settings = YAML::load(File.read(homesteadYamlPath))
    elsif File.exists?   then
        settings = JSON.parse(File.read(homesteadJsonPath))
    end

    Homestead.configure(config, settings)

    if File.exists? afterScriptPath then
        config.vm.provision "shell", path: afterScriptPath, privileged: false
    end

    if defined? VagrantPlugins::HostsUpdater
        config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] }
    end
end

line.8を、
confDir = $confDir ||= File.expand_path("~/.homestead")
から
confDir = $confDir ||= File.expand_path("~/dev/project/.homestead")
に変更した。

vagrantのbox名変更

~/.vagrant.d/boxes/の下に実体ファイルがるのでリネーム

$ cd ~/.vagrant.d/boxes/
$ mv laravel-VAGRANTSLASH-homestead prj-homestead
$ vagrant box list
centos7           (virtualbox, 0)
prj-homestead   (virtualbox, 0.4.4)

※この下(init)不要作業かもしれない・・・
既存Homesteadの作業ディレクトりにもどって、元あったVagrantfileは別名つけといて

$ vagrant init prj-homestead

おわったら、できたファイルを別名にして、↑でわけといた元のファイルをもどす。(Homesteadが自動生成したほうの、長いVagrantfileになってればオk)
ここで生成されたVagrantfileの中のbox名は、あとでHomestead.rbに書く(後述)

virtualbox のvm名前変更

~/VirtualBox VMs/ の下に実体ファイルがあったので、virtualboxのGUIから名前を変更。

"homestead-7" → "prj-homestead" に変更。

Homestead.rbの編集

既存Homestead/scripts/Homestead.rb を編集

Homestead.rb
(略)
#line.15あたり
   # Configure The Box
    #config.vm.box = settings["box"] ||= "laravel/homestead"
    config.vm.box = settings["box"] ||= "prj-homestead"
    config.vm.box_version = settings["version"] ||= ">= 0.4.0"
    config.vm.hostname = settings["hostname"] ||= "homestead"
(略)
#line.30あたり
    # Configure A Few VirtualBox Settings
    config.vm.provider "virtualbox" do |vb|
      # vb.name = settings["name"] ||= "homestead-7"
      vb.name = settings["name"] ||= "prj-homestead"
(略)
# line.40あたり
    # Configure A Few VMware Settings
    ["vmware_fusion", "vmware_workstation"].each do |vmware|
      config.vm.provider vmware do |v|
        #v.vmx["displayName"] = settings["name"] ||= "homestead-7"
        v.vmx["displayName"] = settings["name"] ||= "prj-homestead"
(後略)

変更したvagrantのbox名、virtualboxのvm名に変更。

Vagrant 起動

$ vagrant up --provision で起動。

とりあえず、既存Homesteadが起動。(http://homestead.app で見えた!)
念のため、いったんhaltします。

新Homesteadのインストール

以前(vagrant on mac にLaravel Homesteadを入れてみる)と手順はおなじ。
変更点は以下の通り。
・Homestead作業ディレクトリは、~/dev/Homestead-test/
・IP=192.168.10.11
・site=homestead-test.app

$ vagrant box list
centos7           (virtualbox, 0)
prj-homestead   (virtualbox, 0.4.4)
laravel/homestead (virtualbox, 0.4.4)

$ vagrant up --provision で起動してプロジェクト作成、
http://homestead-test.app/  で見えた。

既存HomesteadとW起動してみる

既存のほうもVagrant up して、みれた!

とりあえずこんなかんじで。


参考

13
12
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12