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 | # going to home folder |
Now you will set the configuration:
- Set a vagrant box. Vagrant boxes are the package format for Vagrant environments. There are many boxes out there, you can find more here
- 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.
- Sync project folder to server folder. In this case, I’m mapping the ‘Web’ folder to Nginx virtual machine folder.
- 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 | Vagrant.configure("2") do |config| |
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.