Tomcat Server Install on CentOS

Tomcat is an application server or web server?

When package or compile an application, it can either be packaged as a .war or .ear, Tomcat is categorized as a web server since it can handle only .war files. But in the traditional developer world, we can refer to the tomcat as an application server, where it can host strictly Java Web-based Applications.

A web server is responsible for process servlets and JSP’s, whereas an application server, should be able to process EJB’s(Enterprise Java Beans), JSF (Java Server Faces) along with servlets and JSP’s. Tomcat can process servlets, JSP’s, Java Expression Language, and Java WebSocket technologies thus making it more than a web server.

A web server is often said to be a part of an application server since an application server exhibits all the features that a web server possesses and in addition to it also contains attributes such as load balancing, data persistence, messaging to name a few. However, vice versa does not apply.


Web Server:

Web server just serves the web pages and some dynamic static pages and it cannot enforce any application logic.

Apache Tomcat vs Apache HTTPd:

The biggest difference between Tomcat and Apache HTTP Server is that Tomcat runs on a JVM. Apache HTTP Server also is known as the Apache webserver and requires only a modern Windows, Linux distribution, or Unix to run. Tomcat users will need to deploy a JDK and properly configure the JAVA_HOME variable to run. The final conclusion is, an application server also contains the webserver.

Follow the steps to achieve the setup of the tomcat application server.  

Step1: Start with JDK installation, as it is mandatory and also a pre-requisite to make tomcat server up and run on Java Runtime Environment. 

$ sudo yum install java-1.8.0-openjdk-devel

$ export JAVA_HOME=/usr/lib/jvm/jre

cp ~/.bashrc ~/.bashrc_backup

sed -i -e '$aJAVA_HOME=/usr/lib/jvm/jre' ~/.bashrc  # Keep the JAVA_HOME consistent through out reboots and new shell prompts. 

Step2: Prepare the right level of access for this tomcat application server to administrate.

$ sudo groupadd tomcat   # Create a group for tomcat user

sudo useradd -M -s /bin/nologin -g tomcat -d /opt/tomcat tomcat  # Create a tomcat user with no shell login capability

Step3: Download or copy the binaries to the desired home directory and assign the right user and group (home: /opt/tomcat, User&group: tomcat)

$ wget https://downloads.apache.org/tomcat/tomcat-8/v8.5.59/bin/apache-tomcat-8.5.59.zip

# Choose the desired version from offical website 
     https://downloads.apache.org/tomcat/tomcat-9/ 
     https://downloads.apache.org/tomcat/tomcat-8/

$ unzip -d /opt/tomcat apache-tomcat-8.5.59.zip

$ sudo chown -R tomcat:tomcat /opt/tomcat/apache-tomcat-8.5.59 && cd /opt/tomcat/apache-tomcat-8.5.59 && sudo chmod 755 webapps temp work && sudo chmod +x bin

Step4 : Try to start in traditional way and check everything was working as expected.

 /opt/tomcat/apache-tomcat-8.5.59/bin/starup.sh 

/opt/tomcat/apache-tomcat-8.5.59/bin/shutdown.sh

$ curl http://localhost:8080 # ( If fails, check the installed firewall utility and rules)

Firewall rules with IPtable:

sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

sudo /sbin/iptables-save > /etc/sysconfig/iptables  (or)

sudo service iptables save

Firewalld: 

firewall-cmd --permanent --zone=public --add-port=8080/tcp

firewall-cmd --reload

Step4:

Make the systemd to manage httpd service:

cat > /etc/systemd/system/tomcat.service

# Systemd unit file for tomcat
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/opt/tomcat/
apache-tomcat-8.5.59/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat/apache-tomcat-8.5.59
Environment=CATALINA_BASE=/opt/tomcat/
apache-tomcat-8.5.59
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/
apache-tomcat-8.5.59/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat
#UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

$ sudo systemctl daemon-reload && sudo systemctl start tomcat && sudo systemctl enable tomcat

Step5:

Create an Admin user to access the manager application:

sudo vi /opt/tomcat/apache-tomcat-8.5.59/conf/tomcat-users.xml  (# Add the below section at the end of the tomcat-users.xml file)

<tomcat-users>
    <user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>

For manager app type comment out the below section to remove the IP restriction:

$ sudo vi /opt/tomcat/apache-tomcat-8.5.59/webapps/host-manager/META-INF/context.xml

<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" />-->
</Context>



Conclusion: Install and configure of basic tomcat is quite straightway but the power and usage of the tomcat server are quite immense, so try to explore more use cases and complex setup to support production application with high availability and fault-tolerant environment setup. 


Comments

Popular Posts