[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:                                                         amzn2-core  
java-1.8.0-openjdk-devel.x86_64                                                         1:                                                            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.

Description=Tomcat 9 servlet container  
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"  

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.

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

To allow external access, edit the following two files.


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