Setting up development environment with Vagrant

Starting a new project web you may spend every time important amount of time setting up a new development environment. The operation is a routine but exist the risk that trying to migrate the project to another environment to not run as we expect as may be some differences between the environments, here comes Vagrant, platform independent, easy to setup and to deploy from one environment to another. Visit the Vagrant download page and select the operating system you are working with. The installation process doesn't require any extra configuration. To use Vagrant is quite easy, the most important piece is Vagrantfile file. This file contains the name of the image we want to use and the rest of configurations that we need to apply.

VAGRANTFILE_API_VERSION ="2"
  Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty32"
  config.vm.network : private_network, ip: "10.20.30.40"
  config.vm.network "forwarded_port", guest: 80, host: 80
  config.vm.provision "shell", path: "provisioner.sh"
end

The first line of the file specifies which API version is used, now is version 2. The image loaded is Ubuntu VM, the private network is setup to 10.20.30.40 and the webserver is listening at port 80. The provisioner.sh file contains additional configuration as setting up the PHP version 7, MySQL server, sets up the root account for MySQL, install and configure NGINX. The template file can be downloaded hereTo start the VM execute the command:

vagrant up

After couple of minutes the installation process is finished and we are ready to start our newly VM using the command:

vagrant ssh

Vagrant will start an SSH session to the newly created VM (exit using Ctrl+D). To access the root directory of the web server type http://10.20.30.40 address in any browser. The MySQL database can be managed through the command line:

mysql -h localhost -u root -p

or using any other utility (MySQLWorkbench, Adminer, SequelPro, etc). As example I will present the steps of how to connect to the MySQL database using MySQLWorkbench. First of all to get the information we need to configure our client must run in Vagrant SSH terminal the command:

vagrant ssh-config

The expected output:

Host default HostName 127.0.0.1 
User vagrant Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/User/..... 
IdentityFile /Users/User/..... 
IdentitiesOnly yes
LogLevel FATAL

Use the information up mentioned to connect to the MySQL server. (see the screenshot below).