Apache Tomcat is a software package designed to run web applications in Java. Apache Tomcat is an implementation of technologies such as Java Servlet, JavaServer Pages, Java Expression Language, and Java WebSocket, which is why it is often referred to as a servlet container.

The installation should be done on a clean server, without the ispmanager panel. Let’s look at the example of the operating systems relevant at the time of publication of the article: Debian, Ubuntu, CentOS, Alma Linux / Rocky Linux.

Pre-settings

Before installing Tomcat itself, you need to create a user for its operation, as well as install Java.

Creating a user:

# useradd tomcat -U -s /bin/false -d /opt/tomcat -m

We install Java. To do this, use the ready—made instructions.

Before installing Tomcat, you need to determine its version. On the official Apache Tomcat page, there is a list of versions on the left.: select the desired one, for example Tomcat 10, then copy the link to download the archive. tar.gz .

Apache Tomcat 10.1 only works with the minimum version of Java 1.11.0!

Check the Java version using the command:

# java -version

If the output contains version 1.11 or higher (openjdk version “11.0.18”), you can install Tomcat 10.1.

If the output contains a lower version, for example 1.8 (openjdk version “1.8.0_362”), you must either install Java 1.11 or install Tomcat 10.0.

Downloading the archive:

# wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.18/bin/apache-tomcat-10.1.18.tar.gz

Unpacking the archive into the previously created tomcat user directory (make sure that you have one Tomcat archive in the current directory, otherwise, instead of apache-tomcat-*.tar.gz specify the full name of the archive):

# tar zxvf apache-tomcat-*.tar.gz -C /opt/tomcat --strip-components 1

And we change the file permissions for the tomcat user.:

# chown -R tomcat:tomcat /opt/tomcat

Since Tomcat is currently running in manual mode, it will not start after restarting the server, and this needs to be fixed. To do this, we will configure the systemd service file, which will run Tomcat and, if it crashes, restart it.

Ubuntu/Debian

# update-java-alternatives -l 

The output will be something like this, the version values may vary:

java-1.11.0-openjdk-amd64  1111   /usr/lib/jvm/java-1.11.0-openjdk-amd64

Create the /etc/systemd/system/tomcat.service file and add the following to it:

[Unit]
Description=Apache Tomcat Server
After=network.target

[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"
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
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

Changing the value of JAVA_HOME to /usr/lib/jvm/java-1.11.0-openjdk-amd64 (may vary depending on the Java version).

CentOS/AlmaLinux/RockyLinux

Create the /etc/systemd/system/tomcat.service file and add the following to it:

[Unit]
Description=Apache Tomcat Server
After=syslog.target network.target

[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=CATALINA_PID=/opt/tomcat/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure
RestartSec=10


[Install]
WantedBy=multi-user.target

Applying settings and launching the Tomcat daemon

Further settings are the same for all operating systems.

To apply the settings, restart the daemon:

# systemctl daemon-reload

Launching Tomcat:

# systemctl start tomcat

We check that the service is up and running:

# systemctl status tomcat
tomcat.service - Apache Tomcat Server
  Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
  Active: active (running) since Fri 2023-02-03 13:04:30 MSK; 1min 4s ago

It will also be useful to set up Tomcat autorun after restarting the server.:

# systemctl enable tomcat

For CentOS, Alma Linux, and Rocky Linux, you will need to open port 8080 in the firewall.:

# firewall-cmd --permanent --add-port=8080/tcp
# firewall-cmd --reload

Go to the browser at the link: ip_server:8080

Configuring access to the web interface

Tomcat has a web interface for management, but access to it still needs to be configured, which is what we’re going to do now.

Creating a user by editing the file /opt/tomcat/conf/tomcat-users.xml , adding the following lines to the tomcat-users section:

<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<user username="USER_NAME" password="PASSWORD_USER" roles="admin-gui,manager-gui"/>

Next, you need to allow a specific IP to access the management web interface. To do this, you need to make changes to the file /opt/tomcat/webapps/manager/META-INF/context.xml .

We find the line:

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

We add a new IP through the straight line “|” (we specify our IP instead of 127.0.0.1), for example:

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|127.0.0.1" />

We do the same with the file /opt/tomcat/webapps/host-manager/META-INF/context.xml .

After all the settings, restart Tomcat.

# systemctl restart tomcat

Next, click on one of the three control points on the site to log in.:

You will be asked to enter your login and password for authorization. Specify the previously created data in the file /opt/tomcat/conf/tomcat-users.xml .

Changing the port

By default, Tomcat runs on port 8080. If necessary, you can change it in the file /opt/tomcat/conf/server.xml by finding the line <Connector port=”8080″ protocol=”HTTP/1.1″. We change 8080 to the desired port and restart Tomcat.

Installing the certificate

We place the key, certificate, and chain on the server.

Next, you need to combine the certificate and the chain using the command:

# cat certificate file_chains > cert.pem

The key is in the .pem format:

# mv file_key.pem key

Generating the .p12 file:

# openssl pkcs12 -export -out /tmp/cert_and_key.p12 -in cert.pem -inkey key.pem -name tomcat

You will be asked for a password, specify any one, and remember it.

Generating a certificate for Tomcat:

# keytool -importkeystore -deststorepass 'PASSWORD_MIN_6_SIGNS' -destkeypass'PASSWORD_MIN_6_SIGNS' -destkeystore /tmp/domain.jks -srckeystore /tmp/cert_and_key.p12 -srcstoretype PKCS12 -srcstorepass 'PASSWORD_IS_PREVIOUS_STEP' -alias tomcat

Moving the finished file:

# mv /tmp/domain.jks /opt/tomcat/conf/domain.jks

In the file /opt/tomcat/conf/server.xml we find the block:

<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
...

</Connector>
-->

Remove the at the edges of the block to uncomment it and make it look like:

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
            maxThreads="150" SSLEnabled="true">
     <SSLHostConfig>
         <Certificate certificateKeystoreFile="conf/domain.jks"
certificateKeystorePassword="PASSWORD_MIN_6_SIGNS"
type="RSA" />
</SSLHostConfig>
</Connector>

In the certificateKeystoreFile, specify the conf/domain file.jks is a previously created certificate that we have moved.

In certificateKeystorePassword, specify the password for the certificate.

Restarting Tomcat:

# systemctl restart tomcat