Introduction
Tomcat 9 is a popular open source web server and servlet container developed by the Apache Software Foundation. It is used to deploy and run Java-based web applications. Installing Tomcat 9 on CentOS 7 is a straightforward process and can be completed in a few simple steps. This guide will walk you through the process of installing Tomcat 9 on CentOS 7.
How to Install Tomcat 9 on CentOS 7
1. Download Tomcat 9
First, download the latest version of Tomcat 9 from the official Apache Tomcat website.
2. Install Java
Tomcat 9 requires Java 8 or later. To install Java 8, run the following command:
sudo yum install java-1.8.0-openjdk
3. Create Tomcat User
Create a new user and group for Tomcat with the following command:
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
4. Download Tomcat
Download the Tomcat 9 archive file using the following command:
wget http://mirrors.estointernet.in/apache/tomcat/tomcat-9/v9.0.37/bin/apache-tomcat-9.0.37.tar.gz
5. Extract Tomcat Archive
Extract the downloaded archive file to the /opt/tomcat directory with the following command:
sudo tar xzvf apache-tomcat-9.0.37.tar.gz -C /opt/tomcat –strip-components=1
6. Update Permissions
Change the ownership of the Tomcat directory with the following command:
sudo chown -R tomcat: /opt/tomcat
7. Create Systemd Service
Create a systemd service file for Tomcat with the following command:
sudo nano /etc/systemd/system/tomcat.service
Add the following lines:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=”JAVA_HOME=/usr/lib/jvm/jre”
Environment=”JAVA_OPTS=-Djava.security.egd=file:///dev/urandom”
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
Save and close the file when you are finished.
8. Start Tomcat Service
Start the Tomcat service and enable it to start on boot with the following command:
sudo systemctl start tomcat
sudo systemctl enable tomcat
9. Access Tomcat
Tomcat is now installed and running. You can access it using the URL http://your-server-ip:8080.
Introduction
Tomcat is an open source Java implementation package developed by the Apache Software Foundation. Learn how to install Tomcat 9 on CentOS 7 in this tutorial.
Prerequisites
- A user account with sudo privileges
- Access to a terminal window / command line (Ctrl-Alt-F2)
Check if Java is Installed
Tomcat relies on an existing Java installation. Check to see if your system has Java installed. Enter the following into a terminal window:
java -version
You should be running at least JDK 1.8.0. If the system reports an older version or no Java installed, install Java by entering:
sudo yum install java-1.8.0-openjdk-devel
Note: This guide uses OpenJDK SE (Standard Edition) 8. OpenJDK is fully open source. If your software uses Oracle Java, you can use it instead.
Create Tomcat User and Group
Tomcat should not be run as root. Create a new user and group by entering:
sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
Download Tomcat 9
Tomcat 9.0.20 is the latest version at the time this was written. A later release may be available on the official download page. Alternately, enter the following:
cd /tmp
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz
To extract the Tomcat tar.gz file to /opt/tomcat, enter the following:
sudo tar xzvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1
Modify Tomcat User Permissions
The new tomcat user needs execute privileges over the directory.
Enter the following:
sudo chown -R tomcat:tomcat /opt/tomcat
sudo sh -c 'chmod +x /opt/tomcat/bin/*.sh'
Create a System Unit File
Creating a systems unit file allows Tomcat to run as a service.
1. Find the Java location with the following command:
readlink -f $(which java)
Copy the parent folder of /jre/bin/java for the following step.
1. To create a tomcat.service file, use the command:
sudo nano /etc/systemd/system/tomcat.service
2. In the file, enter the following:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/"
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
[Install]
WantedBy=multi-user.target
Paste the path from the previous step in the Environment="JAVA_HOME=<path>"
line.
3. Save and close the file.
4. Refresh the system:
sudo systemctl daemon-reload
5. Set the Tomcat service to start on boot:
sudo systemctl enable tomcat
6. Start the Tomcat service:
sudo systemctl start tomcat
7. Verify that the Tomcat service is installed and running:
sudo systemctl status tomcat
Adjust the Firewall
The Tomcat service needs access to Port 8080.
Allow traffic by entering the commands:
firewall-cmd --zone=public --permanent --add-port=8080/tcp
The message success
prints to the terminal. Reload the firewall to apply the option:
firewall-cmd --reload
You should be able to see the Tomcat server in a web browser.
Input this web address into a browser window:
http://server_ip:8080
For example, if you’re running Tomcat on a local server, use:
http://localhost:8080
Set Up Web Management Interface
1. To create a user to access the Web Management Interface, edit the user file by entering:
sudo nano /opt/tomcat/conf/tomcat-users.xml
2. Delete everything from the file and add the following:
<tomcat-users>
<!--
Comments
-->
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<user username="admin" password="good_password" roles="admin-gui,manager-gui"/>
</tomcat-users>
Replace good_password with a secure password of your choosing.
Save the file and exit. You should now be able to access the Web Management Interface in a web browser. Visit http://server_ip:8080/manager/html to use the interface.
Configure Remote Access (Optional)
By default, Tomcat is only accessible from the local machine it’s installed on. This step allows you to grant access to a specific IP address.
1. Edit the following file:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
2. Add the following:
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.0.*" />
3. Save the file and exit.
4. Repeat the process for the second file:
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
5. Add the following:
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.0.*" />
6. Save and exit.
This will grant access to any system in the 192.168.0.* range of IP addresses.
You can change the IP address to a specific range for your intranet. Alternately, you can use the IP address of a single system.
The asterisk acts as a wildcard to allow multiple IP addresses. Granting full access can leave security vulnerabilities. Instead, enable only systems with a business need to access Tomcat.
Conclusion
You should have a working installation of Apache Tomcat 9 on your CentOS server. Furthermore, you should be able to access your Tomcat server from a specific IP range or address in your intranet.
How to Install Tomcat 9 on CentOS 7
Tomcat 9 is a popular open source web server and servlet container developed by the Apache Software Foundation. It is used to deploy and serve Java-based web applications. In this tutorial, we will show you how to install Tomcat 9 on a CentOS 7 server.
Prerequisites
Before you begin with this guide, you should have a non-root user with sudo privileges configured on your server. You can learn how to set up a user with these privileges by following our initial server setup guide for CentOS 7.
You will also need to have Java installed on your server. You can install the latest version of Java 8 with the following command:
sudo yum install java-1.8.0-openjdk-devel
Step 1 — Downloading Tomcat 9
The first step is to download the latest version of Tomcat 9 from the Tomcat 9 downloads page. At the time of writing, the latest version is 9.0.14.
You can download the latest version of Tomcat 9 with the following command:
wget http://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.14/bin/apache-tomcat-9.0.14.tar.gz
Step 2 — Installing Tomcat 9
Now that you have downloaded the Tomcat 9 archive, you can extract it to the /opt
directory with the following command:
sudo tar xzvf apache-tomcat-9*tar.gz -C /opt
Next, rename the extracted directory to tomcat9
with the following command:
sudo mv /opt/apache-tomcat-9* /opt/tomcat9
Next, create a tomcat
user and group with the following command:
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat9 tomcat
Next, give the tomcat
user ownership of the Tomcat 9 directory with the following command:
sudo chown -R tomcat: /opt/tomcat9
Step 3 — Configuring Tomcat 9
Now that Tomcat 9 is installed, you can configure it. You can do this by editing the /opt/tomcat9/conf/tomcat-users.xml
file:
sudo nano /opt/tomcat9/conf/tomcat-users.xml
Add the following lines to the file:
<role rolename="manager-gui"/>
<user username="admin" password="password" roles="manager-gui"/>
Save and close the file when you are finished.
Step 4 — Creating a systemd Service File
Next, you will need to create a systemd service file to manage the Tomcat service. You can do this by creating a tomcat.service
file in the /etc/systemd/system
directory:
sudo nano /etc/systemd/system/tomcat.service
Add the following lines to the file:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/opt/tomcat9/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat9
Environment=CATALINA_BASE=/opt/tomcat9
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat9/bin/startup.sh
ExecStop=/opt/tomcat9/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
Save and close the file when you are finished.
Step 5 — Starting Tomcat 9
Now, you can start the Tomcat 9 service and enable it to start on boot with the following commands:
sudo systemctl start tomcat
sudo systemctl enable tomcat
You can check the status of the Tomcat service with the following command:
sudo systemctl status tomcat
You should see the following output:
● tomcat.service - Apache Tomcat Web Application Container
Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2018-09-20 11:45:17 UTC; 1min 10s ago
Main PID: 8092 (java)
CGroup: /system.slice/tomcat.service
└─8092 /usr/lib/jvm/jre/bin/java -Djava.util.logging.config.file=/opt/tomcat9/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /opt/tomcat9/bin/bootstrap.jar:/opt/tomcat9/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat9 -Dcatalina.home=/opt/tomcat9 -Djava.io.tmpdir=/opt/tomcat9/temp org.apache.catalina.startup.Bootstrap start
Sep 20 11:45:17 centos7 systemd[1]: Started Apache Tomcat Web Application Container.
Step 6 — Configuring the Firewall
If you have a firewall enabled on your server, you will need to open port 8080 to allow external access to the Tomcat 9 web interface. You can do this by running the following command:
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
Step 7 — Accessing the Tomcat 9 Web Interface
Now, you can access the Tomcat 9 web interface by visiting your server’s domain name or public IP address followed by :8080
in your web browser:
http://your_server_ip_or_domain:8080
You should see the Tomcat 9 welcome page in the following image:
You can log in to the Tomcat 9 web interface with the username admin and the password you set in the tomcat-users.xml
file.
Conclusion
In this guide, you installed Tomcat 9 on your CentOS 7 server and configured it to run as a systemd service. You also learned how to open the Tomcat 9 web interface in your web browser.
Now that Tomcat 9 is installed, you can deploy your Java web applications to it.