Apache Tomcat is a web server and servlet container that is used to serve Java applications. Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, released by the Apache Software Foundation. This tutorial covers the basic installation and some configuration of the latest release of Apache Tomcat 9 on Ubuntu 18.04 LTS & Ubuntu 16.04 LTS server.
Step 1./ Install Java 8
Apache Tomcat 9 requires Java 8 or newer to be installed on the server. Java 8 packages are available in the default Ubuntu 18.04 LTS & 16.04 LTS repositories. Run the following command to install Java 8
# apt install -y openjdk-8-jdk
– Once the installation is completed, you can check the installed version using the following command:
# java -version openjdk version "1.8.0_181" OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.18.04.1-b13) OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
Step 2./ Install Tomcat 9
– Go to the official Apache Tomcat website and download the most recent version of the software to your server. At the moment the most recent release is version 9.0.34, here
# VERSION=9.0.34 # wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz
– untar the downloaded Apache Tomcat archive using the following command:
# tar -xzvf apache-tomcat-${VERSION}.tar.gz -C /opt
– Let’s rename the directory to something simple like below:
# cd /opt # mv apache-tomcat-${VERSION}/* tomcat
– It is not recommended to run Apache Tomcat as user root, so we will create a new system user which will run the Apache Tomcat server
# groupadd tomcat # useradd -g tomcat -d /opt/tomcat -s /bin/nologin tomcat useradd: warning: the home directory already exists. Not copying any file from skel directory into it.
– After creating the Apache Tomcat user, let’s change the ownership of all Apache Tomcat files as below:
# chown -R tomcat:tomcat /opt/tomcat/
– Run the below command to list the java versions available and the location on your system
# update-java-alternatives -l
java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
– Create the a systemd file with the following content:
# vi /etc/systemd/system/tomcat.service [Unit] Description=Apache Tomcat 9.x Wants=network.target After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid Environment=CATALINA_HOME=/opt/tomcat Environment='CATALINA_OPTS=-Xms512M -Xmx1G -Djava.net.preferIPv4Stack=true' Environment='JAVA_OPTS=-Djava.awt.headless=true' ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/opt/tomcat/bin/shutdown.sh SuccessExitStatus=143 User=tomcat Group=tomcat UMask=0007 RestartSec=10 Restart=always [Install] WantedBy=multi-user.target
– Now you can start the Apache Tomcat 9 server and enable it to start on boot time using the following command:
# systemctl daemon-reload # systemctl enable tomcat Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /etc/systemd/system/tomcat.service. # systemctl start tomcat
– Apache Tomcat by default is listening on port 8080,Before accessing the Apache Tomcat server, we need to adjust the firewall to allow our requests to get to the service. Use the following commands:
# sudo ufw allow 8080
– You should now be able to access the Apache Tomcat server in your favorite web browser. Open the browser and navigate to http://IP_address:8080 and you will see the home page of Apache Tomcat as below.
Step 3./ Configure Apache Tomcat Web Management Interface
– By default, we can not access the Apache Tomcat web management interface because we have not created a user yet. Apache Tomcat users and their roles are defined in the tomcat-users.xml file. Open the tomcat-users.xml file and add the following lines:
# vi /opt/tomcat/conf/tomcat-users.xml
<tomcat-users>
<!--
Comments
-->
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<user username="admin" password="PASSWORD_CHANGE_ME" roles="admin-gui,manager-gui"/>
</tomcat-users>
– By default Apache Tomcat web management interface is configured to allow access only from the localhost. If you want to be able to access the web interface from a remote IP or from anywhere which is not recommended because it is a security risk you can open the following files and make the following changes. In this configuration we will allow any IP from the subnet 192.168.1.* to gain access to Apache Tomcat web management interface.
# vi /opt/tomcat/webapps/manager/META-INF/context.xml <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.1.*" /> # vi /opt/tomcat/webapps/host-manager/META-INF/context.xml <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.1.*" />
– Finaly restart the Apache Tomcat Server daemon:
#systemctl restart tomcat
Conclusion
You have successfully installed Apache Tomcat 9 Server on your Ubuntu 18.04 LTS / 16.04 LTS Server.
Read also:
- How To Configure Nginx as a Reverse Proxy for Apache Tomcat Server
- How To Configure Apache as a Reverse Proxy for Apache Tomcat Server
We hope this tutorial was enough helpful. If you need more information, or have any questions, just comment below and we will be glad to assist you!