Until you install Apache and Tomcat on Linux (CentOS) and deploy Java apps

Introduction

I wanted to acquire knowledge about Linux, so I tried to rent a Linux server, install JDK, Apache, Tomcat, and deploy a Java application (Hello World level) made on my local PC by myself. In this article, I wrote down what I did at that time and how I solved the problem. The cloud is overwhelmingly mainstream these days, but I feel that the cloud is too black box. I haven't got the basics for Linux (infrastructure) anyway, so I'll start by renting a plain Linux server instead of the cloud and deploying applications on it. In the future, I will extend the application and try to operate it myself. I want to learn the basics of Linux through such attempts. With that in mind, I tried something like this. This time, we have to deploy the application, and expansion and operation are the future. I deployed the application at the Hello World level, so I won't write about Java. I rented a Linux server, installed Apache and Tomcat, set it up, and hit the URL, and Hello World came out! It's about that.

What I actually did

Rental server for individuals

Rent a rental server for individuals that can use Java. This time I decided to use Sakura Internet. There are many, so look for them yourself and choose the right one.

-Comparison of rental servers for individuals -Analyze and compare rental servers that can use Java

OS check

Check the version of CentOS

I'm going to install Java, Apache, and Tomcat on the rented server, but before that, check the basic information of the OS. First, check the version of CentOS with the following command. cat /etc/redhat-release

Execution result


CentOS Linux release 7.7.1908 (Core)

You can see that it is version 7 of CentOS.

Check the architecture (whether the OS is 32bit or 64bit)

Check whether the OS is 32bit or 64bit with the following command.

arch

Execution result(64bit)


X86_64

This indicates that it is 64bit. In the case of 32bit, it will be displayed as follows.

Execution result(32bit)


i686

You can also check with the following command. uname -a

For CentOS 7, it will be displayed as follows.

Display example of CentOS7(64bit)


Linux ik1-344-32350.vs.sakura.ne.jp 3.10.0-957.10.1.el7.x86_64 #1 SMP Mon Mar 18 15:06:45 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

For reference, in the case of CentOS 6, it is displayed as follows.

Display example of CentOS6(64bit)


Linux localhost.localdomain 2.6.32-279.2.1.el6.x86_64 #1 SMP Fri Jul 20 01:55:29 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

Reference: CentOS version check command and architecture check command

Package update

Update the package with the following command. yum update

Normally, the package is specified and updated so as not to affect the currently running program. However, this time I just rented it and no application is running, so I updated all the packages with yum update. The command to specify the package is as follows. Normally, use this to avoid affecting running programs. yum update [package]

Reference: [yum update] Safely update yum packages

Java 8 (OpenJDK) installation

Install java-1.8.0-openjdk if you only need to install the runtime, and java-1.8.0-openjdk-devel if you also want to install the OpenJDK development environment.

Java 8 (JDK) runtime installation

Run the following command to install the Java 8 (JDK) runtime. yum install java-1.8.0-openjdk

Java 8 (JDK) Development environment installation

If you need a development environment, run the following command to install the java 8 (JDK) development environment. I developed it on my PC and deployed it to the server so I didn't install the development environment. yum install java-1.8.0-openjdk-devel

Confirmation of installation completion

Just in case, let's check if the installation is completed. Type the following command, and if the installed Java version is displayed, the confirmation is complete.

Execution command


java -version

Execution result


openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)

Reference: Procedure to install Java 8 (OpenJDK) on CentOS 7 with yum

Apache installation

Install Apache and register for service

Install Apache with the following command.

Execution command


yum -y install httpd

Register the service and start it.

Execution command


systemctl enable httpd.service
systemctl start httpd.service

Confirm that the service is started with the following command.

Execution command


systemctl status httpd.service

If ʻactive (running) is displayed, it is OK. Finally, make sure you see the test page (http: // `).

Reference: Summary when building Apache and Tomcat environment with CentOS 7 of Sakura Cloud

Tomcat installation

Installing Apache Tomcat 9

First, add tomcat as a dedicated user to run Tomcat on Linux.

Execution command


useradd -s /sbin/nologin tomcat

Next, use the curl command to download the Apache Tomcat 9 main unit in tar.gz format from the Apache Tomcat 9 Download Page. As of May 2020, version 9.0.34 is the latest version, so specify the following URL. Please check the URL of the latest version before executing.

Execution command


cd ~
curl -O http://ftp.riken.jp/net/apache/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz

Unzip and place the downloaded tar.gz file. Extract it with the tar command and place it in / opt as follows: Also, let the owner of the unzipped Apache Tomcat be owned by the tomcat user created earlier.

Execution command


tar -xzvf ~/apache-tomcat-9.0.34.tar.gz
mv ~/apache-tomcat-9.0.34 /opt
chown -R tomcat:tomcat /opt/apache-tomcat-9.0.34

Service creation and registration

Starting with CentOS 7, systemd manages the service. Here, Apache Tomcat 9 is registered as a service. First, create a new /etc/systemd/system/tomcat.service, write it as follows, and save it. This is the service definition file.

tomcat.service


[Unit]
Description=Apache Tomcat 9
After=network.target

[Service]
User=tomcat
Group=tomcat
Type=oneshot
PIDFile=/opt/apache-tomcat-9.0.34/tomcat.pid
RemainAfterExit=yes

ExecStart=/opt/apache-tomcat-9.0.34/bin/startup.sh
ExecStop=/opt/apache-tomcat-9.0.34/bin/shutdown.sh
ExecReStart=/opt/apache-tomcat-9.0.34/bin/shutdown.sh;/opt/apache-tomcat-9.0.34/bin/startup.sh

[Install]
WantedBy=multi-user.target

Change the permissions of the definition file you created to 755 with the following command:

Execution command


chmod 755 /etc/systemd/system/tomcat.service

After creating the definition file, enable the service with the following command.

Execution command


systemctl enable tomcat

If the following is displayed, it is OK.

Execution result


Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /etc/systemd/system/tomcat.service.

Finally, go to http: // <IP address>: 8080 and verify that you see the Tomcat test page.

[Sample display screen] image.png

If such a screen is displayed, Tomcat installation and service registration are complete.

Reference: Procedure to install Apache Tomcat 9 on CentOS 7

Cooperation between Apache and Tomcat

Use AJP (Apache JServ Protocol) to link Apache and Tomcat. To use AJP, use a module called mod_proxy_ajp.

Reference: How to link Apache httpd and Tomcat

apache settings

Load proxy_module and proxy_ajp_module

Httpd.conf was in this state when I installed apache.

httpd.conf (partial excerpt)



~ Omitted ~

#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
Include conf.modules.d/*.conf

~ Omitted ~

If you go to "/etc/httpd/conf.modules.d" described here, you can see the following 7 conf files.

01-cgi.conf
00-systemd.conf
00-proxy.conf
00-mpm.conf
00-lua.conf
00-dav.conf
00-base.conf

If you open "00-proxy.conf" in this, you will find the following settings.

00-proxy.conf (partial excerpt)


LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

Mod_proxy_ajp is required to use AJP, and mod_proxy_ajp depends on the mod_proxy module. Therefore, these two modules are required. If there is the above setting in 00-proxy.conf, the required module can be loaded properly. By the way, these modules "mod_proxy.so" and "mod_proxy_ajp.so" can be found by going to the /usr/lib64/httpd/modules directory. (There is a shortcut to / usr / lib64 / httpd / modules under / etc / httpd)

ProxyPass settings

The last line of httpd.conf has the following settings, so if you create a conf file in the /etc/httpd/conf.d directory, the contents will be read.

httpd.conf



~ Omitted ~

# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

Create a proxy-ajp.conf file in the /etc/httpd/conf.d directory with the following contents.

cat /etc/httpd/conf.d/proxy-ajp.conf
<Location /docs/>
  ProxyPass ajp://127.0.0.1:8009/docs/
</Location>

The port is set to 8009 because Tomcat accepts AJP 1.3 communication on port 8009. The Tomcat settings will come out later. Apache setup is now complete.

Tomcat settings

Modify server.xml

Modify /opt/apache-tomcat-9.0.34/conf/server.xml. Before the correction, it is as follows.

server.xml (before modification)


    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

~ Omitted ~

    <Connector protocol="AJP/1.3"
               address="::1"
               port="8009"
               redirectPort="8443" />

If all access from the Internet is via apache, the standby on port 8080 is unnecessary, so comment it out. After the correction, it will be as follows.

server.xml (after modification)


    <!--
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->

~ Omitted ~

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector protocol="AJP/1.3"
               address="::1"
               port="8009"
               redirectPort="8443" />

This completes the settings for linking apache and Tomcat.

Addictive 1

After completing the above work, I entered http: // <IP address> / docs into the browser, but I got a 503 error and could not access it. When I checked the apache log (error_log), the following log was displayed.

Apache log


[Sun Apr 26 14:33:39.753098 2020] [proxy:error] [pid 30144](111)Connection refused: AH00957: AJP: attempt to connect to 127.0.0.1:8009 (127.0.0.1) failed
[Sun Apr 26 14:33:39.753136 2020] [proxy:error] [pid 30144] AH00959: ap_proxy_connect_backend disabling worker for (127.0.0.1) for 60s
[Sun Apr 26 14:33:39.753141 2020] [proxy_ajp:error] [pid 30144] [client 220.100.106.105:51467] AH00896: failed to make connection to backend: 127.0.0.1

At first, I googled variously based on the wording of this log, but I had no idea how to solve it. Next, when I checked the tomcat log (catalina.out), I was able to confirm that the following log was output every time I started tomcat.

Log when starting Tomcat 1


18-Apr-2020 16:36:49.322 SEVERE [main] org.apache.catalina.util.LifecycleBase.handleSubClassException Failed to initialize component [Connector[AJP/1.3-8009]]
	org.apache.catalina.LifecycleException: Protocol handler initialization failed
		at org.apache.catalina.connector.Connector.initInternal(Connector.java:1041)
		at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:136)
		at org.apache.catalina.core.StandardService.initInternal(StandardService.java:533)
		at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:136)
		at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:1057)
		at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:136)
		at org.apache.catalina.startup.Catalina.load(Catalina.java:584)
		at org.apache.catalina.startup.Catalina.load(Catalina.java:607)
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:498)
		at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:303)
		at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:473)
	Caused by: java.net.SocketException: Protocol family unavailable
		at sun.nio.ch.Net.bind0(Native Method)
		at sun.nio.ch.Net.bind(Net.java:433)
		at sun.nio.ch.Net.bind(Net.java:425)
		at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:220)
		at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:85)
		at org.apache.tomcat.util.net.NioEndpoint.initServerSocket(NioEndpoint.java:228)
		at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:211)
		at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1141)
		at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:1154)
		at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:581)
		at org.apache.catalina.connector.Connector.initInternal(Connector.java:1038)
		... 13 more

If you google with "Protocol family unavailable" or "Connector [AJP / 1.3-8009]", go to this site I arrived. Apparently, it happens when the IPv6 address ":: 1" is specified on a server that does not support IPv6. I solved it by rewriting ":: 1" to "0.0.0.0".

xml:server.xml("0.0.0.0"Corrected to)


    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector protocol="AJP/1.3"
               address="0.0.0.0"
               port="8009"
               redirectPort="8443" />

Addictive 2

When I restarted Tomcat after dealing with the above "I got stuck 1", the following log was output next.

Log when starting Tomcat 2


	org.apache.catalina.LifecycleException: Protocol handler start failed
		at org.apache.catalina.connector.Connector.startInternal(Connector.java:1066)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
		at org.apache.catalina.core.StandardService.startInternal(StandardService.java:438)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
		at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
		at org.apache.catalina.startup.Catalina.start(Catalina.java:633)
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:498)
		at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:343)
		at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:474)
	Caused by: java.lang.IllegalArgumentException: The AJP Connector is configured with secretRequired="true" but the secret attribute is either null or "". This combination is not valid.
		at org.apache.coyote.ajp.AbstractAjpProtocol.start(AbstractAjpProtocol.java:264)
		at org.apache.catalina.connector.Connector.startInternal(Connector.java:1063)
		... 12 more

When I looked it up, there was a Connector attribute in server.xml called secretRequired, which defaults to true. If true, it seems necessary to specify a secret word attribute. (Official site) In this site, "secretRequired =" false "" was explicitly specified and solved, so if you try to imitate this, it will be solved. Did.

server.xml (added secretRequired)


    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector protocol="AJP/1.3"
               address="0.0.0.0"
               port="8009"
               redirectPort="8443"
               secretRequired="false" />

This completes the Tomcat settings.

deploy war

Finally, deploy the war file created on your local PC to the rental server. If you store the war file in /opt/apache-tomcat-9.0.34/webapps, Tomcat will deploy it automatically. When the deployment is complete, access it and confirm that Hello World appears, and you're done.

that's all.

reference

-CentOS version check command and architecture check command -[Yum update] Safely update yum packages -Procedure to install Java 8 (OpenJDK) on CentOS 7 with yum -Procedure to install Apache Tomcat 9 on CentOS 7

Recommended Posts

Until you install Apache and Tomcat on Linux (CentOS) and deploy Java apps
Until you install Arch Linux on VMware
Install tomcat 5.5 on Amazon Linux.
To deploy Java application on VPS (Apache / Tomcat installation / linkage)
Install oracle java8 on amazon linux2
Install java (Oracle JDK14) on CentOS7
Until you install and run matplotlib
Install wsl2 and master linux on windows
Install and launch k3s on Manjaro Linux
Install and Configure TigerVNC server on Linux
Install Mecab on Linux (CentOS) with brew
How to install Apache (httpd) on CentOS7
How to install Apache (httpd) on CentOS8
Install pyenv and rbenv on CentOS system-wide
Until you install Caffe and run the sample
Until you create an Ubuntu boot USB on your Macbook and install Ubuntu on your Thinkpad
Until you install Python with pythonbrew and run Flask on a WSGI server
Until you install Anaconda for data analysis on your Mac and launch the IDE
Install Apache Tomcat 9 on Ubuntu 19.10 Eoan Ermine Hello World
Procedure for manually installing Java (jdk1.8) on Linux (CentOS7)
Install Apache 2.4 on Ubuntu 19.10 Eoan Ermine and run CGI
How to install Git GUI and Gitk on CentOS
Build Apache HTTP Server and Wildfly on Oracle Linux 8
Until you install Gauge and run the official sample
Compile and install MySQL-python for python2.7 on amazon linux
Install Docker on Arch Linux and run it remotely
Install Faiss on CentOS 7
Install numba on CentOS 7.2
Install Python3.4 on CentOS 6.6
Integrate Apache and Tomcat
Until you install MySQL-python
Install mecab-python on CentOS
Install Python 2.7.3 on CentOS 5.4
Install awscli on centos7
Install Chainer on CentOS 6.7
[Personal memo] Install the latest Java on Amazon Linux that already contains Java and switch the version
Install LAMP on Amazon Linux 2 and build a WordPress environment.
Until you create Python Virtualenv on Windows and launch Jupyter
How to integrate Apache httpd 2.4 and Tomcat 9 on Cent OS 8
Install Linux (CentOS) on your PC using a USB stick
Install Linux on your Chromebox
Until docker-compose up on CentOS7
Install tomcat 9 on Cent OS 8
Install ImageMagick-6.2.x series on CentOS7.7
Install Python 3.8 on CentOS 7 (SCL)
Apache installation fails on CentOS 8.2
Install the JDK on Linux
Recording and playback on Linux
Install Python 3.7 and Django 3.0 (CentOS)
Install Chrome on CentOS 7 series
Install Python 3.8 on CentOS 8 (AppStream)
Install Homebrew on Amazon Linux 2
Install strongSwan 5.9.1 on Amazon Linux 2
[MariaDB] Install MariaDB on Linux and create a DB and an operating user.
Procedure until you can create a general user and execute the sudo command on CentOs (memorial note)