How to Install Apache Tomcat on an Ubuntu Cloud Server?

01-02-2024 02:11:37

Apache Tomcat is an open-source, lightweight web server designed for hosting Java websites and applications. This article outlines the process of installing Apache Tomcat on an Ubuntu 20.04 cloud server.

Installing Java

Update the operating system and install the Java Runtime Environment.

$ sudo apt update
$ sudo apt install default-jdk -y

Verify the installed version of Java.

$ java -version

Installing Apache Tomcat

Download the latest version of Apache Tomcat, then extract and proceed with configuration and installation. We'll use version 10.0.8 as an example.

$ wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.8/bin/apache-tomcat-10.0.8.tar.gz
$ sudo tar xzvf apache-tomcat-10.0.8.tar.gz
$ sudo mkdir /opt/tomcat/
$ sudo mv apache-tomcat-10.0.8/* /opt/tomcat/

$ sudo chown -R www-data:www-data /opt/tomcat/
$ sudo chmod -R 755 /opt/tomcat/

Edit the Apache Tomcat user configuration file to set up administrator and manager accounts.

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

Add the following code under the tag, remembering to replace StrongPassword with a highly secure password.

<!-- user manager can access only manager section -->
<role rolename="manager-gui" />
<user username="manager" password="StrongPassword" roles="manager-gui" />

<!-- user admin can access manager and admin section both -->
<role rolename="admin-gui" />
<user username="admin" password="StrongPassword" roles="manager-gui,admin-gui" />

Setting Up a Systemd Service

For convenience, we set up a Systemd service for Apache Tomcat.

$ sudo nano /etc/systemd/system/tomcat.service

The content is as follows:

[Unit]
Description=Tomcat
After=network.target

[Service]
Type=forking

User=root
Group=root

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

[Install]
WantedBy=multi-user.target

Reload the systemd daemon service to apply changes.

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

Testing

Finally, visit http://[your-server-IP]:8080. If everything is set up correctly, you should see the default homepage of Apache Tomcat. With Apache Tomcat now installed, you can begin deploying web applications.