Initial configuration required from installation of Tomcat on CentOS to startup
Publication date: December 8, 2020
INFOMARTION > Initial configuration required from installation of Tomcat on CentOS to startup
summary
This section describes the initial configuration steps required to install and start Tomcat on CentOS, assuming CentOS7 is used.
Please refer to the descriptions of Apache linkage and changing log output destinations.
The versions are as follows
CentOS Version | 7.6 (1810) |
---|---|
Apache Version | 2.4.6 |
Tomcat Version | 7.0.76 |
Java Version | OpenJDK 1.8.0_272 64bit |
Table of Contents
1. install
This section describes the configuration of Tomcat from installation to startup.
1-1. Installing Tomcat
Use yum command to install Tomcat.
[username@hostname ~]$ su -
[root@hostname ~]# yum -y install tomcat
2. basic setting
This section describes the setup of Tomcat from installation to startup. The yum mentioned earlier will create "/usr/share/tomcat". Here are the shortcut links to the configuration file folders, etc., and we will modify them.
2-1. Tomcat Path Settings
In other words, in Windows, it is the procedure for setting environment variables.
This is not necessary if tomcat.conf contains the setting "CATALINA_HOME="/usr/share/tomcat". Check "/usr/share/tomcat/conf/tomcat.conf".
If "CATALINA_HOME="/usr/share/tomcat" is not present, add it by following the steps below.
[root@hostname ~]# vi /etc/profile
Add the following at the end
CATALINA_HOME=/usr/share/tomcat
export CATALINA_HOME
This completes the specification of Tomcat environment variables.
Java settings for running Tomcat are also required, so if the Java path settings have not been made, please refer to the following for the settings.
Install Java on CentOS, set path
It can also be installed by executing "yum install java-1.8.0-openjdk".
2-2. Startup Confirmation
Check if Tomcat starts.
If you install Tomcat with yum, there is no html for the Tomcat top page, so install the html first.
[root@hostname ~]# yum -y install tomcat-webapps
Next, activate firewall, which opens port 8080 since Tomcat's default port number is 8080.
[root@hostname ~]# firewall-cmd --permanent --zone=public --add-port=8080/tcp
[root@hostname ~]# firewall-cmd --reload
[root@hostname ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client http https ssh
ports: 8080/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
When "ports: 8080/tcp" appears, it is OK. Start Tomcat.
[root@hostname ~]# tomcat start
Access the site from a browser. In my case, the IP address of the server is 192.168.50.10, so I access "http://192.168.50.10:8080/". It is OK if the following page is displayed.
After confirming that the startup was successful, Tomcat is stopped.
[root@hostname ~]# tomcat stop
2-3. Apache and Tomcat integration settings
Set this option if you want Apache and Tomcat to work together.Tomcat can run as a web server without Apache, but there are advantages to working with Apache for a variety of reasons, such as the convenience of Apache's module function and faster response for static content.
Please see below for Apache installation instructions.
Initial configuration required from installation of Apache on CentOS to startup
The following is the procedure for changing the configuration on the Apache side, which passes requests coming to Apache to Tomcat. This description assumes that there is an Apache configuration file under "/etc/httpd/".
[root@hostname ~]# vi /etc/httpd/conf/httpd.conf
Add the following statement to the end of "httpd.conf".
<Location / >
ProxyPass ajp://localhost:8009/
Order allow,deny
Allow from all
</Location>
As a prerequisite, the following modules must be enabled, if not already enabled. (uncomment out #).
・LoadModule proxy_module modules/mod_proxy.so
・LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
Start Apache (restart if already started) and Tomcat.
[root@hostname ~]# apachectl stop
[root@hostname ~]# apachectl start
[root@hostname ~]# tomcat start
Access the site from a browser. In my case, the IP address of the server is 192.168.50.10, so I access "http://192.168.50.10/".
It is OK if the following page is displayed.
After confirming that the startup was successful, Tomcat is stopped.
[root@hostname ~]# tomcat stop
The figure shows the following access.
Incidentally, the previous procedure was accessed as follows.
2-4. java heap, gc log settings
Java heap is Java memory and gc log is a log about Java memory. Java uses a set amount of memory set up as a heap for processing. Both are set as options to the Java startup command. We will set it in "tomcat.conf".
[root@hostname ~]# vi /usr/share/tomcat/conf/tomcat.conf
The following information is added at the end of this document.
CATALINA_OPTS="-server -XX:MaxMetaspaceSize=256m -XX:MetaspaceSize=256m -Xmx256m -Xms256m -Xloggc:/var/log/tomcat/gc.log -XX:+PrintGCDetails"
Start Tomcat and execute the ps command to display the configuration values (-server -XX:MaxMetaspaceSize=256m -XX:MetaspaceSize=256m -Xmx256m -Xms256m -Xloggc:/var/log/tomcat/gc.log -XX (+:+PrintGCDetails) is displayed, it is OK.
[root@hostname ~]# tomcat start
[root@hostname ~]# ps aux | grep java
/usr/lib/jvm/jre/bin/java -Djavax.sql.DataSource.Factory=org.apache.commons.dbcp.BasicDataSourceFactory -server -XX:MaxMetaspaceSize=256m -XX:MetaspaceSize=256m -Xmx256m -Xms256m -Xloggc:/var/log/tomcat/gc.log -XX:+PrintGCDetails -classpath /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat -Dcatalina.home=/usr/share/tomcat -Djava.endorsed.dirs= -Djava.io.tmpdir=/var/cache/tomcat/temp -Djava.util.logging.config.file=/usr/share/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager org.apache.catalina.startup.Bootstrap start
The above configuration will start Tomcat using 256M of memory. Above 256M, Tomcat will be configured to use no memory.
For gc logs, a log file is created under "/var/log/tomcat/" as "gc.log".
Creation of the directory "/var/log/tomcat/" is performed by the following procedure.
2-5. Change log settings
Create a log directory and change the log output path for Tomcat. Logging-related settings are found in "/usr/share/tomcat/conf/logging.properties" and "/usr/share/tomcat/conf/server.xml".
Below is the directory creation.
[root@hostname ~]# mkdir /var/log/tomcat
[root@hostname ~]# chmod 755 /var/log/tomcat
Create a directory and set the permissions to 755. (Tomcat can write logs, but other users can only see them.)
Then, modify "logging.properties".
[root@hostname ~]# vi /usr/share/tomcat/conf/logging.properties
Correct as follows
1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = /var/log/tomcat
1catalina.org.apache.juli.FileHandler.prefix = catalina.
2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = /var/log/tomcat
2localhost.org.apache.juli.FileHandler.prefix = localhost.
3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = /var/log/tomcat
3manager.org.apache.juli.FileHandler.prefix = manager.
4host-manager.org.apache.juli.FileHandler.level = FINE
4host-manager.org.apache.juli.FileHandler.directory = /var/log/tomcat
4host-manager.org.apache.juli.FileHandler.prefix = host-manager.
Then, modify "server.xml".
[root@hostname ~]# vi /usr/share/tomcat/conf/server.xml
Correct as follows
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/tomcat"
prefix="localhost_access" suffix=".txt"
pattern="combined" />
2-6. Automatic startup setting
Finally, although it is not required, it is hard to start Tomcat every time the server is rebooted, so we will configure Tomcat to start automatically when the server is started. Please note that this procedure is for CentOS7, so those who are using other than CentOS7 need to use the Service command.
[root@hostname ~]# systemctl enable tomcat
[root@hostname ~]# systemctl is-enabled tomcat
enabled
[root@hostname ~]# systemctl list-unit-files --type=service | grep tomcat
tomcat.service enabled
[root@hostname ~]# systemctl daemon-reload
2-7. Mail Sending Settings
This setting is unnecessary if you do not use Tomcat to send mail. If you are implementing mail sending processing in Java, please refer to the configuration.
First, add a jar. Place the following jar under the tomcat lib (/usr/share/tomcat/lib/lib/) and match the permissions with those of the other libs.
・javax.activation-1.2.0.jar
・javax.mail.jar
Modify "context.xml".
[root@hostname ~]# vi /usr/share/tomcat/conf/context.xml
Add the following to the end of context.xml
<Context>
<Resource name="mail/Session" auth="Container"
type="javax.mail.Session"
/>
</Context>
3. summary
We have described the initial configuration required when Tomcat is installed.
Tomcat basically works to some extent with default settings, but if you actually customize it, it will be useful later on to set log output destination settings and auto-start commands.
Thank you for taking the time to read this to the end.
â– INFORMATION
Please click here to go to the top page of INFORMATION.
â– PROFILE
Please click here to view the profile.
â– For inquiries, please contact