LoginSignup
16
18

More than 5 years have passed since last update.

【メモ】Vagrantで作ったローカル環境(Mac)をスマホ実機から見れるようにする

Posted at

条件

固定プライベートIPを使うので、会社によってはNGかも

Vagrantの設定

wifiをpublic_networkに設定してIPを固定する。
他の人と被らないようにしないと社内のシステム担当者に怒られる可能性あるので、
ちゃんと管理者に確認する。

この例だとwapサーバにはapacheがインストールされていて、ホスト側の~/Vagrant/data/wap/にphpのファイルが置いてある前提。

使っているBoxはCentOS7.0

wapのSELinuxとかfirewallはオフにしちゃってます。
オフにしちゃうのが気になる場合は適切に設定する。

~/Vagrant/Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.ssh.insert_key = false
  config.vm.box = "centos70"

  config.vm.define 'wap01' do |wap01|
      wap01.vm.hostname = 'wap01'
      wap01.vm.network "private_network", ip: "172.31.41.21"
      wap01.vm.network :public_network, ip: "192.168.1.200", :bridge => "en0: Wi-Fi (AirPort)" ,:use_dhcp_assigned_default_route => true
      wap01.vm.synced_folder "~/Vagrant/data/wap/", "/host_mount", :nfs => { mount_options: ['dmode=777', 'fmode=777'] }
      wap01.bindfs.bind_folder "/host_mount","/var/apps", :owner => "vagrant", :group => "vagrant", :'create-as-user' => true, :perms => "u=rwx:g=rwx:o=rwx", :'create-with-perms' => "u=rwx:g=rwx:o=rwx", :'chown-ignore' => true, :'chgrp-ignore' => true, :'chmod-ignore' => true
  end

  config.vm.define 'master01' do |master01|
      master01.vm.hostname = 'master01'
      master01.vm.network "private_network", ip: "172.31.41.11"
      master01.vm.synced_folder "~/Vagrant/data/master/", "/host_mount", :nfs => { mount_options: ['dmode=777', 'fmode=777'] }
      master01.bindfs.bind_folder "/host_mount","/var/apps", :owner => "vagrant", :group => "vagrant", :'create-as-user' => true, :perms => "u=rwx:g=rwx:o=rwx", :'create-with-perms' => "u=rwx:g=rwx:o=rwx", :'chown-ignore' => true, :'chgrp-ignore' => true, :'chmod-ignore' => true
  end
end

dnsmasq

ドメイン名でアクセスできるようにする
※ これやらなくても、おそらく同じネットワーク内にスマホをwifi接続していれば、http://192.168.1.200 でつながる。

インストール(Mac)

brew install dnsmasq
sudo vim /etc/resolv.dnsmasq.conf
sudo echo “address=/local.hoge.jp/192.168.1.200” > /usr/local/etc/dnsmasq.conf 
sudo echo "resolv-file=/etc/resolv.dnsmasq.conf" >> /usr/local/etc/dnsmasq.conf
# Mac起動時に自動でdnsmasqも起動するようにする
sudo cp `brew --prefix dnsmasq`/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
# 設定を読み込み
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
# アンロードは以下のコマンド
sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
/etc/resolv.dnsmasq.conf
# local.hoge.jp以外を解決するDNSサーバ
nameserver 192.168.1.1

スマホの設定

スマホを同じネットワークにあるルーターに接続する

接続しているWifiの設定のDNSを192.168.1.200(自分のIP)に変更する

16
18
0

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
16
18