Developers learn Linux (3): Install JDK8 and Tomcat8 in CentOS7

Source: Internet
Author: User

Digression: I didn't want to get a name for this series until I started writing the third part of the series today, but this series is not intended for Linux OPS, but is primarily intended for developers, including but not limited to:
Developers who want to know about Linux;
Developers who need to deploy some components on Linux, such as Memcached/nginx/redis, etc.;
You need to use some systems that might have a better effect on Linux, such as Jetkins, SVN, Javamelody, and so on.
Want to know some of the current hot technology, want to build an environment in the virtual machine, such as. Net Core/hadoop.
So in this series of articles will not talk about the history of Linux and the various branches, and will not talk about the advantages and disadvantages of Linux compared to Windows, not to mention how the Linux shell programming, involving this knowledge will be more quickly, This series of articles is about how to quickly build a running and observing environment in a virtual machine.

Two time before the book, you have already talked about how to install and configure the free open source virtual machine software VirtualBox and how to install and configure CentOS7 in VirtualBox. In this article we will show you how to install JDK and Tomcat in CentOS7, for future needs, this article will describe how to install Oracle JDK8 and TOMCAT8.

Software Preparation
This article uses tar.gz installation, of course, can also be installed in RPM or Yum mode installation, as for the latter two installation methods interested in the relevant articles.
JDK File: jdk-8u131-linux-x64.tar.gz
Official: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Tomcat file: apache-tomcat-8.5.15.tar.gz
Official: http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.15/bin/apache-tomcat-8.5.15.tar.gz
Or: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Note: Since the first two articles are described in CentOS7 x64, the compressed packages downloaded here are also x64.

Installing Oracle JDK
Uninstalling OpenJDK
When installing CentOS7 in the previous way, By default the OpenJDK1.8 is installed, and OPENJDK is actually another implementation on the Java specification, just as mono is also implemented in the Microsoft. NET specification, and the compiled files on one platform can be run directly on another platform, but I've been working on Windows a few years ago. Soft Visual Studio writes an ASP. NET program and then puts it on a Linux system and runs with APACHE+MONO+XSP.
Since there's more to Java behind, it's uninstalling OPENJDK and installing the Oracle JDK.
Check whether the current system installs the JDK, run the command:
Rpm-qa | grep JDK
The results were as follows:
Java-1.8.0-openjdk-headless-1.8.0.131-2.b11.el7_3.x86_64
Java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64
Copy-jdk-configs-1.2-1.el7.noarch
To uninstall OPENJDK, run the command:

Yum Remove Java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64yum Remove Java-1.8.0-openjdk-headless-1.8.0.131-2.b11.el7_3.x86_64


If you run Rpm-qa again at this point | The grep JDK will see that java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64 is uninstalled.

Installing Oracle JDK
Download the jdk-8u131-linux-x64.tar.gz to the host by following the address provided in this article, and then use the SSH client software sshsecureshellclient to upload to the/root directory.
Unzip the file:

Tar–zxf/root/jdk-8u131-linux-x64.tar.gz

After decompression, a jdk1.8.0_131 folder will be added to the/root directory, and then the following command will be moved to the/usr/local directory:

mv./jdk1.8.0_131//usr/local

Note:/usr/local/jdk1.8.0_131 This directory is used when setting the JDK environment variable.

The following need to configure the environment variables, tell the system Java related information, such as installation path, class library path and so on. Here are two ways to configure: one for the specified user configuration, one for all users, if only for a user configuration then only in the. bashrc file in that user directory, if you need to set up for all users, then you need to set in/etc/profile, Here is an example of a global setting that opens the/etc/profile file at the end of the following add:

Export Java_home=/usr/local/jdk1.8.0_131export jre_home=/usr/local/jdk1.8.0_131 export classpath=.: $JAVA _home/jre/ Lib/rt.jar: $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar export path= $PATH: $JAVA _home/bin


Then make the configuration take effect, execute the following command:

Source/etc/profile


You can now check that the Java environment variable is configured correctly, and get the following output on my machine via the java–version command:
[Email protected] ~]# java-version
Java Version "1.8.0_131"
Java (TM) SE Runtime Environment (build 1.8.0_131-B11)
Java HotSpot (TM) 64-bit Server VM (build 25.131-b11, Mixed mode)

At this point, the Oracle JDK is installed correctly.

Installing TOMCAT8
Download the apache-tomcat-8.5.15.tar.gz to your local host (note: You can also download it directly to the virtual machine on a virtual machine that has access to the Internet), and then use the SSH client software sshsecureshellclient upload to /root directory.

Unzip the file:

Tar–zxf/root/jdk-8u131-linux-x64.tar.gz

After decompression, a apache-tomcat-8.5.15 folder will be added to the/root directory, and then the following command will be moved to the/usr/local directory:

mv./apache-tomcat-8.5.15//usr/local

Note:/usr/local/apache-tomcat-8.5.15 This directory is used when setting the Tomcat environment variable. Of course, you can also not set Tomcat-related environment variables, and these environment variables are automatically detected when you work with Tomcat using related scripts.
At this point, Tomcat is ready to install.
Start Tomcat with the following command:

/usr/local/apache-tomcat-8.5.15/bin/startup.sh

If you want to turn Tomcat off, it's easy to do the following:

/usr/local/apache-tomcat-8.5.15/bin/stop.sh

Note: Using/usr/local/apache-tomcat-8.5.15/bin/catalina.sh plus parameters can also start or stop Tomcat, such as/usr/local/apache-tomcat-8.5.15/bin/ catalina.sh start to launch tomcat,/usr/local/apache-tomcat-8.5.15/bin/catalina.sh stop to stop Tomcat, in addition to using debug/run/ Configtest/version parameter, but no restart parameter to restart Tomcat.

The virtual machine can be opened http://localhost:8080 watch that property of the Tomcat start page, in the host to the virtual machine's IP address to access (if the virtual machine has at least one network card is the bridge mode), on my machine on the virtual machine has a network card is bridged, The IP address is: 192.168.60.198, with the host in a network segment, so it can be viewed as http://192.168.60.198:8080, as shown in:
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/96/F0/wKioL1km49mhRuIRAAHgy3eUQjw860.png-wh_500x0-wm_ 3-wmp_4-s_2994066692.png "title=" Jdk8_tomcat8_01.png "alt=" Wkiol1km49mhruiraahgy3euqjw860.png-wh_50 "/> Note: If you can connect to the virtual machine via SSH but cannot open http://192.168.60.198:8080, then it is most likely a firewall problem, execute the following command to turn off the firewall:

Systemctl Disable FIREWALLD

can be accessed after a reboot.

Adding services to Tomcat in CentOS7 (using Systemctl to manipulate Tomcat)
To enable Tomcat to boot with CentOS7, it is possible to control tomcat through Systemctl Start/stop/restart. There are quite a few examples of CentOS7 in previous versions of the Internet, all through the/ETC/RC.D/INIT.D or/etc/ The INIT.D directory adds a script to the CentOS7 that provides another way to use SYSTEMCTL instead of the service, either in/usr/lib/systemd/system or/etc/systemd/system/ Directory to add control commands.
Systemctl usage

Commands for system services in CENTOS7 are: Systemctl list-units (equivalent to SYSTEMCTL): List active Services Systemctl--failed: Lists services that failed to run Systemctl Daemon-reload: Scan for new or changed services Systemctl status A.serivice: View a boot information for this service (note that the name of a is changed to a specific service name at run time, such as the sshd.service of the SSH service), this command is useful for viewing the cause when a service failure is started Systemctl enable A.service: Set a service to start with the system systemctl disable A.service: Disable A This service starts with the system Systemctl start A.service: Start a This servicesystemctl stop a.service: Stop a This servicesystemctl Restart A.service: Restart a This service

Note: In practice, a.service in the above command can be simplified to a, such as Systemctl enable A.service, which can be simplified to systemctl enable.

Adding boot parameters to Tomcat
The previous section describes how to install Oracle JDK8 and Tomcat8.5, and shows the startup.sh/stop.sh or catalina.sh start/catalina.sh in the bin directory using the Tomcat installation location Stop to start or close tomcat. But in real-world development we still want Tomcat to start automatically when the system starts, rather than manually booting from the command line, and here's how to add Tomcat to the CentOS7 startup service.
First we remember the following three points:
1.Oracle JDK8 installation directory:/usr/local/jdk1.8.0_131
2.TOMCAT8 installation directory:/usr/local/apache-tomcat-8.5.15
3. and Java-related environment variables are configured in/etc/profile:

Export Java_home=/usr/local/jdk1.8.0_131export jre_home=/usr/local/jdk1.8.0_131 export classpath=.: $JAVA _home/jre/ Lib/rt.jar: $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar export path= $PATH: $JAVA _home/bin


First you need to add the setenv.sh file under the bin directory of the Tomcat installation directory, you can create the file by vim/usr/local/apache-tomcat-8.5.15/bin/setenv.sh, and then paste the following into the store:

 #set  java_home and jre_home and catalina_home  and catalina_baseexport java_home=/usr/local/jdk1.8.0_131export jre_home=/usr/local/ jdk1.8.0_131/jreexport catalina_home=/usr/local/apache-tomcat-8.5.15export catalina_base=/usr/local/ apache-tomcat-8.5.15#set java_home and jre_home and catalina_home and  Catalina_base#add tomcat pidcatalina_pid= "$CATALINA _base/tomcat.pid" #add  java optsjava_ Opts= "-SERVER -XX:PERMSIZE=256M -XX:MAXPERMSIZE=1024M -XMS512M -XMX1024M -XX: maxnewsize=256m "

The

is set java_home/jre_home/catalina_home/catalina_base/catalina_pid five environment variables.
Note:
1. The setenv.sh file that you just created is automatically invoked when you start Tomcat by executing catalina.sh.
2. Before studying on the Internet to see a lot of articles mentioned only set catalina_home/catalina_base/catalina_pid three environment variables, I have not succeeded, through Systemctl The status command looks at the lack of java_home/jre_home configuration information and can be started successfully after configuring Java_home/jre_home. The front also said, I also configured the/etc/profile in the Java_home/jre_home.
3. Because the Tomcat.pid file is configured, the Tomcat.pid file is automatically generated in the Tomcat root directory when Tomcat is started, and tomcat.pid files are automatically deleted after you stop Tomcat. You can use Tomcat.pid to prevent multiple instances from executing catalina.sh or startup.sh when you start Tomcat.

create Tomcat.service file
Then add Tomcat.service file, which can be/usr/lib/systemd/ System/tomcat.service to create the file and paste it in, then save:

[unit]description=apache tomcat 8.5.15after=syslog.target  network.target remote-fs.target nss-lookup.target[service]type=forkingpidfile=/usr/local/ Apache-tomcat-8.5.15/tomcat.pidexecstart=/usr/local/apache-tomcat-8.5.15/bin/startup.shexecreload=/bin/kill  -s HUP  $MAINPIDExecStop =/bin/kill -s quit  $MAINPIDPrivateTmp =true[install] Wantedby=multi-user.target 


You can then execute the following command to verify that the configuration is correct:
Systemctl enable Tomcat.service: Set up tomcat This service starts with the system
Systemctl Disable Tomcat.service: Disable Tomcat This service starts with the system
Systemctl start Tomcat.service: Start Tomcat this service
Systemctl Stop Tomcat.service: Stop tomcat This service
Systemctl Restart Tomcat.service: Restart Tomcat for this service

Tomcat.service File Structure Description
The Tomcat.service file described above contains [unit], [service], [install] three parts, where:
[Unit] Configures the description and boot order of the Tomcat service, which in this example specifies that after network startup, the execution
[Service] Configure the Tomcat service PID, service start, stop, restart command
[Install] Configures the use of the Tomcat runtime

This article is from the "Zhou Gong (Zhou Jinchao) column" blog, please be sure to keep this source http://zhoufoxcn.blog.51cto.com/792419/1929559

Developers learn Linux (3): Install JDK8 and Tomcat8 in CentOS7

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.