【问题标题】:Vagrant - PhpStorm - Laravel - HTTP Where can I access my local website?Vagrant - PhpStorm - Laravel - HTTP 我在哪里可以访问我的本地网站?
【发布时间】:2015-12-24 16:10:24
【问题描述】:

我是 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 没有成功。

我想要的是类似的“站点映射”体验,就像您编辑 Homestead.yaml 文件时一样。加上什么应该是默认方式?

【问题讨论】:

    标签: vagrant vagrantfile homestead


    【解决方案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
    

    如果文件夹没有“同步”,运行

    vagrant halt
    

    重启 vagrant

    然后

    vagrant up
    

    【讨论】:

    • 我特别想要宅基地,所以我会保持原样。我会在测试您的答案后尽快回复您。谢谢。
    • 不,这也不起作用。文件夹同步没问题。另外,我认为这不应该是“正确的方式”。
    • 使用上面提供的Vagranfile,config.vm.box = "laravel/homestead" 安装成功了吗?我以前从未尝试过,我通常在每个项目中使用 UbuntuTrusty 并手动配置 vagrant 机器(apache、mysql 等)。也许我会在以后的项目中尝试。
    • 干得好康斯坦丁诺斯·迪马基斯。我在我的一些项目中使用 apache2,laravel homestead 预先配置了 nginx..在我的 .vagrant.d 里面,我有这两个盒子,ubuntutrusty 和 laravel/homestead。
    【解决方案2】:

    您需要将虚拟机内的端口转发到主机。 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

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

    【讨论】:

    • 非常感谢您的快速答复。将首先尝试默认值。我会回复你的。
    • 不,这似乎不起作用。 :/谢谢。如果在我的工作区关闭,也许还有别的东西。顺便说一句,我确实在端口 80 上的 localhost 上运行了 apache2。此外,我尝试了 Laravel 文档的说明并且它有效,但它与 Vagrantfile 无关,它是 Homestead.yaml 文件。但我需要这个才能以类似的方式通过 Vagrantfile 工作。
    【解决方案3】:

    我错过了laravel documentation 中的重要一步。设置 vagrant box 的正确方法是从 laravel/homestead 仓库。

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

    【讨论】:

      猜你喜欢
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 2014-08-19
      • 2020-10-15
      • 2019-08-22
      相关资源
      最近更新 更多