Deploying Web Application Using Vagrant
Join the DZone community and get the full member experience.Join For Free
In this article, we will deploy a Spring web application in Tomcat 7 on Ubuntu 12.04 VM, created and provisioned using Vagrant. As an initial step, Download Vagrant specific to your operating system and install it in your machine.
Then create a folder on a drive, for me its "workspace" created on D drive (D:/workspace).
Now, open a command prompt and go to "workspace" folder and execute below command to clone the GIT repository on local drive:
git clone https://github.com/arpitaggarwal/hello-spring.git
Once clone completed, execute command:
Above command will create a Vagrant file (Vagrantfile), replace the content with below:
Vagrant.configure(2) do |config| # A standard Ubuntu 12.04 LTS 32-bit box # For more boxes, you can look at https://atlas.hashicorp.com/boxes/search config.vm.box = "hashicorp/precise32" config.vm.provision "shell", path: "vagrant_provision.sh" # Create a private network, which allows host-only access to the machine # using a specific IP. config.vm.network "private_network", ip: "192.168.33.10" end
Then create a vagrant_provision.sh file under same directory and copy below contents to the file:
#!/usr/bin/env bash sudo apt-get update echo "Installing Apache.." sudo apt-get install -y apache2 echo "Installing Tomcat.." sudo apt-get install -y tomcat7 echo "Installing Tomcat7 docs.." sudo apt-get install -y tomcat7-docs echo "Installing Tomcat7 administration webapps.." sudo apt-get install -y tomcat7-admin echo "Installing Tomcat7 examples webapps.." sudo apt-get install tomcat7-examples echo "Installing Git.." sudo apt-get install -y git echo "Installing Maven.." sudo apt-get install -y maven echo "Installing Java 7.." sudo apt-get install -y software-properties-common python-software-properties echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | sudo /usr/bin/debconf-set-selections sudo add-apt-repository ppa:webupd8team/java -y sudo apt-get update sudo apt-get install oracle-java7-installer echo "Setting environment variables for Java 7.." sudo apt-get install -y oracle-java7-set-default
Then execute command:
Above command will make our VM up and running, also make an instance of Apache and Tomcat running (which we mentioned in vagrant_provison.sh).
Now, we will log into our newly created VM and build our project (cloned from Git) and copy it to the Tomcat deployment directory. So, we will first open our VM terminal executing below command:
Now we will first check where our project is, so execute below command:
cd / ls
Now we can see folder name "vagrant", this is the folder which is linked to our host machine and going inside we will see our cloned project. Next we will go inside and build it executing below commands:
cd vagrant cd hello-spring mvn clean install
It will generate a target folder and our .war file inside it, now we will copy this .war to Tomcat 7 deployment directory after coming back to root directory, using command below:
cd / sudo cp /vagrant/hello-spring/target/hello-spring.war /var/lib/tomcat7/webapps/
Now, our application is copied to tomcat deployment directory and we are ready to hit the url and see our application running, but before that we have to change the Java version for tomcat, as our application is compiled using 1.7 and tomcat is using 1.6. It's easy, just go to the root directory and execute below commands:
cd etc/default nano tomcat7
And search for JAVA_HOME, uncomment it and edit as below:
Now, just restart the Tomcat instance, using command below:
service tomcat7 restart
Open the url : http://192.168.33.10:8080/hello-spring in host browser and we will see the welcome page of our Spring application.
Opinions expressed by DZone contributors are their own.