Vagrant ubuntu/trusty64 未在 mac 上进行端口转发
Vagrant ubuntu/trusty64 not port forwarding on mac
我正在尝试在 Vagrant 中设置一个简单的 Rails 应用程序。这是我第一次设置 Vagrant+rails。这是 Vagrant 文件。如您所见,我正在将 localhost:3000 映射到 guest:3000。
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network 'forwarded_port', guest: 3000, host: 3000
config.vm.provision 'shell', path: 'bootstrap/bootstrap_vagrant.sh'
end
这是我的 bootstrap 文件,为简洁起见,我将其删除:
#!/usr/bin/env bash
set -e
apt-get update
echo "LC_ALL=\"en_US.UTF-8\"" >> /etc/default/locale
locale-gen en_US.UTF-8
update-locale LANG=en_US.UTF-8
apt-get -y install build-essential curl git-core python-software-properties htop vim
apt-get -y install nodejs # needed by Rails to have a Javascript runtime
apt-get -y install zlib1g-dev libssl-dev libreadline6-dev libyaml-dev libncurses5-dev libxml2-dev libxslt-dev
sudo -u vagrant printf 'cd /vagrant\n' >> /home/vagrant/.profile
su vagrant -c "mkdir -p /home/vagrant/downloads; cd /home/vagrant/downloads; \
wget --no-check-certificate https://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz; \
tar -xvf ruby-2.1.5.tar.gz; cd ruby-2.1.5; \
mkdir -p /home/vagrant/ruby; \
./configure --prefix=/home/vagrant/ruby --disable-install-doc; \
make; make install;"
sudo -u vagrant printf 'export PATH=/home/vagrant/ruby/bin:$PATH\n' >> /home/vagrant/.profile
su vagrant -c "/home/vagrant/ruby/bin/gem install bundler --no-ri --no-rdoc"
su vagrant -c "/home/vagrant/ruby/bin/gem install rails"
apt-get -y install libsqlite3-dev
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle;"
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle exec rake db:create;"
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle exec rake db:setup;"
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle exec rake db:seed;"
在重新加载 vagrant 环境时,端口转发似乎正在工作:
==> default: Forwarding ports...
default: 3000 => 3000 (adapter 1)
default: 22 => 2222 (adapter 1)
设置完成后,我通过 ssh 连接到 vagrant box,并创建了一个 rails 应用程序,然后通过 运行ning bundle exec rails s.
如果我通过 ssh 连接到 vagrant 环境,并且从我的笔记本电脑(主机环境)运行 curl http://localhost:3000, I see the Rails welcome page. So the rails server is running fine. But if I go to http://localhost:3000,它不会加载。通过 curl 命中它也无济于事。
$ curl http://localhost:3000
curl: (52) Empty reply from server
我的主机环境是OSX 10.9.5。 (小牛队)。首选项中禁用了防火墙。
您的开发服务器没有监听正确的 IP 地址,这就是您可以从 Vagrant guest 内部访问它的原因(localhost
、127.0.0.1
- rails server
的默认绑定) ,但不是来自外部(您的笔记本电脑 - 主机)
让您的开发服务器监听您的可公开访问的界面。
为了使服务器监听所有接口,将其绑定到 IP 0.0.0.0
:
bin/rails server -b 0.0.0.0
我正在尝试在 Vagrant 中设置一个简单的 Rails 应用程序。这是我第一次设置 Vagrant+rails。这是 Vagrant 文件。如您所见,我正在将 localhost:3000 映射到 guest:3000。
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network 'forwarded_port', guest: 3000, host: 3000
config.vm.provision 'shell', path: 'bootstrap/bootstrap_vagrant.sh'
end
这是我的 bootstrap 文件,为简洁起见,我将其删除:
#!/usr/bin/env bash
set -e
apt-get update
echo "LC_ALL=\"en_US.UTF-8\"" >> /etc/default/locale
locale-gen en_US.UTF-8
update-locale LANG=en_US.UTF-8
apt-get -y install build-essential curl git-core python-software-properties htop vim
apt-get -y install nodejs # needed by Rails to have a Javascript runtime
apt-get -y install zlib1g-dev libssl-dev libreadline6-dev libyaml-dev libncurses5-dev libxml2-dev libxslt-dev
sudo -u vagrant printf 'cd /vagrant\n' >> /home/vagrant/.profile
su vagrant -c "mkdir -p /home/vagrant/downloads; cd /home/vagrant/downloads; \
wget --no-check-certificate https://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz; \
tar -xvf ruby-2.1.5.tar.gz; cd ruby-2.1.5; \
mkdir -p /home/vagrant/ruby; \
./configure --prefix=/home/vagrant/ruby --disable-install-doc; \
make; make install;"
sudo -u vagrant printf 'export PATH=/home/vagrant/ruby/bin:$PATH\n' >> /home/vagrant/.profile
su vagrant -c "/home/vagrant/ruby/bin/gem install bundler --no-ri --no-rdoc"
su vagrant -c "/home/vagrant/ruby/bin/gem install rails"
apt-get -y install libsqlite3-dev
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle;"
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle exec rake db:create;"
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle exec rake db:setup;"
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle exec rake db:seed;"
在重新加载 vagrant 环境时,端口转发似乎正在工作:
==> default: Forwarding ports... default: 3000 => 3000 (adapter 1) default: 22 => 2222 (adapter 1)
设置完成后,我通过 ssh 连接到 vagrant box,并创建了一个 rails 应用程序,然后通过 运行ning bundle exec rails s.
如果我通过 ssh 连接到 vagrant 环境,并且从我的笔记本电脑(主机环境)运行 curl http://localhost:3000, I see the Rails welcome page. So the rails server is running fine. But if I go to http://localhost:3000,它不会加载。通过 curl 命中它也无济于事。
$ curl http://localhost:3000
curl: (52) Empty reply from server
我的主机环境是OSX 10.9.5。 (小牛队)。首选项中禁用了防火墙。
您的开发服务器没有监听正确的 IP 地址,这就是您可以从 Vagrant guest 内部访问它的原因(localhost
、127.0.0.1
- rails server
的默认绑定) ,但不是来自外部(您的笔记本电脑 - 主机)
让您的开发服务器监听您的可公开访问的界面。
为了使服务器监听所有接口,将其绑定到 IP 0.0.0.0
:
bin/rails server -b 0.0.0.0