When I write this topic, I thought that someones might knew vagrant and don't care about my sharing. However, I don't care too :)). After long time to works, I found some issues with my teammate and I wanna to share with all of you. In my company, someone starts with ubuntu and apache. After probation time, Our TAs require them using nginx. Another side, someone installed PHP version 7.0 or higher, some project runs with PHP 5.6 or some ticket you implemented worked on your local but not work on other servers...Therefore, I have a motivation to introduce with you vagrant.
To resolve all of issues I told above, You need many virtual machines to reproduce, debug and test. For manage those virtual machines, Vagrant is a tool that you need.
For this article, I will talk about installing on MacOS.
First, You need install virtualbox with this command
brew cask install virtualbox
Then, Let's install vagrant
brew cask install vagrant
Next, To manage all of you vagrant machines, you need to install vagrant-manager
brew cask install vagrant-manager
Done! You have setup vagrant on your MacOS.
First, I would remind all of you that there are three important components to vagrant: CLI, vagrantfile and vagrant cloud.
The command line tool to start, stop Vagrant VMs, initialize vagrant environments and manage running Vagrant VMs.
This is small program was written by Ruby language. You can read and write the vagrantfile quickly althought you don't know ruby language. Vagrantfiles are mostly statements, not complex logic.
The hashiCorp, Father of vagrant will provide for you all public virtual machines. You can find your box that you need here Now, let talk about how to use vagrant. To use it, you need add the box that you need. Example, I want to build an magento2 project on linux environment. I run this command
cd ~ && mkdir linuxbox && cd linuxbox && vagrant init ubuntu/xenial64
By default, if you still donot have the box, vagrant will get the box that you have declared in your command. In this case, I use the box ubuntu/xenial64. After added, continue run command vagrant up
to download and add the box. When it'd done. You can check status of the box by way execute following command vagrant status
.
When you working with ubuntu, of course you need to know to ssh, Vagrant includes an SSH client that we can use this to connect to box that support SSH. Now, on the box is started, run command vagrant ssh
. Did you see any change in your terminal? Now, you can see you are staying ubuntu16.04 with user vagrant. you can check list directories of the box with command cd / && ll
. To find the ip, type ifconfig
and enter. To exit the box, type exit
.
Vagrant supports 3 primary configurations for VMs: port forwarding, public networks and private networks.
First, you can find in line no.31. That is the config of port forwarding. In your ubuntu box, type following command to setup nginx and test it
sudo apt-get install -y nginx && curl -IL http://localhost
The result return status 200 means you had installed successfully. Next, exit your box and open your browser, try to enter localhost:8080. if you cannot access this address, let try to check your vagrantfile and make sure the line no.31 was uncommentted. Reload and try again. When you reload, you might meet the case: "The forwarded port to 8080 is already in use
on the host machine". Let go to line no.31 and edit port to 8000, reload and test again.
With private networks, go to the line no.35 and replace by this config.vm.network "private_network", type: "dhcp"
. This configurations will use a dhcp server embedded in the virtual box virtual network to assign the box a private non-routable IP address. This config will make the box only accepts all of IPs has same subnet. It's useful for security and testing purpose. Alright, let come back to private networks, after replace, try to reload and ssh to box. Type ifconfig
and you will see an address begin with 172. Go to your browser and enter that IP. You will see it can replace your old address localhost:8000
Vagrant provider allow vagrant defines a box for a specific hypervisor. All of hypervisors need many configurations such as CPU, memory, hard disk driver, etc. Now, let's try with vagrant, ssh to your box and type following command vmstat -s
. This command will show the memory's information for you. If you want to know about CPU, let type lscpu
. As currently, I see the box which I am using is running with 2 CPUs. Do you want to change these configurations? If yes, let find your vagrantfile and uncomment line no.51 and no.58. At line 57, you can input your value for memory, of course it's must less than your reality memory. Same before, You can edit your CPUs on the box with example syntax vb.cpus = 4
With my sharing on the No.3 portion, we can replace it easier when you work with PUPHPET. You can declare your vagrantfile easier such as sync folder, private network, webserver, mysql, cronjobs, etc. After you declare it succesfully and download your config as a zip file. You must extract and paste into you vagrant folder and type vagrant up
in terminal and done. Did you see difficulty?
If you are a developer and often face some bugs on another environment, I think vagrant can help you debug, and find the reason. If you are newbie and want to understand about webserver, how to config webserver, I think vagrant also help you when you using the config files from PuPHPet. One more, if you are using windows and have not ever used linux yet, you also find to vagrant. Hopefully you can enjoy with my sharing.