Objective
Apache Tomcat is a web server used to manage web applications based on Java technology. In this tutorial you will find all the necessary instructions to install Tomcat on Linux Ubuntu 18.04.
First, connect to your server via an SSH connection. If you haven’t done so yet, following our guide is recommended to securely connect with SSH. In case of a local server, go to the next step and open the terminal of your server.
Java installation
Check whether Java is already installed on your system using the command:
$ java -versionIf Java is installed, go to the next step. Otherwise, proceed with its installation.
Update the apt repositories and then proceed with the installation of the Java Development Kit:
$ sudo apt-get update && sudo apt-get install default-jdkTomcat installation
First, create a new user and a new group that will launch the Tomcat service.
Create the “tomcat” group:
$ sudo groupadd tomcatAnd create the related user “tomcat” that, for security reasons, will not have access to the terminal:
$ sudo useradd -s /bin/false -g tomcat -d /usr/local/tomcat tomcatOnce the user and group are created, proceed with the actual installation of Tomcat.
Move to the /usr/local directory:
$ cd /usr/localVisit https://tomcat.apache.org/ to make sure you downloaded the latest version of Tomcat. Take 9.0.30 for example. So, download the package:
$ wget http://it.apache.contactlab.it/tomcat/tomcat-9/v9.0.30/bin/apache-tomcat-9.0.30.tar.gzUnzip the downloaded file and change the name of the folder just extracted into “tomcat”:
$ tar xzvf apache-tomcat-9.0.30.tar.gz && mv apache-tomcat-9.0.30 tomcatContinue by configuring the permissions of the aforementioned folder for the user and the “tomcat” group created previously:
$ sudo chgrp -R tomcat tomcat
$ sudo chmod -R g+r tomcat/conf
$ sudo chmod g+x tomcat/conf
$ sudo chown -R tomcat tomcat/work/ tomcat/logs/ tomcat/webapps/ tomcat/temp/At this point, the installation of Tomcat is completed, so continue with the creation of a service to start it.
First, mark the path relative to your Java installation that you can obtain using the command:
$ update-java-alternatives -lthe Java installation path necessary to create your service will be printed.
Create the tomcat.service file in the / etc / systemd / system / directory
$ sudo nano /etc/systemd/system/tomcat.serviceT The following content has to be included in the file and the previously installed Java pathway has to be carefully replaced in the designated spot:
[Unit]
Description=Apache Tomcat
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=PERCORSO_JAVA
Environment=CATALINA_PID=/usr/local/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat
Environment=CATALINA_BASE=/usr/local/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
For example in this case the file will have the following content:
[Unit]
Description=Apache Tomcat
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
Environment=CATALINA_PID=/usr/local/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat
Environment=CATALINA_BASE=/usr/local/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.targetAt this point save and close the file.
Firewall
If there is a firewall on the system, allow traffic to the default Tomcat port or the 8080. To do so, type the following command:
$ ufw allow 8080end the installation by starting Tomcat:
$ sudo systemctl daemon-reload && sudo systemctl start tomcatCheck the status of the service through the command:
$ sudo systemctl status tomcatIf the service is active, visit http://:8080 or in case of a local server http://localhost:8080 .
If the Tomcat welcome page is displayed, the installation was successful.
Automatic Start
To start the Tomcat service automatically when the system starts, all you need to do is enable it using this command:
$ sudo systemctl enable tomcatManagement Interface
To use the Tomcat management interface, first configure the user data to be used for the log in.
Open the tomcat-users.xml file located in the Tomcat conf folder:
$ sudo nano /usr/local/tomcat/conf/tomcat-users.xmlThen add the following line before the closing tag </tomcat-users> :
<user username="MYUSER" password="MYPASSWORD" roles="manager-gui,admin-gui"/>Replacing MYUSER and MYPASSWORD with the authentication data you want to use, as in the following example:
<user user username="ADMIN" password="ARUBA123" roles="manager-gui,admin-gui"/>At this point the user has been configured to access the management interface.
If you are installing tomcat on a remote server to access the management interface from your browser, you will have to disable some security restrictions:
Open the context.xml file:
$ sudo nano /usr/local/tomcat/webapps/manager/META-INF/context.xmland replace its content with the following :
<Context antiResourceLocking="false" privileged="true" ></Context>Save and close the file.
Finally, restart the Tomcat service:
$ sudo systemctl restart tomcatNow visit http://:8080/manager/html or in the case of a local server http://localhost:8080/manager/html . Access the Tomcat management console, by entering the previously set credentials.
At this point the Tomcat configuration is completed.

