Vagrant - PhpStorm - Laravel - HTTP我在哪里可以访问我的本地网站?

时间:2015-12-24 16:10:24

标签: vagrant vagrantfile homestead

我是Php / Laravel和VMs世界的新手。

我用这个Vagrantfile启动了流浪汉:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "laravel/homestead"

  # 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

  # 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"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder "./devpeople", "/home/vagrant/devpeople"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   sudo apt-get update
  #   sudo apt-get install -y apache2
  # SHELL
end

我已尝试使用文档中的vagrant命令进行说明,并修改Vagrantfile但未成功。

我想要的是类似的网站映射&#34; expirience,就像编辑Homestead.yaml文件一样。什么应该是默认的方式?

3 个答案:

答案 0 :(得分:1)

在主机中创建一个类似

的文件夹
  c:/projects/devpeople

像这样修改您的Vagrant文​​件

 config.vm.box = "ubuntu/trusty64"


 config.vm.network "forwarded_port", guest: 80, host: 8080

或者您可以像这样更改端口

 config.vm.network "forwarded_port", guest: 80, host: 8081

见下文

在虚拟机中创建一个文件夹

 vagrant up

 vagrant ssh

 cd  var/www

 mkdir devpeople

所以你的虚拟机应该有这样的文件夹

  /var/www/devpeople

然后您可以将项目放入主机

所以Vagrant同步文件夹看起来像这样

  config.vm.synced_folder "c:/projects/devpeople", "/var/www/devpeople"

运行     流浪汉

并访问您的开发网站

localhost:8081

如果文件夹没有&#34;同步&#34;,请运行

vagrant halt

重启流浪汉

然后

vagrant up

答案 1 :(得分:0)

您需要将VM中的端口转发到主机。 Here is an example from my Vagrantfile for Payara

config.vm.network :forwarded_port, guest: 4848, host: 4849
config.vm.network :forwarded_port, guest: 8080, host: 8081

因为我知道Payara默认使用端口4848和8080,我已将它们转发到我主机上的类似端口(以避免冲突)。

因此,如果我在运行http://localhost:4849后转到vagrant up,我将被重定向到VM中的端口4848,就像它在本地运行一样。

Looking into the Laravel documentation,看起来以下端口是您需要确保可用的端口:

  • SSH:2222→前往22
  • HTTP:8000→前往80
  • HTTPS:44300→前往443
  • MySQL:33060→前往3306
  • Postgres:54320→前锋至5432

文档暗示这是默认情况下完成的,因此您可能需要先尝试使用它们来确保。

答案 2 :(得分:0)

好吧,我错过了laravel documentation中的重要一步。设置流浪盒的正确方法是从中克隆“setup”文件 laravel / homestead repository。

因此,为了访问服务器,您只需编辑sites文件上的Homstead.yaml