Deploying JAR or WAR on ubuntu using Amazon EC2 services
2 min readMay 4, 2019
One of the recommended ways to deploy a Java service on AWS is to deploy on ubuntu machine, there are another ways like using Elastic Beanstalk, but I prefer custom ubunto machine that you can control completely.
Prerequisites
- Amazon EC2 account
- PuTTy or any other SSH Client
- WinSCP or any other SSH File Manager
Creating Instance and connecting with SSH Client
- Create EC2 instance with Ubuntu machine by following Amazon documentation https://docs.aws.amazon.com/efs/latest/ug/gs-step-one-create-ec2-resources.html
- Before finishing the creation choose to Download the Key Pair file, so we can connect to the machine from SSH Client.
- Go to PuTTYgen and save your PEM file to a PPK file (PPK = PuTTY Private Key), by clicking on “Save private key”.
- Open PuTTY and Go to “SSH -> Auth” and Browse to the .ppk file
- Goto “Session” and Enter the Host Name or the IP of your ubuntu machine with Port 22 and SSH selected and click “open”
Installing Java on the machine
- Update ubuntu
apt-get update && apt-get upgrade
2. Install Java
apt-get install default-jdk
Deploying our Service
So far we created a machine on Amazon EC2, we prepared the machine to run Java applications so we installed the Java JDK, after we set up the right environment its time to deploy and run our service
- Upload your package, .jar or .war file to the machine, using WinSCP File Manage or using the Bash Script
- If you are using WinSCP, you can connect and use the PuTTY .ppk file
- After uploading the .war or .jar file, you need to run it,
nohup java -jar PACKAGE.war > mylog.log
nohup will run your java application in the background.
4. Now it is time to test your service by calling the Machine address with your application PORT.