How to install Apache Tomcat 9 on Ubuntu Linux. Apache Tomcat is an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and WebSocket technologies. Tomcat provides a “pure Java” HTTP web server environment in which Java code can run
Step 1: Install Java
$ sudo apt update
$ sudo apt install default-jdk
$ java -version
Step 2: Install Apache Tomcat 8 on Ubuntu
For security purposes, Tomcat should be run as an unprivileged user (i.e. not root). We will create a new user and group that will run the Tomcat service. Run the following commands to create a new tomcat group and a new tomcat user. The user is then made a member of the tomcat group – home directory of /opt/tomcat and shell of /bin/false.
$ sudo mkdir /opt/tomcat
$ sudo groupadd tomcat
$ sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
$ cd /tmp
$ curl -O http://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.30/bin/apache-tomcat-9.0.30.tar.gz
$ sudo mkdir /opt/tomcat
$ sudo tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
Assign Permissons to Tomcat User
Now run the following commands to assign the permissions to tomcat user. This is done because the used must access the Tomcat installation /opt/tomcat directory.
$ cd /opt/tomcat
$ sudo chgrp -R tomcat /opt/tomcat
$ sudo chmod -R g+r conf
$ sudo chmod g+x conf
$ sudo chown -R tomcat webapps/ work/ temp/ logs/
Step 3: Create a Tomcat systemd Service File
Next step is to create a new systemd service file to manage and run Tomcat as a service under systemd. To do so you must know where Java is installed – JAVA_HOME. The following steps are executed to find the JAVA_HOME.
$ sudo update-java-alternatives -l
NOTE: The JAVA_HOME variable can be extracted from the last column {highlighted in red color} and appending /jre to the end. Now, create the systemd service file. Open a file called tomcat.service in the /etc/systemd/system directory by typing the following command and append the following lines:
$ sudo nano /etc/systemd/system/tomcat.service
Paste the following contents into the service file.
/etc/systemd/system/tomcat.service
After=network.target
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment=’CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC’
Environment=’JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom’
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
When you are finished, save and close the file. Next, reload the systemd daemon:
$ sudo systemctl daemon-reload
$ sudo systemctl start tomcat
$ sudo systemctl status tomcat
Finally, run and test Tomcat. This is done in the browser by using the system’s IP address followed by the service default port 8080.
http://ip-address:8080