vagrant rails server でブラウザに接続出来ない時の対処法

Ruby on RailsでCould not find a JavaScript runtimeが発生!

vagrant 環境で $ rails server -b 192.168.33.10 -d で ブラウザが表示されない事が起きましたので対処法を備忘録として残しておきます。

ruby のバージョンは下記の通りです

[vagrant@localhost myapp]$ ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

rails server で Could not find a JavaScript runtime. が発生。

[vagrant@localhost myapp]$ rails server -b 192.168.33.10 -d
class=”s1″>Traceback (most recent call last):
61: from bin/rails:3:in `<main>’
60: from bin/rails:3:in `load’
略 (この間長いログが・・・)
/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect’: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

Gemfileに追記

[vagrant@localhost myapp]$ vi Gemfile
以下を追記
gem ‘therubyracer’

その後bundle installをする

[vagrant@localhost myapp]$ bundle install

バックグランド -d で動いているのでプロセス番号を調べて kill します

[vagrant@localhost myapp]$ rails server -b 192.168.33.10 -d
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
A server is already running. Check /vagrant/rails_leesson/myapp/tmp/pids/server.pid.
[vagrant@localhost myapp]$ cat tmp/pids/server.pid
24694[vagrant@localhost myapp]$ kill -9 24694

192.168.33.10  でブラウザが表示されない

これでいいと思いローカルのアドレスを入力しましたが表示されません

アクセス出来ません

さて困ってしまいました。いろいろ調べて下記のようにたどり着きました。

Vagrantfile にポート番号を追加する

[vagrant@localhost vagrant]$ cat Vagrantfile
3000番ポートを開くように追加
config.vm.network :forwarded_port, guest: 3000, host: 3000
プライベートアドレス追記かコメントを外しているか確認
config.vm.network “private_network”, ip: “192.168.33.10”

[vagrant@localhost vagrant]$ exit
ログアウト

Vagrantfileのある位置で
$ vagrant reload

これでもダメだったのでサービスをストップしてみます

firewalld をストップする

firewalld をストップする
[vagrant@localhost myapp]$ sudo systemctl stop firewalld.service
maskすると、サービスの起動自体が不可能となり、コマンドを受け付けません。
[vagrant@localhost myapp]$ sudo systemctl mask firewalld.service
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
起動時もfirewalldが起動しないようにする
[vagrant@localhost myapp]$ sudo systemctl list-unit-files | grep firewalld
firewalld.service masked
[vagrant@localhost myapp]$ rails server -b 192.168.33.10 -d
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options

192.168.33.10:3000    (http://192.168.33.10:3000/)でアドレス打ってみます。

無事に表示されました!

Yay!You're on Rails!画像

@Ago0727 さんのブログ参考にさせて頂きました。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください