php 将 vagrant 和 homestead 用于多个站点和每个项目的安装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31139284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:16:46  来源:igfitidea点击:

Using vagrant and homestead for multiple sites and per project installation

phplaravelvagranthomestead

提问by Musterknabe

I have been using XAMPP for quite a time, and after discovering Laravel and finding out, that I quite like it, I also wanted to use Homestead. The problem I'm having is, that I can't seem to be able to run multiple sites.

我使用 XAMPP 已经有一段时间了,在发现 Laravel 并发现我非常喜欢它之后,我也想使用 Homestead。我遇到的问题是,我似乎无法运行多个站点。

I have tried various things, but the main problem currently is, that one project works, while all the others are getting a connection timeout, when trying to access their webpage.

我尝试了各种方法,但目前的主要问题是,当尝试访问他们的网页时,一个项目有效,而所有其他项目都出现连接超时。

These are the steps I've taken to use Homestead

这些是我使用 Homestead 所采取的步骤

  1. Installing VirtualBox
  2. Installing Vagrant
  3. Adding homestead with vagrant box add laravel/homestead
  4. Clonging the repository git clone https://github.com/laravel/homestead.git Homestead
  5. Create Homestead.yaml file in the Homesteaddirectory with the bash init.shscript
  6. Create a new project laravel new projectA
  7. Require homestead composer require laravel/homestead
  8. Generate Vagrantfile php vendor/bin/homestead make
  9. Modify the Homestead.yaml to have an IP that ends with 10
  10. Create another project laravel new projectB
  11. Require homestead composer require laravel/homestead
  12. Generate Vagrantfile php vendor/bin/homestead make
  13. Modify the Homestead.yaml to have an IP that ends with 11
  14. Adding those two sites to the hosts file sudo nano /etc/hosts=> xxx.xx.xx.10 projecta.app& xxx.xx.xx.11 projectb.app
  15. Starting vagrant from one of the two directories vagrant up
  1. 安装 VirtualBox
  2. 安装流浪汉
  3. 添加宅基地 vagrant box add laravel/homestead
  4. 克隆存储库 git clone https://github.com/laravel/homestead.git Homestead
  5. Homestead使用bash init.sh脚本在目录中创建 Homestead.yaml 文件
  6. 创建一个新项目 laravel new projectA
  7. 需要宅基地 composer require laravel/homestead
  8. 生成 Vagrantfile php vendor/bin/homestead make
  9. 修改 Homestead.yaml 为 IP 以 10 结尾
  10. 创建另一个项目 laravel new projectB
  11. 需要宅基地 composer require laravel/homestead
  12. 生成 Vagrantfile php vendor/bin/homestead make
  13. 修改 Homestead.yaml 为 IP 以 11 结尾
  14. 将这两个站点添加到主机文件sudo nano /etc/hosts=> xxx.xx.xx.10 projecta.app&xxx.xx.xx.11 projectb.app
  15. 从两个目录之一启动 vagrant vagrant up

Now, I'm having the problem, that only one of the projects is reachable. It's always the one from where I called vagrant up. So if I call vagrant upfrom Project A I can access http://projecta.appand http://projectb.apptimes out. The same the other way around, sadly.

现在,我遇到了问题,只有一个项目可以访问。它总是从我打电话的地方vagrant up。因此,如果我vagrant up从 Project AI调用可以访问http://projecta.apphttp://projectb.app超时。可悲的是,反过来也是一样。

This is my vagrant global-status

这是我的 vagrant global-status

id       name    provider   state   directory                           
------------------------------------------------------------------------
fc6fadb  default virtualbox running /Users/mknb/work/projectA

I thought I would just do another vagrant upfrom the projectBdirectory but that doesn't work of course.

我以为我会vagrant upprojectB目录中做另一个,但这当然行不通。

I don't want to use the global Homestead, because Laravel said, that it is possible to have a per projectinstallation, so how do I achieve it? Do you need more information?

我不想用全局的Homestead,因为Laravel说可以有per project安装,那我怎么实现呢?你需要更多信息?

I didn't modify the Homestead.yamlexcept of the IP and the domainname homestead.app=> projecta.app

Homestead.yaml除了IP和域名我没有修改homestead.app=>projecta.app

It seems like a global installation is fairly easy with Homestead, since I would just have to add more sites to the Homestead.yaml, but as I said I'd like to have a per project installation. Can anybody help?

似乎使用 Homestead 进行全局安装相当容易,因为我只需要向 Homestead.yaml 添加更多站点,但正如我所说,我希望每个项目都安装一个。有人可以帮忙吗?

Starting vagrant from the Homestead directory doesn't work of course.

从 Homestead 目录启动 vagrant 当然不起作用。

回答by Hieu Le

By using Homestead in your way, you create a virtual machine for each projects. Therefore, the VirtualBox cannot forward the HTTP request from your host machine for allof virtual machine. You can only run one machine (so, one project) each time.

通过以您的方式使用 Homestead,您可以为每个项目创建一个虚拟机。因此,VirtualBox 无法为所有虚拟机转发来自您主机的 HTTP 请求。您每次只能运行一台机器(因此,一个项目)。

To run multiple projects with Homestead, you can do as follow:

要使用 Homestead 运行多个项目,您可以执行以下操作:

  • Clone Homestead git clone https://github.com/laravel/homestead.git Homestead
  • Inside the Homesteadfolder, run bash init.sh
  • 克隆家园 git clone https://github.com/laravel/homestead.git Homestead
  • Homestead文件夹内,运行bash init.sh

Edit the foldersproperty of ~/.homestead/Homestead.yamlto share your code of both projects with VM:

编辑folders属性~/.homestead/Homestead.yaml以与 VM 共享两个项目的代码:

folders:
    - map: ~/pj1
      to: /path/to/project1
    - map: ~/pj2
      to: /path/to/project2

Edit the sitesproperty of ~/.homestead/Homestead.yamlto make Nginx enable the domain of both site:

编辑 的sites属性~/.homestead/Homestead.yaml以使 Nginx 启用两个站点的域:

sites:
    - map: project1.local
      to: /home/vagrant/pj1/public
    - map: project2.local
      to: /home/vagrant/pj2/public

Edit your hostsfile to forward these domain fo localhost

编辑您的hosts文件以将这些域转发到本地主机

127.0.0.1 project1.local
127.0.0.1 project2.local
  • Run vagrant upat the folder that you cloned the Homestead code inside it (which contains the init.shfile).
  • vagrant up在您克隆 Homestead 代码的文件夹中运行(其中包含init.sh文件)。

Now, you can run as many project as you want with just one Homestead virtual machine.

现在,您可以使用一台 Homestead 虚拟机运行任意数量的项目。

回答by hhsadiq

There are some important steps missing in the accepted answer although it helped me lot. I have added those necessary steps. Thanks @Hieu Lefor answer.

接受的答案中缺少一些重要的步骤,尽管它对我有很大帮助。我已经添加了那些必要的步骤。感谢@Hieu Le的回答。

I assume you have correctly installed your fist site as by the instructions of Laravel docs. Now you have another laravel site which you want to shift on vagrant. Follow the following steps.

我假设您已经按照Laravel 文档的说明正确安装了您的第一个站点。现在你有另一个 Laravel 站点,你想把它转移到 vagrant 上。请按照以下步骤操作。

  1. cdinto the directory of new Laravel project which you want to add. I assume you have all laravel files in it and its working using MAMPor any non-vagrant solution.
  2. run vagrant init laravel/homestead. This command will add the necessary VagrantFilein this new project.
  3. open the directory of your first original project file and open its Homestead.yamlfile in editor.
  4. Now follow the steps defined by @Hieu Lein accepted answer to modify .yamlfile

    folders:
         - map: ~/pj1
           to: /path/to/project1
         - map: ~/pj2
           to: /path/to/project2
    
    sites:
        - map: project1.local
          to: /home/vagrant/pj1/public
        - map: project2.local
          to: /home/vagrant/pj2/public
    

    Edit your hosts file to forward these domain fo localhost

    127.0.0.1 project1.local
    127.0.0.1 project2.local
    
  5. On terminal cdinto your first original original project directory.
  6. Run command vagrant reload --provision. This will reload the vagrant machine so that the changes which we made in .yamlfile come in effect. You database of original project will remain intact.
  7. Run vagrant ssh
  8. Run lsand make sure you can see the folder of your new project. If its there you have configured your new site correctly.
  9. Hit the url of new site with addition of http://and your are DONE.
  1. cd进入您要添加的新 Laravel 项目的目录。我假设您拥有所有 Laravel 文件及其使用MAMP或任何非流浪解决方案的工作。
  2. vagrant init laravel/homestead。此命令将VagrantFile在这个新项目中添加必要的。
  3. 打开第一个原始项目文件的目录并Homestead.yaml在编辑器中打开它的 文件。
  4. 现在按照@Hieu Le在接受的答案中定义的步骤修改.yaml文件

    folders:
         - map: ~/pj1
           to: /path/to/project1
         - map: ~/pj2
           to: /path/to/project2
    
    sites:
        - map: project1.local
          to: /home/vagrant/pj1/public
        - map: project2.local
          to: /home/vagrant/pj2/public
    

    编辑您的主机文件以将这些域转发到本地主机

    127.0.0.1 project1.local
    127.0.0.1 project2.local
    
  5. 在终端上cd进入您的第一个原始原始项目目录。
  6. 运行命令vagrant reload --provision。这将重新加载 vagrant 机器,以便我们在.yaml文件中所做的更改生效。您原始项目的数据库将保持不变。
  7. vagrant ssh
  8. 运行ls并确保您可以看到新项目的文件夹。如果在那里,您已经正确配置了新站点。
  9. 点击添加的新网站的网址,http://您就完成了。

回答by Soroush

Like how heresays, you can install Homestead directly into your project, require it using this composer require laravel/homestead --devat root directory of each project you have. Now by makecommand you can generate Vagrantfileand Homestead.yamlfile into your project's root directory.

就像这里所说的那样,您可以将 Homestead 直接安装到您的项目中,composer require laravel/homestead --dev在您拥有的每个项目的根目录中使用它。现在通过make命令您可以生成VagrantfileHomestead.yaml文件到您的项目的根目录中。

  • Mac/Linux:

    php vendor/bin/homestead make
    
  • Windows:

    vendor\bin\homestead make
    
  • Mac/Linux:

    php vendor/bin/homestead make
    
  • 视窗:

    vendor\bin\homestead make
    

On each project root you will have a Homestead.yamlfile to edit:

在每个项目根目录上,您将有一个Homestead.yaml要编辑的文件:

  • Project-A

    ip: "192.168.10.10"
    ...
    folders:
        - map: "~/Code/projecta"
          to: "/home/vagrant/projecta"
    sites:
        - map: project.a
          to: "/home/vagrant/projecta/public"
    
  • Project-B

    ip: "192.168.10.10"
    ...
    folders:
        - map: "~/Code/projectb"
          to: "/home/vagrant/projectb"
    sites:
        - map: project.b
          to: "/home/vagrant/projectb/public"
    
  • 项目-A

    ip: "192.168.10.10"
    ...
    folders:
        - map: "~/Code/projecta"
          to: "/home/vagrant/projecta"
    sites:
        - map: project.a
          to: "/home/vagrant/projecta/public"
    
  • 项目B

    ip: "192.168.10.10"
    ...
    folders:
        - map: "~/Code/projectb"
          to: "/home/vagrant/projectb"
    sites:
        - map: project.b
          to: "/home/vagrant/projectb/public"
    

Add this to /etc/hosts:

将此添加到/etc/hosts

    192.168.10.10 project.a
    192.168.10.10 project.b

Then you have to cd to each project's root and vagrant up. Now if you vagrant sshfrom each project, you will have that project in your VM environment.

然后你必须 cd 到每个项目的根目录和vagrant up. 现在,如果您vagrant ssh来自每个项目,那么您的 VM 环境中将拥有该项目。

回答by Midas Code

there is a short cut command to proxy the sites you want to add..

有一个快捷命令可以代理您要添加的站点。

without having to messed up your Homestead.yaml file and provision your vagrant box all over again...

无需弄乱您的 Homestead.yaml 文件并重新配置您的流浪箱......

This applies to BOTH GLOBAL AND PER PROJECT INSTALLATION

这适用于全球和每个项目的安装

Just Make sure if you are adding another project...

只要确保您要添加另一个项目...

You add it the (whole project) on your Shared Folder Declared in your Homestead.yaml

您将(整个项目)添加到 Homestead.yaml 中声明的共享文件夹中

Assuming your shared folder is C:/Users/MYACCOUNT/Codes

假设您的共享文件夹是 C:/Users/MYACCOUNT/Codes

Add another project in that Folder laravel new homestead.app

在该文件夹中添加另一个项目 laravel new homestead.app

Then

然后

Assuming your are ssh in your Homestead Type a.) if your using nginx

假设您在 Homestead 类型 a.) 中使用 ssh,如果您使用 nginx

serve homestead.app /home/Vagrant/Code/homestead/public

serve homestead.app /home/Vagrant/Code/homestead/public

b.) if your using hhvm serve-hhvm homestead.app /home/Vagrant/Code/homestead/public Just change your domain name and path to public folder of your project

b.) 如果您使用 hhvm serve-hhvm homestead.app /home/Vagrant/Code/homestead/public 只需更改您的域名和项目公共文件夹的路径

Then Edit your etc/hosts file as Administrator

然后以管理员身份编辑您的 etc/hosts 文件

What ever ip address you define in your Homestead.yaml

您在 Homestead.yaml 中定义的任何 ip 地址

usually the default is 192.168.10.10

通常默认为 192.168.10.10

Use it instead of 127.0.0.1

使用它代替 127.0.0.1

Why? because if you use 127.0.0.1 your url will look like homestead.app:8000

为什么?因为如果你使用 127.0.0.1 你的 url 看起来像 homestead.app:8000

If you Use the IP address in the Homestead.yaml

如果您使用 Homestead.yaml 中的 IP 地址

192.168.10.10 homestead.app

192.168.10.10 homestead.app

you can access your site without port 8000 and just use homestead.app

您可以在没有端口 8000 的情况下访问您的网站,只需使用 homestead.app

This Solution is Much Better than Provision... And is Faster...

此解决方案比 Provision 好得多……而且速度更快……

This is what i Do

这就是我所做的