[Linux] Building an Amazon Linux 2 Server Part 2 (Tomcat Installation)


Writing time : 2021-12-22 20:55:02

Building an Amazon Linux 2 Server Part 2 (Tomcat Installation)

Let's install NginX, Tomcat, Jenkins on Amazon Linux 2 server
In this part, open JDK is installed and then Tomcat is installed.

Install java

Check if java is installed.

$ java  
-bash: java: command not found  


If installation is required, check the installable java version.

yum list java*jdk-devel  
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd  
Available Packages  
java-1.7.0-openjdk-devel.x86_64                                                         1:1.7.0.261-2.6.22.2.amzn2.0.2                                                         amzn2-core  
java-1.8.0-openjdk-devel.x86_64                                                         1:1.8.0.312.b07-1.amzn2.0.2                                                            amzn2-core  


Install the JDK.

sudo yum install -y java-1.8.0-openjdk-devel.x86_64  


Check the installed java version.

$ java -version  
openjdk version "1.8.0_312"  
OpenJDK Runtime Environment (build 1.8.0_312-b07)  
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)  


Install Tomcat

If JDK is installed, proceed with Tomcat installation.
First, add the tomcat user.

sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat  


Go to the tmp folder and download the tomcat file.

$ cd /tmp  
$ wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz  


Unzip and move the file to install Tomcat.

$ tar -xf apache-tomcat-9.0.56.tar.gz  
$ sudo mv apache-tomcat-9.0.56 /opt/tomcat/  


Create a symbolic link and change the tomcat folder settings.

$ sudo ln -s /opt/tomcat/apache-tomcat-9.0.56 /opt/tomcat/latest  
$ sudo chown -R tomcat: /opt/tomcat  
$ sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'  


Create tomcat.service file.

$ sudo vi /etc/systemd/system/tomcat.service  


Add the following to the tomcat.service file.

[Unit]  
Description=Tomcat 9 servlet 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"  
  
Environment="CATALINA_BASE=/opt/tomcat/latest"  
Environment="CATALINA_HOME=/opt/tomcat/latest"  
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"  
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"  
  
ExecStart=/opt/tomcat/latest/bin/startup.sh  
ExecStop=/opt/tomcat/latest/bin/shutdown.sh  
  
[Install]  
WantedBy=multi-user.target  


Register Tomcat to start when the server is rebooted and start Tomcat.

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


Check the status of started Tomcat.

$ sudo systemctl status tomcat  
● tomcat.service - Tomcat 9 servlet container  
   Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled)  
   Active: active (running) since 수 2021-12-22 00:47:47 UTC; 8s ago  
  Process: 15694 ExecStart=/opt/tomcat/latest/bin/startup.sh (code=exited, status=0/SUCCESS)  
 Main PID: 15701 (java)  
   CGroup: /system.slice/tomcat.service  
           └─15701 /usr/lib/jvm/jre/bin/java -Djava.util.logging.config.file=/opt/tomcat/latest/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogMana...  


Allow the firewall if necessary.
In AWS, you just need to add the port to open in the console.

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


Add the tomcat user.
Make a copy of the tomcat-users.xml file and start editing it.

$ sudo cp /opt/tomcat/latest/conf/tomcat-users.xml /opt/tomcat/latest/conf/tomcat-users-20211222.xml  
$ sudo vi /opt/tomcat/latest/conf/tomcat-users.xml  


Add role and user elements to the last part of tomcat-users.xml file.
The user element specifies the user name and password to log in.

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


To allow external access, edit the following two files.

/opt/tomcat/latest/webapps/manager/META-INF/context.xml  
/opt/tomcat/latest/webapps/host-manager/META-INF/context.xml  


First, make a copy of the first file and edit the file.

$ sudo cp /opt/tomcat/latest/webapps/manager/META-INF/context.xml /opt/tomcat/latest/webapps/manager/META-INF/context-20211222.xml  
$ sudo vi /opt/tomcat/latest/webapps/manager/META-INF/context.xml  


Comment out the elements below that start with Value in the file.

<!--  
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"  
         allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" />  
-->  


The rest of the files are also created and modified.

$ sudo cp /opt/tomcat/latest/webapps/host-manager/META-INF/context.xml /opt/tomcat/latest/webapps/host-manager/META-INF/context-20211222.xml  
$ sudo vi /opt/tomcat/latest/webapps/host-manager/META-INF/context.xml  


Comment out the Valve element.

<!--  
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"  
         allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" />  
-->  


Restart Tomcat.

$ sudo systemctl restart tomcat  


If you connect to http://ip:8080 and click the Manager App button, you can log in with the account added to the tomcat-users.xml file.

Other posts in the category