1.简介
1.1Tomcat
一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Apache服务器。
1.2JVM
Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的。具有一次编译,到处运行的特性。
2.环境准备
[root@web04 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@web04 ~]# uname -r
3.10.0-327.el7.x86_64
[root@web04 ~]# getenforce
Disabled
[root@web04 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
[root@web04 ~]# ifconfig
eth0: flags=4163
inet 10.0.0.18 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c:29ff:fec7:37ae prefixlen 64 scopeid 0x20
ether 00:0c:29:c7:37:ae txqueuelen 1000 (Ethernet)
RX packets 1395 bytes 340494 (332.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 149 bytes 17284 (16.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163
inet 172.16.1.18 netmask 255.255.255.0 broadcast 172.16.1.255
inet6 fe80::20c:29ff:fec7:37b8 prefixlen 64 scopeid 0x20
ether 00:0c:29:c7:37:b8 txqueuelen 1000 (Ethernet)
RX packets 772 bytes 46320 (45.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10 bytes 744 (744.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 0 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3.JDK安装(所有涉及到JAVA应用都需要安装JDK)
3.1下载安装包到windows并上传
8版本的包地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
9版本的包地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html
3.2解压
[root@web04 tools]# tar xf jdk-8u60-linux-x64.tar.gz -C /application/
3.3创建软链接
[root@web04 application]# ln -s jdk1.8.0_60/ jdk
3.4修改环境变量
[root@web04 application]# sed -i.ori '$a export JAVA_HOME=/application/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
[root@web04 application]# source /etc/profile
3.5查看版本号
[root@web04 application]# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
4.安装Tomcat
4.1下载tomcat
官方地址:http://tomcat.apache.org/
4.2解压
[root@web04 tools]# tar xf apache-tomcat-8.0.27.tar.gz -C /application/
4.3创建软链接
[root@web04 application]# ln -s apache-tomcat-8.0.27 tomcat
4.4修改环境变量
[root@web04 application]# echo 'export TOMCAT_HOME=/application/tomcat'>>/etc/profile
[root@web04 application]# source /etc/profile
4.5授权用户
[root@web04 application]# chown -R root.root /application/jdk/ /application/tomcat/
4.6查看tomcat相关信息
[root@web04 ~]# /application/tomcat/bin/version.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.27
Server built: Sep 28 2015 08:17:25 UTC
Server number: 8.0.27.0
OS Name: Linux
OS Version: 3.10.0-327.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_60-b27
JVM Vendor: Oracle Corporation
4.7启动tomcat
[root@web04 ~]# /application/tomcat/bin/startup.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@web04 ~]# ss -lntup | grep java
tcp LISTEN 0 100 :::8009 :::* users:(("java",pid=2886,fd=51))
tcp LISTEN 0 100 :::8080 :::* users:(("java",pid=2886,fd=46))
5.解决tomcat在CentOS 7 启动慢
5.1原因
1)查看实时日志时,生成随机数的时候卡住,导致tomcat启动慢
2) 没有足够的熵产生随机数
[root@web04 ~]# cat /proc/sys/kernel/random/entropy_avail ---查看熵值
5.2解决
1)随便敲打命令、按键、移动鼠标,使其聚气
2)安装熵服务器,增大熵池(推荐)
[root@web04 ~]# yum install -y rng-tools
[root@web04 ~]# systemctl start rngd
6.访问web界面
7.利用tomcat搭建jpress
7.1安装MySQL数据库(tomcat服务器本地创建数据库)
[root@web04 ~]# yum install -y mariadb-server
7.2启动MySQL数据库
[root@web04 ~]# systemctl start mariadb.service
7.3创建库
MariaDB [(none)]> create database jpress DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)
7.4授权用户
MariaDB [(none)]> grant all on jpress.* to jpress@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
7.5上传jpress-web-newest.war上传webapps目录
tomcat会自动解压jpress-web-newest.war
7.6浏览器访问http://10.0.0.18:8080/jpress-web-newest,网页安装
注意:搭建jpress站点,必须先启动MySQL 数据库,再启动tomcat。(原因:自己测试,实时查看tomcat日志,/applicatiom/tomcat/logs//catalina.out )
8.tomcat多实例(一个站点,多个实例)
8.1创建多个实例
[root@web04 application]# cp -a apache-tomcat-8.0.27/ tomcat8_1
[root@web04 application]# cp -a apache-tomcat-8.0.27/ tomcat8_2
8.2修改多实例配置文件
[root@web04 application]# sed -i 's#8005#8011#;s#8080#8081#' tomcat8_1/conf/server.xml
[root@web04 application]# sed -i 's#8005#8011#;s#8080#8081#' tomcat8_2/conf/server.xml
8.3启动tomcat多实例
[root@web04 ~]# /application/tomcat8_1/bin/startup.sh
Using CATALINA_BASE: /application/tomcat8_1
Using CATALINA_HOME: /application/tomcat8_1
Using CATALINA_TMPDIR: /application/tomcat8_1/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat8_1/bin/bootstrap.jar:/application/tomcat8_1/bin/tomcat-juli.jar
Tomcat started.
[root@web04 ~]# /application/tomcat8_2/bin/startup.sh
Using CATALINA_BASE: /application/tomcat8_2
Using CATALINA_HOME: /application/tomcat8_2
Using CATALINA_TMPDIR: /application/tomcat8_2/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat8_2/bin/bootstrap.jar:/application/tomcat8_2/bin/tomcat-juli.jar
Tomcat started.
8.4tomcat反向代理集群(nginx反向代理服务器,这里使用的是CentOS 6.9系统)
[root@lb01 ~]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream web_pools {
server 10.0.0.18:8081;
server 10.0.0.18:8082;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.jsp index.html index.htm;
proxy_pass http://web_pools;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
8.5启动nginx反向代理服务器(不能使用平滑启动)
[root@lb01 ~]# nginx -s stop
[root@lb01 ~]# nginx
9.zabbix监控tomcat服务器
9.1tomcat服务器开启远程监控功能
[root@web04 ~]# vim /application/tomcat/bin/catalina.sh +97
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=10.0.0.17"
[root@web04 ~]# /application/tomcat/bin/shutdown.sh
[root@web04 ~]# /application/tomcat/bin/startup.sh
9.2zabbix服务端开启java监控功能
[root@m01 ~]# sed -i -e '220a JavaGateway=127.0.0.1' -e '236a StartJavaPollers=5' /etc/zabbix/zabbix_server.conf
[root@m01 ~]# systemctl restart zabbix-server.service
[root@m01 ~]# yum -y install zabbix-java-gateway
9.3zabbix服务端监控