How to Install Apache Tomcat in virtual server running in VirtualBox Application using CentOS 8

Posted on

Introduction

This article is an article about how to install Apache Tomcat Webserver. The installation process exist in a virtual server running using CentOS 8 operating system. Furthermore, it is actually a virtual server exist and running in a VirtualBox application. The following content is a content where the main reference exist in this link. It is an article with the title of ‘Install Apache Tomcat 9 on CentOS 8 / RHEL 8’. There are several steps in order to be able to achieve it. There is a preparation part, installation and configuration part. So, the following are the explanation of all those parts.

Preparation Part for Installing Apache Tomcat Webserver

This part is the preparation part in order for installing Apache Tomcat Webserver. The following are those steps :

  1. First of all, just start the VirtualBox application. The following image will appear :

    How to Install Apache Tomcat in virtual server running in VirtualBox Application using CentOS 8
    How to Install Apache Tomcat in virtual server running in VirtualBox Application using CentOS 8
  2. Then, run the virtual server. After the virtual server is running, just access it directly or remotely. By accessing it remotely, it is using a certain method such as SSH connection. Read the article with the title ‘How to Remote CentOS Virtual Server running in a VirtualBox with a NAT Network using SSH’ in this link for more information.

  3. Access the command line interface. For the first time, download the Apache Tomcat Webserver installer. Use any available tool for downloading the Apache Tomcat Webserver installer. In this context, it is using ‘wget’ tool. The following is the command execution :

    [root@10 ~]# wget http://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
    --2021-03-28 06:11:49--  http://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
    Resolving archive.apache.org (archive.apache.org)... 138.201.131.134, 2a01:4f8:172:2ec5::2
    Connecting to archive.apache.org (archive.apache.org)|138.201.131.134|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 9818695 (9.4M) [application/x-gzip]
    Saving to: ‘apache-tomcat-9.0.8.tar.gz’
    
    apache-tomcat-9.0.8.tar.gz                 100%[========================================================================================>]   9.36M  72.2KB/s    in 33s
    
    2021-03-28 06:12:22 (293 KB/s) - ‘apache-tomcat-9.0.8.tar.gz’ saved [9818695/9818695]
    
    [root@10 ~]#
  4. Extract the installer of Apache Tomcat Webserver as follows :

    [root@10 ~]# tar -zxvf apache-tomcat-9.0.8.tar.gz
    apache-tomcat-9.0.8/conf/
    apache-tomcat-9.0.8/conf/catalina.policy
    apache-tomcat-9.0.8/conf/catalina.properties
    apache-tomcat-9.0.8/conf/context.xml
    apache-tomcat-9.0.8/conf/jaspic-providers.xml
    apache-tomcat-9.0.8/conf/jaspic-providers.xsd
    apache-tomcat-9.0.8/conf/logging.properties
    apache-tomcat-9.0.8/conf/server.xml
    apache-tomcat-9.0.8/conf/tomcat-users.xml
    apache-tomcat-9.0.8/conf/tomcat-users.xsd
    apache-tomcat-9.0.8/conf/web.xml
    apache-tomcat-9.0.8/bin/
    apache-tomcat-9.0.8/lib/
    apache-tomcat-9.0.8/logs/
    apache-tomcat-9.0.8/temp/
    apache-tomcat-9.0.8/webapps/
    apache-tomcat-9.0.8/webapps/ROOT/
    apache-tomcat-9.0.8/webapps/ROOT/WEB-INF/
    ...
    ...
    [root@10 ~]#
    
  5. Copy the extracted installer of Apache Tomcat Webserver to ‘/usr/share’. This is actually a preferred location. Sometime there are another references which is suggesting to put it in ‘/opt’. In this context, it is using the suggested pattern of ‘/usr/share’. Execute it as the following command :

  6. [root@10 ~]# cp -rv apache-tomcat-9.0.8/ /usr/share
    ....
    [root@10 ~]#
    
  7. Following after, create a new symbolic link to make it simpler for accessing the root folder or home directory of the Apache Tomcat Webserver :
  8. [root@10 ~]#ln -s /usr/share/apache-tomcat-9.0.8 /usr/share/tomcat
    ...
    [root@10 ~]
    
  9. Create a user for running and owning the service of the Apache Tomcat Webserver. Execute the following command :
  10. [root@10 ~]# sudo groupadd --system tomcat
    [root@10 ~]# sudo useradd -d /usr/share/tomcat -r -s /bin/false -g tomcat tomcat
    [root@10 ~]#
    
  11. Do not forget to change the ownership of the Apache Tomcat Webserver and also the symbolic link created in the previous step as follows :
  12. [root@10 ~]# chown -Rv tomcat.tomcat /usr/share/tomcat 
    [root@10 ~]# chown -Rv tomcat.tomcat /usr/share/tomcat-9.0.8/
    ...
    [root@10 ~]#
    

Configuration Part for Installing Apache Tomcat Webserver

After executing several steps for preparing the Apache Tomcat Webserver, the following part is focusing on configuring Apache Tomcat Webserver. The following are steps for configuring it :

  1. The first step is just simply to create a new file configuration for maintaining the service. Execute the following command :

      
    [root@10 ~]# vim /etc/systemd/system/tomcat.service
    

    The above command is a command to create a new file with the name of ‘tomcat.service’. Just fill the file with the following content :

    [Unit]
    Description=Tomcat Server
    After=syslog.target network.target
    
    [Service]
    Type=forking
    User=tomcat
    Group=tomcat
    
    Environment=JAVA_HOME=/usr/lib/jvm/jre
    Environment='JAVA_OPTS=-Djava.awt.headless=true'
    Environment=CATALINA_HOME=/usr/share/tomcat
    Environment=CATALINA_BASE=/usr/share/tomcat
    Environment=CATALINA_PID=/usr/share/tomcat/temp/tomcat.pid
    Environment='CATALINA_OPTS=-Xms512M -Xmx1024M'
    ExecStart=/usr/share/tomcat/bin/catalina.sh start
    ExecStop=/usr/share/tomcat/bin/catalina.sh stop
    
    [Install]
    WantedBy=multi-user.target
    

    The file is a service script for managing Apache Tomcat Webserver’s service. It is to start, restart and stop the service.

  2. After creating the service file, execute the following command to implement the new added service’s script :
    [root@10 ~]# systemctl daemon --reload
    [root@10 ~]# systemctl start tomcat
    [root@10 ~]# systemctl enable tomcat
    
  3. Finally, access the Apache Tomcat Webserver’s service from a web browser. Basically the address will be at localhost:8080. But since it is running on a virtual server, it has a special way for accessing it. It need a rule which is called the port forwarding rule. The rule enable for forwarding an incoming request in the host machine forwarded to the virtual machine. Since it is runnning on port 8080 in the virtual machine or the guest machine side, just add any port in the host machine side. Read the article with the title of ‘How to Add Port Forwarding Rule to access Code Igniter 4 Service of Guest Machine running in VirtualBox Manager from Host Machine’ in this link to do it.

  4. Last but not least, access the Apache Tomcat Webserver. In this article’s example, the host machine for accepting the incoming request to the port 8080 of the guest machine is port ‘9080’. So, in order to access the Apache Tomcat Webserver’s service, just access the address of ‘localhost:9080’ as available in the following image :

    How to Install Apache Tomcat in virtual server running in VirtualBox Application using CentOS 8
    How to Install Apache Tomcat in virtual server running in VirtualBox Application using CentOS 8

Leave a Reply