rails sコマンド実行後にlocalhost:3000に接続できない場合
/etc/sysconfig/iptables
ファイルに-A INPUT -p tcp -m tcp --dport 3000 -j ACCEPT
という記述を追記する。
$ sudo vi /etc/sysconfig/iptables $ sudo cat /etc/sysconfig/iptables # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m tcp --dport 3000 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT $ exit
仮想マシンからログアウトし、Vagrantfileを下記のように修正した。
- コメントアウト
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "forwarded_port", guest: 80, host: 8080
- 追記
config.vm.network :"forwarded_port", guest: 3000, host: 3000
# -*- mode: ruby -*- # vi: set ft=ruby : # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # All Vagrant configuration is done here. The most common configuration # options are documented and commented below. For a complete reference, # please see the online documentation at vagrantup.com. # Every Vagrant virtual environment requires a box to build off of. config.vm.box = "CentOS64" # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.network :"forwarded_port", guest: 3000, host: 3000 # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" (省略) end
Vagrantfileの修正を反映するため、vagrant reload
コマンドを実行する。
$ vagrant reload ==> default: Attempting graceful shutdown of VM... ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 3000 => 3000 (adapter 1) default: 22 => 2222 (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection timeout. Retrying... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Mounting shared folders... default: /vagrant => /Users/ponkiti/Vagrant/rails_lesson ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: to force provisioning. Provisioners marked to run always will still run. $ vagrant ssh
仮想マシンにSSH接続し、作成済みのアプリケーションディレクトリ(今回はfirst_app)に移動し、rails s
コマンドを再実行する。
$ cd rails_projects/first_app $ rails s => Booting WEBrick => Rails 4.0.5 application starting in development on http://0.0.0.0:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [2014-10-16 07:29:25] INFO WEBrick 1.3.1 [2014-10-16 07:29:25] INFO ruby 2.0.0 (2013-02-24) [x86_64-linux] [2014-10-16 07:29:25] INFO WEBrick::HTTPServer#start: pid=2020 port=3000
接続できた!
上記ページに接続できると、コマンドラインツール上に下記が表示される。
Started GET "/" for 10.0.2.2 at 2014-10-16 07:29:29 +0000 Processing by Rails::WelcomeController#index as HTML Rendered /home/vagrant/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.5/lib/rails/templates/rails/welcome/index.html.erb (2.4ms) Completed 200 OK in 43ms (Views: 42.9ms | ActiveRecord: 0.0ms)