Skip to main content

Command Palette

Search for a command to run...

Automating the Deployment of a Java Web App Using Vagrant

Posted as part of my DevOps learning journey | Udemy Course: Decoding DevOps – From Basics to Advanced Projects with AI

Updated
7 min read
Automating the Deployment of a Java Web App Using Vagrant

Introduction

Imagine you are working on a project that has a variety of services powering it — a web server, a database, a runtime environment, and more. All of these need to work together perfectly for your application to run.

Now, you want to make some changes or test something new. But you are not comfortable making those changes directly on the real production server — and rightly so! One wrong move can bring down the entire application for everyone.

The obvious solution is to set things up on your local machine. But doing that manually is a whole different problem. You have to install the right versions of software, configure everything correctly, and if a teammate wants the same setup, they have to repeat all those steps again from scratch. It is complex, time-consuming, and most importantly — not repeatable.

What you really need is a local setup that is automated, repeatable, and follows the principle of Infrastructure as Code (IaC) — where your entire environment is defined in a simple configuration file that anyone can run and get the exact same result every time.

That's exactly what Vagrant helps you achieve. In this blog, we will use Vagrant to automatically set up a virtual machine and deploy a Java web application on it — with just one command.

Prerequisites

Before we start, make sure you have the following installed on your computer:

  • VirtualBox — This is the software that runs your virtual machine. Download it from virtualbox.org

  • Vagrant — This is the tool that automates the VM setup. Download it from vagrantup.com

Both tools are free and available for Windows, macOS, and Linux.

Step 1 — Install the Applications

Once you download the installers, install them in the following order:

  1. Install VirtualBox first

  2. Install Vagrant after VirtualBox is set up

After installation, open your terminal (or Command Prompt on Windows) and verify the installation by running:

vagrant --version

If you see a version number, you are good to go.

Step 2 — Clone the Repository

Now, clone the project repository to your local machine. Open your terminal and run:

git clone https://github.com/hkhcoder/vprofile-project.git

This will download all the project files, including the Vagrantfile, to your computer.

Step 3 — Open the Folder with the Vagrantfile in Terminal

Navigate into the project folder where the Vagrantfile is located:

cd vprofile-project\vagrant\Automated_provisioning_WinMacIntel\Vagrantfile

Make sure you can see the Vagrantfile inside this folder by listing the files:

ls        # on Mac/Linux
dir       # on Windows

You should see Vagrantfile listed there.

Step 4 — Run the Vagrantfile

This is the most exciting step. Just run this single command:

vagrant up

Vagrant will now do everything automatically:

  • Download the base operating system (Ubuntu/CentOS, etc.)

  • Create a new virtual machine in VirtualBox

  • Install Java, Tomcat, or any other required software

  • Copy the Java web app into the right location

  • Start the web server

This may take a few minutes the first time since it needs to download the base box. Grab a cup of tea ☕ and wait for it to finish.

Step 5 — Test the App in the Browser

Once vagrant up completes, open your web browser and go to:

http://web01

You should see your Java web application running in the browser — fully deployed, with zero manual configuration!

Login credentials

ID : admin_vp
Password : admin_vp

Vagrantfile Explained

This Vagrantfile is more advanced than a basic single-VM setup. It spins up 5 virtual machines at once, each responsible for a different service — just like a real-world project setup. Let's go through it piece by piece.

Vagrant.configure("2") do |config|
  config.hostmanager.enabled = true
  config.hostmanager.manage_host = true
  • Vagrant.configure("2") — Tells Vagrant to use version 2 of the configuration format, which is the current standard.

  • config.hostmanager.enabled = true — Enables the vagrant-hostmanager plugin, which automatically manages hostname resolution between VMs.

  • config.hostmanager.manage_host = true — Also updates the /etc/hosts file on your host machine, so you can reach VMs by their hostname directly.

VM 1 — Database (db01)

config.vm.define "db01" do |db01|
  db01.vm.box = "centos/stream9"
  db01.vm.hostname = "db01"
  db01.vm.network "private_network", ip: "192.168.56.15"
  db01.vm.provider "virtualbox" do |vb|
    vb.memory = "600"
  end
  db01.vm.provision "shell", path: "mysql.sh"
end
  • This VM runs the MySQL database for the application.

  • It uses CentOS Stream 9 as the operating system.

  • It is assigned the private IP 192.168.56.15 so other VMs can talk to it.

  • It is given 600 MB of RAM to keep things lightweight.

  • The mysql.sh script runs automatically to install and configure MySQL.

VM 2 — Memcache (mc01)

config.vm.define "mc01" do |mc01|
  mc01.vm.box = "centos/stream9"
  mc01.vm.hostname = "mc01"
  mc01.vm.network "private_network", ip: "192.168.56.14"
  mc01.vm.provider "virtualbox" do |vb|
    vb.memory = "600"
  end
  mc01.vm.provision "shell", path: "memcache.sh"
end
  • This VM runs Memcache, which is a caching service used to speed up the application by storing frequently accessed data in memory.

  • It uses IP 192.168.56.14 and 600 MB of RAM.

  • The memcache.sh script installs and sets up Memcache automatically.

VM 3 — RabbitMQ (rmq01)

config.vm.define "rmq01" do |rmq01|
  rmq01.vm.box = "centos/stream9"
  rmq01.vm.hostname = "rmq01"
  rmq01.vm.network "private_network", ip: "192.168.56.16"
  rmq01.vm.provider "virtualbox" do |vb|
    vb.memory = "600"
  end
  rmq01.vm.provision "shell", path: "rabbitmq.sh"
end
  • This VM runs RabbitMQ, a message broker that allows different parts of the application to communicate with each other using message queues.

  • It is assigned IP 192.168.56.16 with 600 MB of RAM.

  • The rabbitmq.sh script handles the complete RabbitMQ installation.

VM 4 — Tomcat / App Server (app01)

config.vm.define "app01" do |app01|
  app01.vm.box = "centos/stream9"
  app01.vm.hostname = "app01"
  app01.vm.network "private_network", ip: "192.168.56.12"
  app01.vm.provision "shell", path: "tomcat.sh"
  app01.vm.provider "virtualbox" do |vb|
    vb.memory = "800"
  end
end
  • This is the most important VM — it runs the Java web application on a Tomcat server.

  • It gets a slightly higher memory of 800 MB because running a Java application requires more resources.

  • The tomcat.sh script installs Tomcat, builds the Java app, and deploys it automatically.

VM 5 — Nginx / Web Server (web01)

config.vm.define "web01" do |web01|
  web01.vm.box = "ubuntu/jammy64"
  web01.vm.hostname = "web01"
  web01.vm.network "private_network", ip: "192.168.56.11"
  web01.vm.provider "virtualbox" do |vb|
    vb.gui = true
    vb.memory = "800"
  end
  web01.vm.provision "shell", path: "nginx.sh"
end
  • This VM runs Nginx, which acts as a reverse proxy — it receives requests from the browser and forwards them to the Tomcat app server.

  • Unlike the other VMs, this one uses Ubuntu Jammy (22.04) instead of CentOS.

  • vb.gui = true means VirtualBox will open a graphical window for this VM.

  • The nginx.sh script configures Nginx to forward traffic to app01.

The Big Picture

Here is how all 5 VMs connect together:

All of this spins up automatically with a single vagrant up command — that is the true power of Infrastructure as Code!

Attribution

The Java web application used in this tutorial was provided by the instructor of the Udemy course. All credit for the application code goes to them. My contribution is solely the deployment process documented above.

Conclusion

With just a Vagrantfile and a shell script, we automated the entire deployment process of a Java web application. No more manual configuration, no more "it works on my machine" problems. Anyone who clones this repository can spin up the exact same environment with a single vagrant up command.

This approach is very useful for development, testing, and learning DevOps concepts. Once you understand Vagrant, you can take this a step further and explore tools like Docker and Ansible for even more powerful automation.

Happy deploying! 🚀