vagrantでLaravelウェブアプリを作る(1)

Vagrantでnodeを切って作っていく」

 

Vagrant 事始め 04 - Vagrant のネットワーク設定 - Qiita
( https://qiita.com/centipede/items/64e8f7360d2086f4764f )
vagrantのネットワークについて - Qiita
( https://qiita.com/ftakao2007/items/0ec05c2ef3c14cdbea11 )

を参考に

 

config.vm.define :node1 do |node|
config.vm.network "private_network", ip: "192.168.33.20"
config.vm.network "public_network", ip: "192.168.16.7", bridge: "イーサネット"
end

config.vm.define :node2 do |node|
config.vm.network "private_network", ip: "192.168.33.50"
config.vm.network "public_network", ip: "192.168.16.7", bridge: "イーサネット"
end

 

を追記した。

vagrant upで躓く。

 

Bringing machine 'node2' up with 'virtualbox' provider...
==> node1: [vagrant-hostsupdater] Checking for host entries
==> node1: Checking if box 'generic/ubuntu2010' version '3.2.18' is up to date...
~~~
config.vm.network :forwarded_port, guest: 8888, host: 1234

でポート変えろと言われる。
netstat | find 8880で見るも引っかからず。再度vagrant upしたら動いた。
すぐにやったから解放されるまでのタイムラグぽい

PS G:\SANDBOX\500dev\PHP\sandLaravel> vagrant up
Bringing machine 'node1' up with 'virtualbox' provider...
Bringing machine 'node2' up with 'virtualbox' provider...
~~
==> node1: Specific bridge 'イーサネット' not found. You may be asked to specify
==> node1: which network to bridge to.
==> node1: Available bridged network interfaces:
==> node1: When choosing an interface, it is usually the one that is
==> node1: being used to connect to the internet.
==> node1:
node1: Which interface should the network bridge to?

イーサネットっていうブリッジ見つからんけどって怒られる。
ブリッジ名を尋ねられるがどう答えて良いか分からず。

強制的に止めたら、vagrant upできないので

ps | grep vagrant
ps | grep ruby

 

 でUID検索したらrubyでひっかかったのでkillした。upはできたがまた同じことになった。参考は次のところ。

vagrant upでエラー - Qiita
( https://qiita.com/YusukeHigaki/items/f975e0b682ba54976a47 )


この辺を参考にするも分からず。

Vagrantで起動時のネットワークインターフェースを指定するには · THINKING MEGANE
( https://blog.monochromegane.com/blog/2013/05/22/vagrant-specify-network-interface/ )
Vagrant public_network を使用する - Why it doesn't work?
( https://www.whyit.work/entry/2018/06/30/225903 )

VirtualBoxのブリッジアダプターを使ってみる。 - Qiita
( https://qiita.com/hirooka0527/items/10821f5490738328b442 )

 
どうもVitualboxのブリッジ設定ができないことが問題のようなので
ここを参考にした
ブリッジアダプターが使えなくなった時の処方 [VirtualBox] | リネージュノケモノ道
( https://lineagewalker.blogspot.com/2019/06/VirtualBox-BridgeAdapter.html )
ホスト側のNICにがVBoxNetLwfなかったので、プログラムのVirtualboxからネットワークドライブのinfファイルを引っ張ってきて、nicに設定。
(参考記事が割と新しくて良かった。こういう記事は5年前のとかよくあるからなあ)

 

<該当の説明箇所>
正常な状態であれば画像にあるように,使用するクライアント・サービス・プロトコルの一覧に「VirtualBox NDIS6 Bridged Networking Driver」が存在している.

無効(チェックが外れている)になっているだけならチェックを入れるだけでよい.
無かった場合は[インストール]から「サービス」を[追加],製造元の選択肢に「Oracle Corporation」が無ければ[ディスク使用]から下記ファイルを指定してインストールする.
※ アドレスは VirtualBox をデフォルト設定のままインストールした時のものです.
C:\Program Files\Oracle\VirtualBox\drivers\network\netlwf\VBoxNetLwf.inf
なお,このインストール作業でネットから一時的に切断される.
VirtualBox の導入時にネットワークが切断される旨の注意書きがあるのは恐らくこの為で,逆にネット切断が発生しなかった場合は上述のドライバが正常に追加されていない可能性が高く,本件の症状が発生しているだろうと予想される. 

 
ここでOS再起動がいるらしいので一旦再起動。

Virtualboxの更新が来てたので6.1.22に更新。
Virtualboxのネットワークでブリッジアダプターを設定したら、今度は「名前」にNIC名が入っていた

f:id:kaidnu2:20210505034608p:plain

Virtualbox更新の影響は分からなくなってしまったが、結果オーライにした。

vagrant upしたが初回はどうしても8880で躓く。netstatで調べるも8880は使ってない。
再度upしたら走り出した。意味不明。

ここでvirtualbox更新したのを早くも忘れてしまいpluginの更新をしてないことに気づく・・・(本当はやっておくべき)


でnode1の分は status inactive 非活性になってしまったものの終えられた様子だがnode2のほうでポートが使われてると怒られる。これはnode1で使ってるからってことなのか?

node1: Processing triggers for systemd (246.6-1ubuntu1.3) ...
node1: Processing triggers for man-db (2.9.3-2) ...
node1: Processing triggers for libc-bin (2.32-0ubuntu3) ...
node1: Status: inactive
==> node2: Importing base box 'generic/ubuntu2010'...
==> node2: Matching MAC address for NAT networking...
==> node2: Checking if box 'generic/ubuntu2010' version '3.2.18' is up to date...
==> node2: [vagrant-hostsupdater] Checking for host entries
==> node2: Setting the name of the VM: sandLaravel_node2_1620154745723_79682
Vagrant cannot forward the specified ports on this VM, since they would collide with some other application that is already listening on these ports. The forwarded port to 8880 is already in use on the host machine.

あれこれしてて気づかなかったがVirtualbox側でこんなエラー。

f:id:kaidnu2:20210505040538p:plain

ExtensionPackには悪い思いでしかないので極力避けたいのだが・・・

調べものの中でこういう記事にぶつかる(しかも ストック済みだったw)

Vagrantで複数環境を立ち上げる際、IPアドレスの割当やポートフォワードを全て自動化する - Qiita ( https://qiita.com/jyokyoku/items/85b030537a5728c9d771 )

 独立して会社持てる人はこれくらいのこと出来て当然なんだろうけど 。

Virtualbox側を見ると、nodeごとに作られているのがわかる。こうなるのか

f:id:kaidnu2:20210505041915p:plain

なるほどこれなら8880が既にあると怒られるのも無理はない。ってことはnodeごとにポートフォワーディング設定できるはずってこと?
とりあえずhaltして止める

PS G:\SANDBOX\500dev\PHP\sandLaravel> vagrant halt
==> node2: [vagrant-hostsupdater] Removing hosts on suspend
==> node1: [vagrant-hostsupdater] Removing hosts on suspend
==> node1: Attempting graceful shutdown of VM...
PS G:\SANDBOX\500dev\PHP\sandLaravel>

 ポートフォワーディングの書き方が間違っているぽいので次のように修正

config.vm.define :node1 do |node|
config.vm.network "forwarded_port", guest: 80, host: 8880, id:"http"
config.vm.network "forwarded_port", guest: 8888, host: 8888, id:"http"
config.vm.network "forwarded_port", guest: 443, host: 8443, id:"https"
config.vm.network "private_network", ip: "192.168.33.20"
config.vm.network "public_network", ip: "192.168.16.17", bridge: "en2: Intel(R) I211 Gigabit Network Connection"
end
config.vm.define :node2 do |node|
config.vm.network "forwarded_port", guest: 80, host: 8880, id:"http"
config.vm.network "forwarded_port", guest: 8888, host: 8888, id:"http"
config.vm.network "forwarded_port", guest: 443, host: 8443, id:"https"
config.vm.network "private_network", ip: "192.168.33.50"
config.vm.network "public_network", ip: "192.168.16.17", bridge: "en2: Intel(R) I211 Gigabit Network Connection"
end

これでupすると案の定というか

Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 8888 is already in use
on the host machine.

となったので

config.vm.define :node1 do |node|
config.vm.network "forwarded_port", guest: 80, host: 8880, id:"http"
config.vm.network "forwarded_port", guest: 8888, host: 8888, id:"http"
config.vm.network "forwarded_port", guest: 443, host: 8443, id:"https"
config.vm.network "private_network", ip: "192.168.33.20"
config.vm.network "public_network", ip: "192.168.16.17", bridge: "en2: Intel(R) I211 Gigabit Network Connection"
end

config.vm.define :node2 do |node|
config.vm.network "private_network", ip: "192.168.33.50"
config.vm.network "public_network", ip: "192.168.16.17", bridge: "en2: Intel(R) I211 Gigabit Network Connection"
end

 とした。

would collide with some other application that is already listening on these ports. The forwarded port to 8888 is already in use on the host machine.

To fix this, modify your current project's Vagrantfile to use another port. Example, where '1234' would be replaced by a unique host port: config.vm.network :forwarded_port, guest: 8888, host: 1234

 結局同じ結果。なので

config.vm.define :node1 do |node|
config.vm.network "private_network", ip: "192.168.33.20"
config.vm.network "public_network", ip: "192.168.16.17", bridge: "en2: Intel(R) I211 Gigabit Network Connection"
end

config.vm.define :node2 do |node|
config.vm.network "private_network", ip: "192.168.33.50"
config.vm.network "public_network", ip: "192.168.16.17", bridge: "en2: Intel(R) I211 Gigabit Network Connection"
end

 結局参考サイトとほぼ同じ内容にしてみた。結果・・

PS G:\SANDBOX\500dev\PHP\sandLaravel> vagrant global-status --all
G:/SANDBOX/500dev/PHP/sandLaravel
node1 running (virtualbox) 2021-05-05 05:03:31 +0900
node2 running (virtualbox) 2021-05-05 05:05:08 +0900

f:id:kaidnu2:20210505050709p:plain

ポートフォワーディング設定を見るとこうなっていた。やっぱり

node1: 22 (guest) => 2222 (host) (adapter 1)
node1: SSH address: 127.0.0.1:2222
 ==> node2: Fixed port collision for 22 => 2222. Now on port 2200.
  node2: SSH address: 127.0.0.1:2200

ホスト側のvagrant/machinesもnodeごとに作成されていた

f:id:kaidnu2:20210505051841p:plain

いちおう出来たものの、ここはゴール地点ではない。
あと今のvagrantfile設定だとsshログイン後、vagrant ssh node1またはnode2で個別にログインできるもののどっちに入っているのか判別しづらい。
確かvagrantfileで設定できたと思うのだが…

 

ここにPHPやnodeやら入れてないのでこれらを入れてからやれてない事を入れていく。

とりあえず長くなったのでこの章はここまでにする。

次回
vagrantでLaravelベースウェブアプリを作る(2)へ