Vagrant - How to setup? Part 2

By El.Vin | El.Vin | 29 Aug 2020


We are now on the second part of our series, Vagrant - How to setup?. In case you missed the first part, you can just read it here. Let's proceed.

First, open your terminal, change directory (cd) into the folder where you want your Vagrantfile to live and execute this code:

vagrant init

After executing this code, you will have a notification that the Vagrantfile was created on the directory where you've cd into.

terminal

There should be a "Vagrantfile" file created. Find the Vagrantfile and open it on your editor of choice. In my case, I have visual studio code editor installed. 

Go to vagrant boxes and search for a box that you want to use. If you want ubuntu, then search ubuntu.

vagrant box search bar

If you've decided which box to use, copy the name of the box (e.g. ubuntu/trusty64 or so) and go back to the editor where you open the Vagrantfile that was created earlier.

Now, find the line that says config.vm.box = "base" and replace the word base with the name of the box that you've chosen (please don't delete or remove any double quotes, it is part of the syntax). This will instruct vagrant that you want to use ubuntu/trusty64 box or the one that you have selected.

Let's work with the other settings, I will make it as simple as possible.

Note: The number or hash sign (#) indicates that the line is a comment or was commented. Please remove the number or hash sign (#) on the line to uncomment it.

config.vm.box_check_update = false

If you want vagrant to check if the box has an update, you can set this to true.

config.vm.network "private_network", ip: "192.168.33.10"

You can change the value of the IP address (192.168.33.10) to any IP that you want. I prefer the default, so I will not change it. This IP will be used to access your site after installing the server in your vagrant box.

config.vm.synced_folder "../data", "/vagrant_data"

Now, to sync the folder from your local machine to the vagrant box, you need to create a new folder (same directory with the Vagrantfile for easy access) and name it html (or anything you like).

The "../data" refers to a folder in your local machine and the "/vagrant_data" refers to  a folder inside your vagrant box. I will changed "../data" to "html" to match the folder in my local machine and "/vagrant_data" to "/var/www/html". This"/var/www/html" path is pointed to the root directory of the server inside the vagrant box.

  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end

This block of code refers to the settings of the VirtualMachine. You can set any settings that you want to change here. For example, you can set the VirtualMachine's memory by changing the value of the vb.memory. And also, don't forget to uncomment it before changing any values. But for now, I will not touch these settings. 

  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL

In this block, you can add the commands (if you have at least a background in linux command line) that will be executed on the terminal INSIDE the vagrant box (or your Virtual Machine). Uncomment this block,  we need this block to install our apache server. After uncommenting it, we need to add the following line before the end of the block. This will install the PHP programming language.

apt-get install -y php

Or you can also just add php after apache2. Like this:

apt-get install -y apache2 php

After this line, you will have something like this:

  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y apache2 php
    # apt-get install -y php
  SHELL

Now, we're ready to start our vagrant box. Just run this command on the terminal.

vagrant up

This command will execute the codes that written on your Vagrantfile. It will download the box that you've specify, it will check if there is an update for the box, set the IP of the box, sync the folder from your local machine to vagrant box and execute the commands that written on the last block (that has "shell" on it).

After the commands (or scripts) doing their job and everything is done. You can now access the box by using this command on your terminal.

vagrant ssh

Inside your vagrant box, you need to start the apache server. It can be done by executing

sudo systemctl start apache2.service

If everything went right, you can now access 192.168.33.10 (or the IP that you set earlier) in your browser. And now you have your own vagrant box local server. Your codes should live inside the html folder in your local machine, it will just sync automatically.

QUICK NOTES:

To start the vagrant box, do (please make sure that you are in the same directory of the Vagrantfile)

vagrant up

To stop the box, you can do

vagrant halt

To reload (stop the box and start it again), please do

vagrant reload

Destroy the box, please do

vagrant destroy

I hope this will help someone. If you got some errors or questions, feel free to comment it (I will answer it if I know the answer, insert evil laugh).

Next part, I will show how to automatically install needed apps/packages using the "shell" or command line script.

How do you rate this article?


2

0

El.Vin
El.Vin

Just a nobody trying to be somebody.


El.Vin
El.Vin

Just trying to improve my knowledge and grammar. Please correct me if you found something wrong.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.