How to install Apache Tomcat(Pure Java Http) Web Server in Ubuntu

What is Apache Tomcat

Apache Tomcat, often referred to as Tomcat, is an open-source application server developed by the Apache Software Foundation. Functioning as a servlet container, Tomcat implements the Java Servlet and JavaServer Pages (JSP) specifications, providing a platform for deploying and running Java web applications. It is widely used due to its lightweight nature, simplicity, and robust performance.

Tomcat supports Java EE (Enterprise Edition) technologies, making it a popular choice for hosting dynamic and scalable web applications. Its modular architecture allows components to be added or removed based on specific project requirements. Additionally, Tomcat integrates with the Apache HTTP Server, serving as a connector for handling dynamic content through servlets and JSP.

Developers appreciate Tomcat for its ease of configuration, extensibility, and strong community support. It plays a crucial role in the Java web application ecosystem, powering a multitude of applications across various industries.

Installing Tomcat

				
					sudo useradd -m -d /opt/tomcat -U -s /bin/false tomcat

#install the JDK:
sudo apt update
sudo apt install default-jdk
java -version

cd /tmp
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.4/bin/apache-tomcat-10.1.4.tar.gz
cd /opt
mkdir tomcat
cd /tmp
sudo tar xzvf apache-tomcat-10.1.4.tar.gz -C /opt/tomcat --strip-components=1
sudo chown -R tomcat:tomcat /opt/tomcat/
sudo chmod -R u+x /opt/tomcat/bin
				
			

Configuring Admin Users

				
					sudo nano /opt/tomcat/conf/tomcat-users.xml

<role rolename="manager-gui" />
<user username="manager" password="manager_password" roles="manager-gui" />

<role rolename="admin-gui" />
<user username="admin" password="admin_password" roles="manager-gui,admin-gui" />

#remove the restriction for the Manager:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml

comment value

#repeat for Host Manager:
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

#Creating a systemd service:
sudo update-java-alternatives -l

sudo nano /etc/systemd/system/tomcat.service
-----
[Unit]
Description=Tomcat
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

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

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target
-----

sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl status tomcat
sudo systemctl enable tomcat

#Allow port
sudo ufw allow 8080

#visit
http://your_server_ip:8080
				
			

Leave a Comment

Your email address will not be published. Required fields are marked *