Host a website on a VM using automation

Host a website on a VM using automation

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

vagWht.png

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?

vagArc.PNG

  1. An user or a developer will create a Vagrantfile or obtain one from some repository.
  2. or use the Vagrant init command which also creates the Vagrantfile.
  3. Vagrant up command searches for the Vagrantfile and then runs it on top of VirtualBox to create a new image.
  4. The image is then available for Vagrant to use.
  5. Vagrant will run a script or several scripts to do provisioning and create the environment setup.
  6. Environment setup could be like setting up servers and databases.
  7. Provisioning tools supported by Vagrant are Chef/Puppet/Bash.
  8. 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.

  1. 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
    
    1.PNG
  2. 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
    
    2.PNG 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.
  3. Boot up or start the VM
    vagrant up
    
    3.PNG This will download the Ubuntu box as well as create and configure the virtual machine.

Exploring the VM

  1. Explore the /vagrant folder. While still being inside the root project folder i.e VagrantDemo, run the ssh command
    vagrant ssh
    
    4.PNG This gives you an SSH terminal session inside the Ubuntu VM.
  2. List the contents of the Vagrant folder
    ls /vagrant
    
    Currently this should have only the Vagrantfile. 5.PNG

Install Apache web server

Inside the VM terminal, we will install the apache server using an Ubuntu package manager called apt

  1. Update all packages on the system
    sudo apt update
    
    sudo may be required if the current user doesn't have root access.
  2. Install apache
    sudo apt install apache2
    
    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. 7a.PNG
  3. How do we know the Apache server is running?
    systemctl status apache2
    
    This should give the following output... 9.PNG
  4. Make a request to the server
    curl -v localhost:80
    
    This should give some HTML as output. We are now able to access the server from within the VM. 10.PNG 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.. 11.PNG

  • “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

  1. Exit Vagrant ssh and you should be back into the Project root directory
    exit
    
  2. Edit the Vagrantfile using any text editor and add the below line just before end
    config.vm.network "private_network", type: "dhcp"
    
    Save the file and close the editor. 12.PNG
  3. Reload the VM and start it
    vagrant reload
    
  4. Once the machine is up, ssh into it
    vagrant ssh
    
  5. Check the IP address again
    ip addr
    
    Check for the new network created. 14.PNG
  6. 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

  1. Where is the default apache home page located? 15.PNG These lines from the demo page give us the location i.e. /var/www/html
  2. Go to the directory
    cd /var/www/html
    
  3. 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
    
    17a.PNG Once you've made some changes, refresh the browser to see the changes. 17b.PNG

Now let's display an actual website.

  1. 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 18.PNG
  2. 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
    cd /vagrant
    ls
    
    You should see the Vagrantfile and webcontent folder 19.PNG
  3. Now we need to copy the webcontent folder to the apache Html folder
    sudo cp /vagrant/webcontent/* /var/www/html/
    
  4. Now reload the browser and you should see your website 21.PNG

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.
  1. Once again exit the VM and edit the Vagrantfile as shown below.
    exit
    notepad Vagrantfile
    
  2. Add the commands to install apache and to copy the website content to the Html folder into the Vagrantfile as shown below
    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
    
    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. 22.PNG
  3. Validate the Vagrantfile
    vagrant validate
    
    This should print something like Vagrantfile validated successfully. 23.PNG
  4. Run vagrant destroy to delete the existing virtual machine.
    vagrant destroy
    
    24.PNG
  5. Now bring up the VM
    vagrant up
    
    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!
  6. Check the IP address
    ip addr
    
  7. 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.

Did you find this article valuable?

Support WeMakeDevs by becoming a sponsor. Any amount is appreciated!