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

Vagrant - PhpStorm - Laravel - HTTP Where can I access my local website?

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

我用这个 Vagrantfile 启动了 vagrant:

# -*- 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 但没有成功。

我想要的是类似"site mapping"的体验,就像编辑Homestead.yaml文件一样。另外默认方式应该是什么?

您需要将端口从 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,所以我将它们转发到主机上的类似端口(以避免冲突)。

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

Looking into the Laravel documentation,您需要确保以下端口可用:

  • SSH: 2222 → 转发到 22
  • HTTP: 8000 → 转发到 80
  • HTTPS:44300 → 转发到 443
  • MySQL: 33060 → 转发到 3306
  • Postgres:54320 → 转发到 5432

不过,文档暗示这是默认完成的,所以您可能想先尝试使用它们来确定。

在您的主机中创建一个类似

的文件夹
  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

如果文件夹没有"sync"、运行

vagrant halt

重启 vagrant

然后

vagrant up

好吧,我错过了 laravel documentation 中的关键一步。设置你的 vagrant box 的正确方法是克隆 "setup" 文件 laravel/homestead 存储库。

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