Using vagrant and homestead for multiple sites and per project installation

后端 未结 4 1924
星月不相逢
星月不相逢 2020-12-23 12:04

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, th

相关标签:
4条回答
  • 2020-12-23 12:22

    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...

    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

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

    Add another project in that Folder laravel new homestead.app

    Then

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

    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

    Then Edit your etc/hosts file as Administrator

    What ever ip address you define in your Homestead.yaml

    usually the default is 192.168.10.10

    Use it instead of 127.0.0.1

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

    If you Use the IP address in the Homestead.yaml

    192.168.10.10 homestead.app

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

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

    This is what i Do

    0 讨论(0)
  • 2020-12-23 12:25

    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 all of virtual machine. You can only run one machine (so, one project) each time.

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

    • Clone Homestead git clone https://github.com/laravel/homestead.git Homestead
    • Inside the Homestead folder, run bash init.sh

    Edit the folders property of ~/.homestead/Homestead.yaml to share your code of both projects with VM:

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

    Edit the sites property of ~/.homestead/Homestead.yaml to make Nginx enable the domain of both site:

    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
    
    • Run vagrant up at the folder that you cloned the Homestead code inside it (which contains the init.sh file).

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

    0 讨论(0)
  • 2020-12-23 12:28

    Like how here says, you can install Homestead directly into your project, require it using this composer require laravel/homestead --dev at root directory of each project you have. Now by make command you can generate Vagrantfile and Homestead.yaml file into your project's root directory.

    • Mac/Linux:

      php vendor/bin/homestead make
      
    • Windows:

      vendor\bin\homestead make
      

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

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

    Add this to /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 ssh from each project, you will have that project in your VM environment.

    0 讨论(0)
  • 2020-12-23 12:37

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

    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.

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

      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 cd into 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 .yaml file come in effect. You database of original project will remain intact.
    7. Run vagrant ssh
    8. Run ls and 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.
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题