Apache Tomcat is an open-source Java Servlet and JavaServer Pages implementation that provides a pure Java HTTP web server environment for running Java code. It is widely used for deploying and running Java-based web applications. In this step-by-step guide, we will show you how to install and set up Apache Tomcat 8 on CentOS 7 or RHEL 7.
Step 1: Install Java Development Kit (JDK)
Apache Tomcat requires the Java Development Kit (JDK) to run. First, install the JDK using the following commands:
sudo yum install -y java-1.8.0-openjdk-devel
After installation, verify the installed JDK version:
java -version
Step 2: Create Tomcat User
For security reasons, it’s recommended to create a separate user to run the Tomcat service:
sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
Step 3: Download and Install Apache Tomcat
Download the latest version of Apache Tomcat 8 from the official website (replace the version number if needed):
wget http://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.73/bin/apache-tomcat-8.5.73.tar.gz
Extract the downloaded archive and move it to the /opt/tomcat directory:
sudo tar xf apache-tomcat-8.5.73.tar.gz -C /opt/tomcat --strip-components=1
Step 4: Configure Tomcat User Permissions
Set the proper ownership and permissions for the Tomcat directory:
sudo chown -R tomcat:tomcat /opt/tomcat sudo chmod -R g+r /opt/tomcat/conf sudo chmod g+x /opt/tomcat/conf
Step 5: Create a Systemd Service File
To manage the Tomcat service easily, create a systemd service file:
sudo vi /etc/systemd/system/tomcat.service
Add the following content to the file:
[Unit] Description=Apache Tomcat 8 Servlet Container After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/jre" 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 [Install] WantedBy=multi-user.target
Save and close the file.
Step 6: Start and Enable Tomcat Service
Reload the systemd daemon and start the Tomcat service:
sudo systemctl daemon-reload sudo systemctl start tomcat sudo systemctl enable tomcat
Step 7: Adjust Firewall Rules
If you have a firewall enabled, allow traffic to the default Tomcat port (8080):
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp sudo firewall-cmd --reload
Step 8: Verify Tomcat Installation
To verify that the Tomcat installation was successful, open a web browser and access the Tomcat server using the following URL:
http://your_server_ip:8080
You should see the Apache Tomcat default welcome page.
Step 9: Secure the Tomcat Manager
By default, the Tomcat Manager and Host Manager web applications are not secured. To restrict access, edit the tomcat-users.xml file:
sudo vi /opt/tomcat/conf/tomcat-users.xml
Add the following lines before the closing </tomcat-users> tag:
<role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="your_username" password="your_password" roles="manager-gui,admin-gui"/>
Replace your_username and your_password with your desired credentials.
Save and close the file. Restart the Tomcat service for the changes to take effect:
sudo systemctl restart tomcat
Now, you can access the Tomcat Manager and Host Manager using the following URLs:
http://your_server_ip:8080/manager/html http://your_server_ip:8080/host-manager/html
Conclusion
In this guide, we have shown you how to install and set up Apache Tomcat 8 on a CentOS 7 or RHEL 7 system. You can now deploy and manage your Java-based web applications using the Tomcat servlet container.
Remember to keep your system and Tomcat installation up-to-date with the latest security patches and updates. Additionally, maintain good security practices by regularly reviewing and updating user access, permissions, and credentials.
If you have any questions, comments, or suggestions for improvement, please feel free to share your thoughts in the comments section below. Your feedback is invaluable to us, and it helps us create better and more informative content for our users.
7 Comments
Nice post. It just made my tomcat setup quick & easy and saved me few hours. Thank you.
Thanks for the useful tips and tricks! Also great blog here with all of the valuable information you have. Keep up the good work you are doing here
Thanks for sharing this article.
good post. Saved couple of hours. Thank you. Suman
Thanks for sharing
Very Good post, Thanks for the tricks
Thanks for the article.