This quick tip will show you how to get a working Homestead Vagrant Box up and running in no time. Note, this quick tip is based on homestead_improved, my own version of Homestead which doesn’t ask you to deal with SSH keys and automatically forwards port 5000 in order to accommodate Heroku’s Foreman tool.
Relevant articles (optional additional reading):
Step 1 – prerequisites
Make sure you have the newest Vagrant and VirtualBox installed for your operating system.
If on Windows, also install Git Bash via git tools.
Step 2 – cloning
Using a command line (terminal in *nix, git bash on Windows),
cd into the folder where you’d like your virtual box to run from. For example, I use Windows as my host OS and have all my Vagrant things on
Run the following command ($DIR is the name of the directory you want your VM to reside in, e.g. “Homestead”):
git clone https://github.com/Swader/homestead_improved $DIR
Step 3 – set up sites and folders
Homestead.yaml and change your “folders” section and/or your “sites” section.
“sites” adds new virtual hosts for Nginx, so if you need to access multiple apps on one VM, add them here:
sites: - map: myapp.com to: /home/vagrant/Code/myapp/public - map: myapp2.com to: /home/vagrant/Code/myapp2/public
Under “folders”, “map” is the location of the folder on the host machine and “to” is the location of the folder in the VM.
For example, if you keep your code in
/var/www/myapp and want to test/host it inside the VM, you could change the folders to this:
folders: - map: /var/www/myapp to: /home/vagrant/Code/myapp
To add more of these bindings, just add a new map-to block underneath the current one, like so:
folders: - map: /var/www/myapp to: /home/vagrant/Code/myapp - map: /var/www/myapp2 to: /home/vagrant/Code/myapp2
Note: This is no longer explicitly necessary, because by default HI will now map the current folder (the one the Vagrantfile is in) to the Code folder on the VM. This completely removes the need for changing the folders block, unless you keep your code somewhere else, not specific to this VM’s instance.
Step 4 – hosts
Open your host machine’s
hosts file and add a line for each of your added sites listed under the aforementioned
127.0.0.1 myapp.com 127.0.0.1 myapp2.com
For instructions on how to edit your hosts file on any given OS, see here.
Step 5 – vagrant up
vagrant up while inside
$DIR, and when the procedure is finished, run
vagrant ssh to enter the VM through SSH.
You can now access your sites through the browser on your host machine via
http://myapp2.com:8000/. You can connect to MySQL from the host machine’s Workbench or any other database management tool by selecting the following connection parameters:
- port: 33060
- host: 127.0.0.1
- username: homestead
- password: secret