Java runtime environment construction method (Tomcat @ Linux)

1. Initial introduction

There are open source openjdk and adoptjdk as java that can be used on Linux. Furthermore, organize up to tomcat environment construction The basic flow is --java package installation --java switching --Path definition such as JAVA_HOME in profile

1.java install


#Package installation
###RHEL/CentOS
sudo yum -y install java-11-openjdk java-11-openjdk-devel

###Amazon Linux2
sudo amazon-linux-extras install java-openjdk11 java-11-openjdk-devel

2.OS default java switching method



#java switching
[root@XXX ~]$ alternatives --config java

There are 4 programs'java'To provide.

Select command
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.amzn2.0.1.x86_64/jre/bin/java)
   2           java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.231-2.6.19.1.amzn2.0.1.x86_64/jre/bin/java)
   3           /usr/lib/jvm/adoptopenjdk-11-openj9/bin/java
   4           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.5.10-0.amzn2.x86_64/bin/java)

Press Enter to select the current[+]Or enter the selection number:4


#java body confirmation
[root@XXX ~]# java -version
openjdk version "11.0.5" 2019-10-15 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.5+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.5+10-LTS, mixed mode, sharing)

You can switch the default java above, but the mechanism is ...

How java switching works


#Click here for the default java command.
[root@XXX ~]# which java
/usr/bin/java

#When ls, it becomes a symbolic link
[root@XXX ~]# ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 November 6 01:19 /usr/bin/java -> /etc/alternatives/java

#Furthermore, when the link destination is ls, it becomes a link to java switched by java switching. Therefore, the default java command is switched by the above switching method.
[root@XXX ~]# ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 77 November 6 01:19 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.amzn2.0.1.x86_64/jre/bin/java

3.Profile switching


cat >> ~/.bash_profile <<EOF
export JAVA_HOME=$(dirname $(dirname $(readlink $(readlink $(which java)))))
export PATH=\$PATH:\$JAVA_HOME/bin
EOF

Install the tomcat module. Introduced tarball v9.0.30 instead of rpm.

4.tomcat installation&Defrost


curl -O https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.30/bin/apache-tomcat-9.0.30.tar.gz
tar zxvf apache-tomcat-*.tar.gz -C /usr/libexec/
chown -R tomcat. /usr/libexec/apache-tomcat-*
ln -s /usr/libexec/apache-tomcat-* /usr/libexec/tomcat9
useradd -d /usr/libexec/tomcat9 tomcat

5.Systemd setting for startup


##Unit file creation
cat << EOF > /etc/systemd/system/tomcat9.service
[Unit]
Description=Apache Tomcat 9
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/libexec/tomcat9/bin/startup.sh
ExecStop=/usr/libexec/tomcat9/bin/shutdown.sh
RemainAfterExit=yes
User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target
EOF

6.Environment variable definition for tomcat


cat << EOF > /usr/libexec/tomcat9/bin/setenv.sh
#!/bin/sh
JAVA_OPTS="-server -Xms512M -Xmx512M"
export JAVA_OPTS
EOF

By the way, in tomcat, either CATALINA_OPTS or JAVA_OPTS can be placed in setenv.sh, but the difference between the two is as follows. In conclusion, it seems that it is recommended for tomcat to go with CATALINA_OPTS unification.

python


https://stackoverflow.com/questions/11222365/catalina-opts-vs-java-opts-what-is-the-difference
>>>
So why are there two different variables? And what's the difference?

Firstly, anything specified in EITHER variable is passed, identically, to the command that starts up Tomcat - the "start" or "run" command - but only values set in JAVA_OPTS are passed to the "stop" command. That probably doesn't make any difference to how Tomcat runs in practise as it only effects the end of a run, not the start.
→ JAVA even during stop processing_OPTS takes arguments.

The second difference is more subtle. Other applications may also use JAVA_OPTS, but only Tomcat will use CATALINA_OPTS. So if you're setting environment variables for use only by Tomcat, you'll be best advised to use CATALINA_OPTS, whereas if you're setting environment variables to be used by other java applications as well, such as by JBoss, you should put your settings in JAVA_OPTS.
→JAVA_OPTS may also refer to other application servers other than tomcat. CATALINA_OPTS uses only tomcat.

7.Automatic start


systemctl enable tomcat9 --now

2. Tomcat various settings

Application manager

If you set the following in $ {CATALINA_HOME} /conf/tomcat-user.xml, you can access http: // : 8080 / Various functions can be accessed after drawing the application manager.

${CATALINA_HOME}/conf/tomcat-user.xml


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

Session timeout / session security

Set in $ {CATALINA_HOME} \ conf \ web.xml. The unit is minutes. After setting, restarting tomcat is required.

${CATALINA_HOME}/conf/web.xml


    <session-config>
        <session-timeout>5</session-timeout>
  	<cookie-config>
    	   <httponly>true</httponly>
    	   <secure>true</secure>
	</cookie-config>
    </session-config>

access log

In Tomcat, access logging is implemented as one of the extensions, so it is set in server.xml instead of logging.properties. The meaning of each definition of pattern is described in detail in this manual, but basically it seems that customization is sufficient. https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html

ruby:setting file:${CATALINA_HOME}/conf/server.xml


        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

ruby:${CATALINA_HOME}/logs/localhost_access_log.yyyy-mm-dd.txt(Access log output above)


127.0.0.1 - - [01/Jan/2020:01:07:20 +0900] "GET /hoge2/SessionTest HTTP/1.0" 404 864
127.0.0.1 - - [01/Jan/2020:01:07:21 +0900] "GET /favicon.ico HTTP/1.0" 200 21630
127.0.0.1 - - [01/Jan/2020:01:07:26 +0900] "GET /hoge/SessionTest HTTP/1.0" 200 35

Connection pool

Thread pool

3. Create test app

App placement

App placement


#Since hoge is an application name, any value can be used. hoge=Servlet part
mkdir -p /usr/libexec/tomcat9/webapps/hoge/WEB-INF/classes
chown tomcat:tomcat -R /usr/libexec/tomcat9/webapps/hoge/
vi /usr/libexec/tomcat9/webapps/hoge/WEB-INF/classes/HelloWorld.java
##################
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<h1>Hello World!</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}
##################

cd /usr/libexec/tomcat9/webapps/hoge/WEB-INF/classes
javac -classpath /usr/libexec/tomcat9/lib/servlet-api.jar HelloWorld.java

URL definition

Define the following in web.xml </ strong> --Servlet-class: Specify the name of the servlet class file. --Servlet-name: Any name that uniquely identifies each Servlet. --url-pattern: Specify the URL to access the Servlet.

The entity of the servlet is listed in the servlet tag. URLs are listed in the servlet-mapping tag. And the structure that links both is called servlet-name.

For example, by defining the following, it is possible to access the web application placed in advance by accessing http: // : 8080 / hoge / HWPage.

5.URL definition


vi /usr/libexec/tomcat9/webapps/hoge/WEB-INF/web.xml
##################
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

  <servlet>
    <servlet-name>HelloWorldName</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>HelloWorldName</servlet-name>
    <url-pattern>/HWPage</url-pattern>
  </servlet-mapping>

</web-app>
##################

Reference </ strong> https://www.ipentec.com/document/java-servlet-post-http-servlet-show-table https://commons.apache.org/proper/commons-dbcp/configuration.html https://www.techscore.com/tech/Java/JavaEE/JDBC/6-2/ https://www.ipa.go.jp/security/awareness/vendor/programmingv2/contents/302.html https://tomcat.apache.org/tomcat-9.0-doc/config/executor.html

  • java11 Manual https://docs.oracle.com/en/java/javase/11/index.html

Recommended Posts