Skip to content

Fast Install Java and Tomcat

If you'd like to install Java 8 and Tomcat 8 on Enterprise Linux (EL) 7, please refer to another post : How to Install Java 8 and Tomcat 8 on Enterprise Linux 7.

Normally, seeking and installing the newest versions of Java and Tomcat are quite trivial and should be aware of many configuration issues. If you don't mind the not-so-new but verified versions of the twos, you can save your energy on seeking and installing by yum.

Enterprise Linux 7 hereby means Red Hat Enterprise Linux 7 or its clones like CentOS 7 or Oracle Enterprise Linux 7, etc.

Let's see what versions of Java and Tomcat we are going to have.

[root@test ~]# yum info java-1.7.0-openjdk-devel tomcat
...

OK, Java is 1.7.0.65 and Tomcat is 7.0.42 in repository.

Install Java 7 as well as Tomcat 7 via yum without prompt.

[root@test ~]# yum -y install java-1.7.0-openjdk-devel tomcat
...

On EL 7, there's a new service control mechanism named systemctld. If we want to startup / shutdown Tomcat through systemctld, we must have a service delegates Tomcat to operate with. Before we decide to create a customized Tomcat service, we can check whether a formal tomcat.service exists or not, which can save our time.

[root@test ~]# systemctl status tomcat.service
tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled)
   Active: inactive (dead)

[root@test ~]# systemctl show tomcat.service
Id=tomcat.service
Names=tomcat.service
Requires=basic.target
Wants=system.slice
Conflicts=shutdown.target
Before=shutdown.target
After=syslog.target network.target systemd-journald.socket basic.target system.s
Description=Apache Tomcat Web Application Container
LoadState=loaded
ActiveState=inactive
SubState=dead
FragmentPath=/usr/lib/systemd/system/tomcat.service
UnitFileState=disabled
InactiveExitTimestampMonotonic=0
ActiveEnterTimestampMonotonic=0
ActiveExitTimestampMonotonic=0
InactiveEnterTimestampMonotonic=0
CanStart=yes
CanStop=yes
CanReload=no
CanIsolate=no
StopWhenUnneeded=no
RefuseManualStart=no
RefuseManualStop=no
AllowIsolate=no
DefaultDependencies=yes
OnFailureIsolate=no
IgnoreOnIsolate=no
IgnoreOnSnapshot=no
NeedDaemonReload=no
JobTimeoutUSec=0
ConditionTimestampMonotonic=0
ConditionResult=no
Transient=no
Slice=system.slice
Type=forking
Restart=no
NotifyAccess=none
RestartUSec=100ms
TimeoutStartUSec=1min 30s
TimeoutStopUSec=1min 30s
WatchdogUSec=0
WatchdogTimestampMonotonic=0
StartLimitInterval=10000000
StartLimitBurst=5
StartLimitAction=none
ExecStart={ path=/usr/sbin/tomcat-sysd ; argv[]=/usr/sbin/tomcat-sysd start ; ig
ExecStop={ path=/usr/sbin/tomcat-sysd ; argv[]=/usr/sbin/tomcat-sysd stop ; igno
PermissionsStartOnly=no
RootDirectoryStartOnly=no
RemainAfterExit=no
GuessMainPID=yes
MainPID=0
ControlPID=0
Result=success
UMask=0022
LimitCPU=18446744073709551615
LimitFSIZE=18446744073709551615
LimitDATA=18446744073709551615
LimitSTACK=18446744073709551615
LimitCORE=18446744073709551615
LimitRSS=18446744073709551615
LimitNOFILE=4096
LimitAS=18446744073709551615
LimitNPROC=14573
LimitMEMLOCK=65536
LimitLOCKS=18446744073709551615
LimitSIGPENDING=14573
LimitMSGQUEUE=819200
LimitNICE=0
LimitRTPRIO=0
LimitRTTIME=18446744073709551615
OOMScoreAdjust=0
Nice=0
IOScheduling=0
CPUSchedulingPolicy=0
CPUSchedulingPriority=0
TimerSlackNSec=50000
CPUSchedulingResetOnFork=no
NonBlocking=no
StandardInput=null
StandardOutput=journal
StandardError=inherit
TTYReset=no
TTYVHangup=no
TTYVTDisallocate=no
SyslogPriority=30
SyslogLevelPrefix=yes
SecureBits=0
CapabilityBoundingSet=18446744073709551615
User=tomcat
Group=tomcat
MountFlags=0
PrivateTmp=no
PrivateNetwork=no
SameProcessGroup=no
IgnoreSIGPIPE=yes
NoNewPrivileges=no
KillMode=control-group
KillSignal=15
SendSIGKILL=yes
SendSIGHUP=no
CPUAccounting=no
CPUShares=1024
BlockIOAccounting=no
BlockIOWeight=1000
MemoryAccounting=no
MemoryLimit=18446744073709551615
DevicePolicy=auto
ExecMainStartTimestampMonotonic=0
ExecMainExitTimestampMonotonic=0
ExecMainPID=0
ExecMainCode=0
ExecMainStatus=0

Great! A formal tomcat.service exists (but is disabled). This could be the best part of this approach, we don't have to create the service by ourselves, because creating a systemctl service is quite complicated.

If you're interested in creating a Tomcat service by yourselves, please refer to the following post: How to Start Tomcat 8 Automatically at Server Boot on Enterprise Linux 7.

Enable tomcat.service for starting up automatically at server boot.

[root@test ~]# systemctl enable tomcat.service
ln -s '/usr/lib/systemd/system/tomcat.service' '/etc/systemd/system/multi-user.target.wants/tomcat.service'
[root@test ~]# systemctl status tomcat.service
tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled)
   Active: inactive (dead)

Startup Tomcat in current Linux instance.

[root@test ~]# systemctl start tomcat.service
[root@test ~]# systemctl status tomcat.service -l
tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled)
   Active: active (running) since Thu 2014-08-21 19:28:24 CST; 2s ago
  Process: 2517 ExecStart=/usr/sbin/tomcat-sysd start (code=exited, status=0/SUCCESS)
 Main PID: 2535 (java)
   CGroup: /system.slice/tomcat.service
           ??2535 java -classpath /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat -Dcatalina.home=/usr/share/tomcat -Djava.endorsed.dirs= -Djava.io.tmpdir=/var/cache/tomcat/temp -Djava.util.logging.config.file=/usr/share/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager org.apache.catalina.startup.Bootstrap start

Aug 21 19:28:24 test.example.com
#localhost.localdomain systemd[1]: Starting Apache Tomcat Web Application Container...
Aug 21 19:28:24 test.example.com
#localhost.localdomain systemd[1]: Started Apache Tomcat Web Application Container.

Open port for public http (i.e. 80) accesses for both runtime and permanent. Since firewalld is newly introduced on EL 7, you should be aware of the settings below.

[root@test ~]# firewall-cmd --zone=public --add-service=http
success
[root@test ~]# firewall-cmd --zone=public --add-service=http --permanent
success

Forward port 80 to tomcat port 8080 for both runtime and permanent.

[root@test ~]# firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080
success
[root@test ~]# firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080 --permanent
success

By forwarding all the traffic from port 80 to port 8080 of Tomcat, you need not to open port 8080 on firewall and clients also need not to specify the port number in URL explicitly. I will explain more on the following post: How to Make Tomcat Respond to Port 80.

Leave a Reply

Your email address will not be published. Required fields are marked *