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: Homestead Improved is configured to automatically set your current folder as shared with the
/Code folder inside your VM, so you generally don’t need to change the “folders” block. However, some OSs fail to recognize “.” as “current folder” and will throw errors during provisioning (Windows <8 and Mac OS X) so you’ll have to change the “.” to an absolute path to the folder you want to share, as described above. To map the current folder to this location, you can use this simple command:
sed -i '' "s@map\: \.@map\: $PWD@g" Homestead.yaml
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