Using vagrant and homestead for multiple sites and per project installation
Solution 1
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, runbash 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 theinit.sh
file).
Now, you can run as many project as you want with just one Homestead virtual machine.
Solution 2
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.
-
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 usingMAMP
or any non-vagrant solution. - run
vagrant init laravel/homestead
. This command will add the necessaryVagrantFile
in this new project. - open the directory of your first original project file and open its
Homestead.yaml
file in editor. -
Now follow the steps defined by @Hieu Le in accepted answer to modify
.yaml
filefolders: - 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
- On terminal
cd
into your first original original project directory. - 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. - Run
vagrant ssh
- Run
ls
and make sure you can see the folder of your new project. If its there you have configured your new site correctly. - Hit the url of new site with addition of
http://
and your are DONE.
Solution 3
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.
Musterknabe
Updated on May 03, 2020Comments
-
Musterknabe about 4 years
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
- Installing VirtualBox
- Installing Vagrant
- Adding homestead with
vagrant box add laravel/homestead
- Clonging the repository
git clone https://github.com/laravel/homestead.git Homestead
- Create Homestead.yaml file in the
Homestead
directory with thebash init.sh
script - Create a new project
laravel new projectA
- Require homestead
composer require laravel/homestead
- Generate Vagrantfile
php vendor/bin/homestead make
- Modify the Homestead.yaml to have an IP that ends with 10
- Create another project
laravel new projectB
- Require homestead
composer require laravel/homestead
- Generate Vagrantfile
php vendor/bin/homestead make
- Modify the Homestead.yaml to have an IP that ends with 11
- Adding those two sites to the hosts file
sudo nano /etc/hosts
=>xxx.xx.xx.10 projecta.app
&xxx.xx.xx.11 projectb.app
- 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 callvagrant up
from Project A I can accesshttp://projecta.app
andhttp://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 theprojectB
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 domainnamehomestead.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.