How to make Tomcat use APR connector

Install APR

APR introduction:

APR is short for Apache Portable Runtime, which is a highly portable library. APR has many uses, including access to advanced I/O functions (such as sendfile, epoll and openssl), operating system level functions (random number generation, system status, etc.) and native process processing (shared memory, NT pipes and Unix sockets), etc.

The connector based on Apr can operate system level functions, so its performance is higher than other connectors. Let Tomcat use APR connector is also one of the common tuning methods. This article will teach you how to use APR connector under Linux.

Official website address:

http://apr.apache.org/

Download address:

http://apr.apache.org/download.cgi

Enter the download page and copy the download link of the source package:

To download on Linux, use the wget command:

[root@txy-server ~]# cd /usr/local/src
[root@txy-server /usr/local/src]# wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz

Required dependencies for installation and compilation:

[root@txy-server ~]# yum install -y openssl-devel gcc expat-devel

Extract and download the good apr source package, and complete the compilation and installation by referring to the following command:

# decompression
[root@txy-server /usr/local/src]# tar -zxvf apr-1.7.0.tar.gz
[root@txy-server /usr/local/src]# cd apr-1.7.0/
# Configure compilation parameters and generate Makefile
[root@txy-server /usr/local/src/apr-1.7.0]# ./configure --prefix=/usr/local/apr  # Specify installation path
# Compile and install
[root@txy-server /usr/local/src/apr-1.7.0]# make && make install  

Install APR dependency module - APR util

Since APR relies on several modules, one of them is APR util. So we have to install the modules that APR depends on. Or the previous download page, copy the download link of the APR util source package:

To download on Linux, use the wget command:

[root@txy-server /usr/local/src/apr-1.7.0]# cd ../
[root@txy-server /usr/local/src]# wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz

Unzip and download the good APR util source package. Refer to the following command to complete the compilation and installation:

[root@txy-server /usr/local/src]# tar -zxvf apr-util-1.6.1.tar.gz
[root@txy-server /usr/local/src]# cd apr-util-1.6.1/
[root@txy-server /usr/local/src/apr-util-1.6.1]# ./configure -prefix=/usr/local/apr-util --with-apr=/usr/local/apr  # Specify the installation path of apr
[root@txy-server /usr/local/src/apr-util-1.6.1]# make && make install

Install APR dependency module - APR iconv

Then there is the APR iconv module, which is also the previous download page. Copy the download link of the APR iconv source package:

To download on Linux, use the wget command:

[root@txy-server /usr/local/src/apr-util-1.6.1]# cd ../
[root@txy-server /usr/local/src]# wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-iconv-1.2.2.tar.gz

Unzip and download the good APR iconv source package. Refer to the following command to complete the compilation and installation:

[root@txy-server /usr/local/src]# tar -zxvf apr-iconv-1.2.2.tar.gz
[root@txy-server /usr/local/src]# cd apr-iconv-1.2.2/
[root@txy-server /usr/local/src/apr-iconv-1.2.2]# ./configure -prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
[root@txy-server /usr/local/src/apr-iconv-1.2.2]# make && make install

Install APR dependency module - OpenSSL

APR connector is based on OpenSSL library to provide support for https (high performance), so you need to install this library next. The download address of OpenSSL is as follows:

https://www.openssl.org/source/

Copy the download link to the download page:

To download on Linux, use the wget command:

[root@txy-server /usr/local/src/apr-util-1.6.1]# cd ../
[root@txy-server /usr/local/src]# wget https://www.openssl.org/source/openssl-1.0.2t.tar.gz

Unzip and download the OpenSSL source package, and complete the compilation and installation by referring to the following command:

[root@txy-server /usr/local/src]# tar -zxvf openssl-1.0.2t.tar.gz
[root@txy-server /usr/local/src]# cd openssl-1.0.2t/
[root@txy-server /usr/local/src/openssl-1.0.2t]# ./config --prefix=/usr/local/openssl

Unlike compiling other libraries, you need to modify the Makefile file generated in the previous step and add a compilation parameter:

[root@txy-server /usr/local/src/openssl-1.0.2t]# vim Makefile
# Add the - fPIC parameter at the end of the CFLAG attribute
CFLAG= ... -fPIC

After the modification, perform the compilation and installation:

[root@txy-server /usr/local/src/openssl-1.0.2t]# make && make install

Install Tomcat

Download address on official website:

https://tomcat.apache.org/download-90.cgi

Copy download link:

To download on Linux, use the wget command:

[root@txy-server ~]# cd /usr/local/src
[root@txy-server /usr/local/src]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz

Unzip and download the compressed package, and rename the directory after unzip:

[root@txy-server /usr/local/src]# tar -zxvf apache-tomcat-9.0.27.tar.gz -C /usr/local
[root@txy-server /usr/local/src]# mv ../apache-tomcat-9.0.27/ ../tomcat9

Install Tomcat native

Finally, install the Tomcat native component, which allows Tomcat to access the APR connector. Tomcat has brought its own Tomcat native, no need to download it. We only need one installation, the steps are as follows:

# Unzip Tomcat native
[root@txy-server ~]# tar -zxvf /usr/local/tomcat9/bin/tomcat-native.tar.gz -C /usr/local/src
[root@txy-server ~]# cd /usr/local/src/tomcat-native-1.2.23-src/native/
# Configure compilation parameters, specify installation location of APR and OpenSSL libraries, and generate Makefile
[root@txy-server /usr/local/src/tomcat-native-1.2.23-src/native]# ./configure --with-apr=/usr/local/apr --with-ssl=/usr/local/openssl
[root@txy-server /usr/local/src/tomcat-native-1.2.23-src/native]# make && make install  # Compilation and installation

The official document address of Tomcat native is as follows:

http://tomcat.apache.org/native-doc/

Configure Tomcat to use APR connector

Edit Tomcat's server.xml file, change the value of the protocol attribute of the Connector to org.apache.coyote.http11.Http11AprProtocol, that is, enable the APR Connector. Here is an example:

[root@txy-server ~]# vim /usr/local/tomcat9/conf/server.xml
    <Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol"
               connectionTimeout="20000"
               redirectPort="8443" />

Then specify the library path of APR in the catalina.sh script file of Tomcat. And since the native memory used by the JVM will increase after the APR connector is turned on, the Metaspace space needs to be increased appropriately:

[root@txy-server ~]# vim /usr/local/tomcat9/bin/catalina.sh
# Add a line of JVM parameter configuration
JAVA_OPTS="$JAVA_OPTS -XX:MetaspaceSize=128m -Djava.library.path=/usr/local/apr/lib"

Parameter Description:

  • XX:MetaspaceSize=128m: set the Metaspace space to 128M
  • -Djava.library.path=/usr/local/apr/lib ": specify the library path of apr

After configuration, start the Tomcat server with the following command:

[root@txy-server ~]# /usr/local/tomcat9/bin/startup.sh

After starting tomcat, use the following command to view the startup log of Tomcat:

[root@txy-server ~]# tail -n10 /usr/local/tomcat9/logs/catalina.out

The message "APR" is output as follows, which means that the APR connector is enabled successfully:

Tags: Java Tomcat OpenSSL Apache Linux

Posted on Sun, 03 Nov 2019 10:58:33 -0800 by tbales