Using Apache-Tomcat how to deploy Java Spring Boot API or WAR file on Amazon EC2 Instance

Shrihari Haridas
4 min readDec 18, 2022

--

1. What is Tomcat..?

Apache Tomcat is a free and open-source implementation of the Jakarta Servlet, Jakarta Expression Language, and WebSocket technologies. It provides a “pure Java” HTTP web server environment in which Java code can also run. Thus it’s a Java web application server, although not a full JEE application server.

2. Why Apache-Tomcat..?

Tomcat is considered a web server instead of an application server because it functions as a web server and Servlet container. It allows the users to run Servlet and JAVA Server Pages that are based on the web-applications. It can be used as the HTTP server. The performance of the Tomcat server is not as good as the designated web server.

3. Install tomcat on Server

1. Launch Your EC2 machine (Choose Ubuntu OS)(Open port 8080 for tomcat)

2. After Login Update your machine

sudo apt update

3. For Install tomcat Java JDk is necessary, so after up to date your machine install java

sudo apt install default-jdk

4. Check Java is Install or not properly to verify

java --version

5. Create a new group for tomcat

sudo groupadd tomcat

6. Then Create a new user name tomcat in tomcat group

sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

7. Then create a tomcat folder for installation of tomcat(depends on your need you select installation directory)

sudo mkdir /opt/tomcat (then cd /opt/tomcat)

8. Download or Install Apache tomcat on your system I am installing 9 version depends on your need you install version

wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz

9. Unzip the downloded file using tar command

tar -xvf apache-tomcat-9.0.8.tar.gz

10. then go to folder which you unzip

cd apache-tomcat-9.0.8

11. then change the permission of conf file (R- rucursive, G- group, x- others, r- read)

chmod -R g+r conf
chmod g+x conf

12. then change the owner of /webapp /work /app /logs

chown -R tomcat webapps/ work/ temp/ logs/

13. then update-java-alternatives updates all alternatives belonging to one runtime or development kit for the Java language. A package does provide these information of it’s alternatives in /usr/lib/jvm/.

update-java-alternatives -l

14. then create tomcat service with following file

vim /etc/systemd/system/tomcat.service

15. file content following details

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat/apache-tomcat-9.0.8
Environment=CATALINA_BASE=/opt/tomcat/apache-tomcat-9.0.8
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/apache-tomcat-9.0.8/bin/startup.sh
ExecStop=/opt/tomcat/apache-tomcat-9.0.8/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

16. restart daemon-relod

sudo systemctl daemon-reload

17. then hit your public ip with port 8080, but you see “403 forbidden access denied error ” for this you go to following location and comment some line

cd /opt/tomcat/apache-tomcat-9.0.8/webapps/manager/METF-INF
vi content.xml

18. then comment following line

For Comment line use “<! — for ending →”

19. After you start tomcat with following command

sudo systemctl start tomcat
sudo systemctl status tomcat

20. then click on Host-app when you click its showing pop-up login, then you create a deployer role. For creating it go to following location

cd /opt/tomcat/apache-tomcat-9.0.8/conf 
vi tomcat-user.xml

21. and paste you code befor </tomcat-users> and after paste this code save file

 <role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-status"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<user name="admin" password="admin" roles="admin-gui,admin-script,manager-gui,manager-status,manager-script,manager-jmx"/>

22. Now restart your tomcat

systemctl restart tomcat

23. then again click on host-app login with your credentials and upload you “WAR” File

Here you can see I deploy my war file then you can access you API

--

--

Shrihari Haridas
Shrihari Haridas

Written by Shrihari Haridas

Hello everyone, I am Shrihari Haridas I am a Cloud & DevOps Engineer, I work with most of DevOps Tools like, Jenkins, Git, Docker, etc.!& for Cloud AWS

No responses yet