Table of contents
Virtual Machines are being widely used by developers for testing code on various configurations. VMs can be created using VirtualBox or VMware or Vagrant. Vagrant is a handy tool to know especially if you're trying to get into DevOps.
Benefits of using Vagrant over VirtualBox or VMWare
- Vagrant is not just about creating VMs, it is about automating the work of setting up development environments for your projects.
- Whereas with VirtualBox every time you need a VM to work/test, you have to install the OS from scratch and manually install every tool needed; or you can clone the existing one. But both are manual jobs and not customizable.
- With Vagrant you will have a new VM ready for work in no time, with every tool you need.
- Vagrant also allows you to store all the configuration in a text file which can be stored and accessed from any code repository.
What we will be building
- We will first create a Virtual machine which has Ubuntu running on it.
- Then we will run a web server from this virtual machine.
- We will host our website on this server.
- Finally, we will use vagrant to automate this process.
Tools/Stack
- Vagrant: used to automate and manage virtual machines
- VirtualBox: runs the operating system
- Ubuntu(Linux) : a popular operating system
- Apache HTTP Server : a web server
NOTE: For this project I have worked on a windows machine.
Installation of software
Vagrant : Follow the instructions given on the Vagrant website
Virtual Box : Follow the instructions given on the VirtualBox website
Vagrant
What is Vagrant ?
- Vagrant is an open-source tool that is used to manage virtual machines with the help of a provider like VirtualBox.
- Using the command line one can manage any available operating system by installing it, updating it, starting it up or shutting it down.
- VirtualBox is the software that runs the operating system on the virtual machine.
- A Virtual Machine can be compared to a computer or machine that exits virtually. It has it's own operating system and runs as an independent machine on the host system.
How does it work?
- An user or a developer will create a Vagrantfile or obtain one from some repository.
- or use the Vagrant init command which also creates the Vagrantfile.
- Vagrant up command searches for the Vagrantfile and then runs it on top of VirtualBox to create a new image.
- The image is then available for Vagrant to use.
- Vagrant will run a script or several scripts to do provisioning and create the environment setup.
- Environment setup could be like setting up servers and databases.
- Provisioning tools supported by Vagrant are Chef/Puppet/Bash.
- Basically, a user will run Vagrant up, which internally calls the provider like VirtualBox, which then creates the VirtualMachine.
Vagrantfile
- When we run the vagrant command, it looks for a config file in your current directory, called a ‘Vagrantfile’.
- This file contains the description of the Vagrant Virtual Machine. It specifies the OS, RAM, Storage etc. that the VM should have.
- In case there are multiple Vagrant VMs on the system, then first we need to enter into the directory containing our specific Vagrantfile and then run Vagrant commands.
Vagrant folder
Whenever you start a virtual machine with vagrant up it automatically syncs the contents of the current directory into a directory called /vagrant on the virtual machine. As we proceed further, this directory will be useful while hosting our website.
Hosting a website on an Ubuntu machine
Through this example, we will learn,
- How to create a VM using Vagrant?
- How to install the Apache server on the VM?
- How to host our own website on an Apache server?
Create a Virtual Machine
Before we start ensure the vagrant is installed.
- Open your terminal (i.e Command prompt for Windows & Terminal for Mac). Create a new directory and cd into it like so
mkdir VagrantDemo cd VagrantDemo
- Use the vagrant init command to initialize a new virtual machine.
Specify the type of VM we want to use. The basic unit in a Vagrant setup is called a “box” or a “Vagrantbox.” This is a complete, self-contained image of an operating system environment.
vagrant init ubuntu/jammy64
This initializes a new Vagrant virtual machine, that is running Ubuntu version 22.04 also known as “Jammy Jellyfish”, hence the “jammy” bit. This command will create a Vagrantfile, which we discussed above.
- Boot up or start the VM
vagrant up
This will download the Ubuntu box as well as create and configure the virtual machine.
Exploring the VM
- Explore the /vagrant folder.
While still being inside the root project folder i.e VagrantDemo, run the ssh command
vagrant ssh
This gives you an SSH terminal session inside the Ubuntu VM.
- List the contents of the Vagrant folder
Currently this should have only the Vagrantfile.ls /vagrant
Install Apache web server
Inside the VM terminal, we will install the apache server using an Ubuntu package manager called apt
- Update all packages on the system
sudo may be required if the current user doesn't have root access.sudo apt update
- Install apache
Press Y when asked to confirm the installation. Now Ubuntu downloads the Apache HTTP Server files from repositories and installs them for you. The package manager also calculates other packages it needs (the “dependencies”) and installs them.sudo apt install apache2
- How do we know the Apache server is running?
This should give the following output...systemctl status apache2
- Make a request to the server
This should give some HTML as output. We are now able to access the server from within the VM.curl -v localhost:80
How do we access the server from our browser?
Server networking
A web server listens to some port in order to serve content to users. The default port used is 80.
Get VM's IP address
ip addr
You will get a similar output..
“inet” indicates the IP addresses. In this example, the machine currently has 2 x IP addresses:
127.0.0.1, which is an IP address that machines use to refer to themselves. You use this IP address when you want to access something on the machine, from the machine itself! Also called “localhost”.
10.0.2.15 This is the machine’s IP address that it uses to identify itself to the network. We can use this to access it from our host computer. But when we put 10.0.2.15 in our browser we can't access the server.
- This is because the default VirtualBox configuration uses Network Address Translation (NAT).
- NAT is used so the machine can see/connect to the outside network but the machine remains invisible to the outside network.
- We need to use DHCP or Port Forwarding to expose the server to the outside network.
Setting up the virtual machine's network
- Exit Vagrant ssh and you should be back into the Project root directory
exit
- Edit the Vagrantfile using any text editor and add the below line just before end
Save the file and close the editor.config.vm.network "private_network", type: "dhcp"
- Reload the VM and start it
vagrant reload
- Once the machine is up, ssh into it
vagrant ssh
- Check the IP address again
Check for the new network created.ip addr
- Now you should be able to access the VM from your browser using the ip address mentioned at inet i.e. http://192.68.56.16, this will open the default Apache page
Replacing the default page with our custom website
- Where is the default apache home page located?
These lines from the demo page give us the location i.e. /var/www/html
- Go to the directory
cd /var/www/html
- There will be an index.html file.
Edit this file to ensure we are in the correct directory. Use nano or vi or any text editor.
sudo nano index.html
Once you've made some changes, refresh the browser to see the changes.
Now let's display an actual website.
- Download this sample website from GitHub or use any other of your choice.
And place the webcontent folder into the root directory i.e. VagrantDemo
- As we mentioned above, the vagrant up command syncs contents of the current directory to /vagrant on the VM. Inside the VM terminal go into the /vagrant folder and list all the contents
You should see the Vagrantfile and webcontent foldercd /vagrant ls
- Now we need to copy the webcontent folder to the apache Html folder
sudo cp /vagrant/webcontent/* /var/www/html/
- Now reload the browser and you should see your website
Let's automate using Vagrant Provisioner
- Instead of writing each command separately let's try to create a script that automatically runs the website.
- Whenever the virtual machine is created, we want to automatically install Apache HTTP Server and copy the website files. We use provisioning to achieve this.
- In Vagrant, a block of provisioning commands begins with config.vm.provision.
- Once again exit the VM and edit the Vagrantfile as shown below.
exit notepad Vagrantfile
- Add the commands to install apache and to copy the website content to the Html folder into the Vagrantfile as shown below
NOTE : I ran into issues while installing Apache on my system hence I used apt --fix-broken install. It isn't mandatory, but if you have issues, you can try it out.config.vm.provision "shell", inline: <<-END apt update apt install -y apache2 apt --fix-broken install apache2 -v cp -r /vagrant/webcontent/* /var/www/html/ echo "Machine provisioned at $(date)! Welcome!" END
- Validate the Vagrantfile
This should print something like Vagrantfile validated successfully.vagrant validate
- Run vagrant destroy to delete the existing virtual machine.
vagrant destroy
- Now bring up the VM
This should give a lot of log output but the final line should be Machine provisioned at Sun Jun 114:43:22 UTC 2022! Welcome!vagrant up
- Check the IP address
ip addr
- As before enter the IP address in the browser. Your website should be up and running!!
Bonus Example
For extra practice, I also provisioned a VM to host a Spring Boot application that I found online. Here is the content of my Vagrantfile for the same.
config.vm.network "private_network", type: "dhcp"
config.vm.provision "shell", inline: <<-END
apt update
apt install -y openjdk-11-jdk --fix-missing
apt install -y --fix-broken
apt install -y maven --fix-missing
cd /vagrant/spring-petclinic
mvn spring-boot:run
END
That's it for this blog. I hope you will try it out and learn something new. Also in case, I have missed something, feel free to share your thoughts and feedback. Thanks for reading.