Vagrant for PHP development environment
What is Vagrant?
I asked this question before, I think is about 1 year+ ago. Many times, I search for it, but couldn’t understand how it work.
Until recently, I found that Laravel also recommended it. I tried and eventually I found it really useful!
Configure now
First of all, you will need a ssh key, Vagrant & VirtualBox.
Generate ssh key
1 | $ ssh-keygen -t rsa -b 4096 -C "username@example.com" |
Navigate to your development folder, e.g. cd ~/dev
Clone the Laravel configuration for Vagrant (we’re refering to Laravel’s vagrant file)
1 | vagrant box add laravel/homestead |
Assuming that all projects are hosted on the same VM.
The configuration files stored in ~/.homestead
1 | cd ~/.homestead/ |
Edit the file Homestead.yaml, refer to Laravel doc for complete information.
There are few things you need to change
1 | folders: |
Just an example here, look at the section folders, it actually created a shared folder in the VM. ~/dev/web
is the local folder which keep all the web project (make sure it exists before you run vagrant up
), whereas the /home/vagrant/dev
will be inside the VM
For sites, is actually the virtual hosts for a particular project, in this example is to map laravel.homestead.com
to the project named laravel
Now you can create the VM
1 | cd ~/dev/homestead |
Now edit your /etc/hosts file, by adding the following line
1 | 192.168.10.10 laravel.homestead.com |
(the ip address was in your ~/.homestead/Homestead.yaml)
Create a new laravel project (if you don’t know how to install laravel, refer http://laravel.com/docs/5.0)
1 | cd ~/dev/web |
Now open up your browser, and type in the address laravel.homestead.com, and you will see the page!
You can remain your development in the host OS, but host the project in VM.
You can actually verify this by open a new terminal
1 | cd ~/dev/homestead |
Now you logged in to the VM
1 | cd ~/dev |
Go back to the previous terminal, create a dummy file
1 | $ touch ~/dev/web/new-file |
Switch back to the second terminal
1 | $ ll |
Means now you can create any other PHP projects beside Laravel in this environment as you like.
Enjoy yourself :)