Vagrant as Development Environment

Vagrant is a great tool for development without screw up your OS. It will create a virtual machine according to your need very fast.

Let me show you how to create a CentOS 7 environment with VirtualBox and Vagrant for PHP + MySQL development.

Installation

Vagrant supports several providers as VirtualBox, Hyper-V, VMware. I like to use VirtualBox because is powerful and free. You can download here.

After VirtualBox installed, you can download and install Vagrant. Link here.

The Vagrant File

You need to create a file with ‘Vagrantfile’ name in some folder. I created a CentOS folder just for organize it. Follow the commands bellow using terminal:

1
2
3
4
5
6
7
8
9
# going to home folder
$ cd

# creating folder for web projects and environment (organize!)
$ mkdir -p Projects/Web
$ mkdir -p Projects/Servers/CentOS

# creating vagrant file
$ touch Vagrantfile

Now you will set the configuration:

  1. Set a vagrant box. Vagrant boxes are the package format for Vagrant environments. There are many boxes out there, you can find more here
  2. Set the forward ports. In this case, you will set the web server port (80) to 8888 and mysql port (3306) to 3306, both on localhost.
  3. Sync project folder to server folder. In this case, I’m mapping the ‘Web’ folder to Nginx virtual machine folder.
  4. Set the CPU and memory’s virtual machine. I used the max CPU I have, but you can set the number according to your physical CPU and 1GB of memory is sufficient.
    Check the Vagrant file’s content:
1
2
3
4
5
6
7
8
9
10
11
12
13
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"

config.vm.network "forwarded_port", guest: 80, host: 8888
config.vm.network "forwarded_port", guest: 3306, host: 3306

config.vm.synced_folder "../../Web", "/var/www"

config.vm.provider "virtualbox" do |vb|
vb.cpus = 2
vb.memory = "1024"
end
end

Start up the environment

For start the environment, you will just run the following command in the Vagrantfile path:

1
$ vagrant up

Vagrant will create the virtual machine with the set configurantion and download the CentOS box. Then, it will start the machine and map the sync folder. Finally, the VM will be ready for SSH access.

Access the environment

For SSH access, run the following command:

1
$ vagrant ssh

Now you are accessing the machine with CentOS 7. The next post I will show you how to install LEMP (Nginx + PHP-FPM + MySQL) stack for start developing.