Using vagrant and homestead for multiple sites and per project installation
Asked Answered
H

5

35

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.

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

  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 Homestead directory with the bash init.sh script
  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

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 up from Project A I can access http://projecta.app and http://projectb.app times out. The same the other way around, sadly.

This is my vagrant global-status

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

I thought I would just do another vagrant up from the projectB directory but that doesn't work of course.

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

I didn't modify the Homestead.yaml except of the IP and the domainname 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?

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

Held answered 30/6, 2015 at 13:13 Comment(2)
I'm not too familiar with Homestead, but I do have Vagrant running ubuntu images with multiple sites. Perhaps you should add the vhost configuration to the Homestead.yaml file too. (Example here). How do you try to connect to the homestead, through localhost:someforwardedport (maybe they forward to the same port?) or directly to their IP addresses with entries in your local hosts file too? (so not on your vagrant instance)Tavern
I have added the entries in sites but Nginx has not enabled so I have to manually enable it. Can you give a way to auto enable it with just adding the entries in yaml fileDebenture
B
46

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.

Baynebridge answered 1/7, 2015 at 7:17 Comment(6)
Hi, jeyp, that's what I have done now. Using one global Homestead installation for multiple projetcs. I upvoted and accepted your answer anyway for your effort :pHeld
I have added the entries in sites but Nginx has not enabled so I have to manually enable it.Debenture
How can I set a different port for each site?Hannus
I'm afraid that the current version of Homestead does not allow to do that config from the configuration file. You have to SSH to the virtual machine and update the Nginx configuration.Baynebridge
Manually updating the hosts file is no longer required. Homestead is now compatible with the vagrant-hostsupdater plugin. Just run vagrant plugin install vagrant-hostsupdater from the Homestead directory.Checky
"$ vagrant up --provision" - is needed.Finedrawn
D
13

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.
Debenture answered 30/7, 2015 at 10:25 Comment(1)
Number 2 is an unnecessary and irrelevant step. The only vagrant file you need in a global homstead setup is in the homestead directory.Skate
C
1

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.

Clericalism answered 23/8, 2015 at 16:17 Comment(1)
OK but does this allow to run both VMs at the time ?Leitmotif
V
0

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

Virgo answered 3/5, 2016 at 0:36 Comment(0)
S
0

Step 1: First you just map your other project and database in the homestead.yaml file enter image description here.

Step 2: Create the database you mentioned in the existing workbench connection enter image description here.

Step 3: Mention that database in your project .env file. enter image description here

Step 4: Add another path in your drivers/etc/hosts file and run homestead reload --provision. Your problem is solved. :)enter image description here.

Storied answered 1/3, 2022 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.