This brief tutorial shows you how to get Apache Tomcat 9 series on Ubuntu 16.04 | 18.04 LTS systems.
For those who don’t know what Tomcat is, below is an overview.
Apache Tomcat software is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket developed by the Apache Software Foundation.
Although not as popular as Apache2 or Nginx HTTP servers, Tomcat is still important to some projects.
Tomcat works best when rendering web pages the include Java server page coding and Java Servlet. These languages are still required by some other protocols used by Java developers.
When you’re ready to install Tomcat, follow the steps below:
Step 1: Install OpenJDK
Tomcat requires Java JDK to be installed in order to function. You can either install Oracle Java JDK or its open source alternative called OpenJDK.
For this tutorial, we’re going to be installing OpenJDK and to install it in Ubuntu is pretty straightforward.
Simply run the commands below to install OpenJDK:
sudo apt update sudo apt install default-jdk
That should install OpenJDK 8 and configure it as the default…
Now that Java is installed, we can now download and create a tomcat user, which will be used to run the Tomcat service.
Step 2: Create Tomcat Service Account
You’ll want to run Tomcat as it own user without root privileges. You should create a new user and group that will run the Tomcat service.
To do that, run the commands below
First, create a new tomcat group called tomcat.. Linux systems usually create groups based on the account name..
sudo groupadd tomcat
Next, create a new tomcat user called tomcat and make the user a member of the tomcat group above..
To do that, run the commands below
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Now that our tomcat user is set up, let’s download and install Tomcat package..
Step 3: Download Tomcat Packages
After installing OpenJDK and creating a service account for Tomcat, run the commands below to download Tomcat version 9.
At the time of this writing the latest version of the 8 series was 9.0.35.
You can get the latest from the link below:
cd /tmp wget ftp://apache.cs.utah.edu/apache.org/tomcat/tomcat-8/v9.0.35/bin/apache-tomcat-9.0.35.tar.gz
After downloading, we’ll also want to create a new Tomcat directory at /opt/tomcat.. Then we’ll extract the downloaded content into that directory…
To do that, run the commands below:
sudo mkdir /opt/tomcat sudo tar xzvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1
Next, give the tomcat user control of the entire directory and make all the scripts in the bin location executable…
sudo chown -R tomcat: /opt/tomcat sudo sh -c 'chmod +x /opt/tomcat/bin/*.sh'
When you’re done, continue below to creating Tomcat service…
Step 4: Configure Tomcat Service
Now that the package is extracted, run the commands to open Tomcat configuration file for its default user
sudo nano /opt/tomcat/conf/tomcat-users.xml
Then create an account with password for the user and save by copying and pasting the lines below into the file.. just before the </tomcat-users>
<role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="admin" password="type_new_password_here" roles="manager-gui,admin-gui"/>
Save the file and exit..
Next, run the commands below to create a server account for Tomcat
sudo nano /etc/systemd/system/tomcat.service
then copy and paste the lines below into the file and save
[Unit] Description=Tomcat servlet container After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/default-java" 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
Save and exit.
After that, run the commands below to reload systemd profiles and enable tomcat service…
sudo systemctl daemon-reload sudo systemctl start tomcat.service sudo systemctl enable tomcat.service
To check if Tomcat is running or not, run the commands below:
sudo systemctl status tomcat.service
You should get similar screen as below;
● tomcat.service - Tomcat 8.5 servlet container Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2019-07-08 13:46:15 CDT; 1min 35s ago Main PID: 8947 (java) Tasks: 46 (limit: 4680) CGroup: /system.slice/tomcat.service └─8947 /usr/lib/jvm/default-java/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.mana Jul 08 13:46:15 ubuntu1804 systemd: Starting Tomcat 8.5 servlet container... Jul 08 13:46:15 ubuntu1804 startup.sh: Existing PID file found during start. Jul 08 13:46:15 ubuntu1804 startup.sh: Removing/clearing stale PID file. Jul 08 13:46:15 ubuntu1804 startup.sh: Tomcat started. Jul 08 13:46:15 ubuntu1804 systemd: Started Tomcat 8.5 servlet container.
That is how you now tomcat is running…
Now, open your browser and browse to the local server IP or hostname
and you should see Tomcat default page..
Click on the Manager App to logon to the backend page…
By default,Tomcat restrict access to the Manager and Host Manager apps to connections coming the local server only.
If you want to access the Tomcat server remotely, you’ll want to whitelist the remote IP address to be allowed… To change the IP address restrictions on these, open the appropriate context.xml files.
For the Manager app, type:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
For the Host Manager app, type:
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
Inside, comment out the IP address restriction to allow connections from anywhere. Alternatively, if you would like to allow access only to connections coming from your own IP address, you can add your public IP address to the list:
context.xml files for Tomcat webapps should look similar to the one below:
<Context antiResourceLocking="false" privileged="true" > <!--<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|new_public_ip_address_here" />--> </Context>
Save and close the files and you’re done!
Congratulations! You have successfully installed and configured Tomcat on Ubuntu 20.04 and 18.04 LTS systems.
You may also like the post below: