Blog

How to Install Apache Tomcat 9 on Ubuntu 18.04

Apache Tomcat is an accessible, open-source application server used to house many of today applications. It’s free, stable, lightweight and is utilized to render Java coding as well a range of other applications.

Today we will be focusing on how to install Apache Tomcat 9 on our Liquid Web Ubuntu server, specifically Ubuntu 18.04 LTS.

Pre-flight
  • Open the terminal and log in as root. If you are logged in as another user, you will need to add sudo before each command.
  • Working on a Linux Ubuntu 18.04 server

Step 1: Install or Verify Java 8 is installed

java -version

If nothing is returned or you get the message “No such file or directory” then you’ll need to install Java 8.

In this case, we’ll be installing OpenJDK 8 because it’s free and, the OracleJDK recently reached its EOL in January 2019.

apt install default-jdk -y

 

If Java was installed successfully, you’d see something like this when using the java -version command

java -version

Output:

openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)

 

Step 2: Install Tomcat 9

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

 

Step 3: Extract Tomcat 9 Tarball

However, fun to think about, this tarball doesn’t belong to any cat, it belongs to Tomcat!

We’ll be moving this tarball into the /opt/tomcat directory.

mkdir /opt/tomcat

mv apache-tomcat-9.0.8.tar.gz /opt/tomcat

tar -xvzf /opt/tomcat/apache-tomcat-9.0.8.tar.gz

 

Step 4: Create a Tomcat user

We create the tomcat user as extra security as this user will be the one who has group ownership to the Tomcat files.

groupadd tomcat

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

 

Step 5: Update permissions to Tomcat

Since Tomcat belongs to the group category, we have to update the permission for some of the directories of Tomcat.

chgrp -R tomcat /opt/tomcat

cd /opt/tomcat/apache-tomcat-9.0.8

chmod -R g+r conf

chmod g+x conf

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

 

Step 6: Create a Systemd Service File

Copy the path of Tomcat’s home by running this command:

update-java-alternatives -l

Output:
java-1.11.0-openjdk-amd64      1101       /usr/lib/jvm/java-1.11.0-openjdk-amd64</b

 

Your path may look different but, that’s alright, take the highlighted path and put it into your /etc/systemd/system/tomcat.service file, as the JAVA_HOME variable (shown below).

vim /etc/systemd/system/tomcat.service

Just as the path may look different from our example, you’ll focus on providing the variables in this file the path to our Tomcat installation(a.k.a. Tomcat’s root directory). For example, we’ll let the server know that the startup.sh and shutdown.sh files are located in the /bin directory of Tomcat. At any rate, be sure that your paths (bolded in the example) are the correct paths.

 

[Unit] Description=Apache Tomcat
After=syslog.target network.target
[Service] Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat/apache-tomcat-9.0.8/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'
WorkingDirectory=/opt/tomcat/apache-tomcat-9.0.8
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

Be sure to save the file by using :wq

 

Step 7: Reload the Systemd File

We do this so the system can recognize the changes to the file that we just edited.

systemctl daemon-reload

 

Step 8: Restart Tomcat

systemctl start tomcat

Note:
If the Tomcat service fails to start use journalctl -xn as a way to know the exact errors that are occurring.

 

Step 9: Verify Tomcat is Running

systemctl status tomcat

Output:

* tomcat.service - Apache Tomcat
Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-04-04 14:33:04 EDT; 4min 29s ago
Process: 10912 ExecStart=/opt/tomcat/apache-tomcat-9.0.8/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 10930 (java)
Tasks: 47 (limit: 2157)
CGroup: /system.slice/tomcat.service
`-10930 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/apache-tomcat-9.0.8/conf/logging.properties -Djava.util.logging.manage

If correctly installed you’ll also be able to see the Tomcat default page by visiting http://Host_IP:8080 in your browser, replacing Host_IP with your server’s IP or hostname, followed by Tomcat’s port number 8080.

 

Related posts

Shell: How to wrap text files to fit specified width

admin

Tại sao chúng ta thích các bài hát từ khi còn nhỏ

admin

Bệnh nhân thực vật vẫn "giao tiếp"

admin

Ảnh đẹp: Kỳ lạ vùng sa mạc nằm giữa các núi băng ở Nga

admin

Phát hiện tiểu hành tinh chưa được nhận dạng

admin

Thuế, phí khi tặng cho bất động sản giữa ông ngoại và cháu ?

admin

Bệnh tật nhìn từ quan điểm của thuyết tiến hóa

admin

Dụng cụ thao tác mới cho công nghệ micro và nano

admin

FreeBSD Laptop: Find Out Battery Life Status Command

admin

Leave a Comment