一、tomcat介绍
Tomcat服务器是一个免费的开放源代码的Web应用服务器,即能处理静态页面,又能处理动态的java页面
二、JDK安装
jdk是java开发工具包,也可以叫java虚拟机jvm,jvm会将一份代码自动转换成不同平台的代码,类似php功能
1、系统环境
[root@tomcat ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@tomcat ~]# uname -r 3.10.0-327.el7.x86_64 [root@tomcat ~]# getenforce Disabled [root@tomcat ~]# 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@tomcat ~]# hostname -I 172.19.5.10 172.16.1.10
2、安装JDK
cd /application/tools/ #上传jdk-8u60-linux-x64.tar.gz tar xf jdk-8u60-linux-x64.tar.gz -C /application/ ln -s /application/jdk1.8.0_60 /application/jdk 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 source /etc/profile
检查JDK版本信息
[root@tomcat ~]# 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)
三、部署tomcat
1、安装tomcat
cd /application/tools/ #上传apache-tomcat-8.0.27.tar.gz tar xf apache-tomcat-8.0.27.tar.gz -C /application/ ln -s /application/apache-tomcat-8.0.27 /application/tomcat echo 'export TOMCAT_HOME=/application/tomcat'>>/etc/profile source /etc/profile chown -R root.root /application/jdk/ /application/tomcat/
检查tomcat版信息
[root@tomcat ~]# /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
2、tomcat目录介绍
[root@tomcat ~]# tree /application/tomcat -L 1
/application/tomcat
├── bin #启动,关闭命令
├── conf #配置文件
├── lib
├── LICENSE
├── logs #日志文件
├── NOTICE
├── RELEASE-NOTES
├── RUNNING.txt
├── temp
├── webapps #站点目录
└── work
3、tomcat相关命令
/application/tomcat/bin/startup.sh #启动程序
/application/tomcat/bin/shutdown.sh #关闭程序
netstat -tunlp|grep java #查看tomcat端口信息
http://172.19.5.10:8080/
/logs/catalina.out #tomcat实时日志,此文件会越来越大,记得清空
4、配置tomcat管理
[root@tomcat ~]# tail -4 /application/tomcat/conf/tomcat-users.xml"manager-gui"/> "admin-gui"/> "tomcat" password="tomcat" roles="manager-gui,admin-gui"/> /application/tomcat/bin/shutdown.sh /application/tomcat/bin/startup.sh
http://172.19.5.10:8080/manager/status
注:适合测试环境,线上最好不用
5、自定义站点目录
vim /application/tomcat/conf/server.xml #124行后加入以下内容"" docBase="/application/tomcat/webapps/memtest" debug="0" reloadable="false" crossContext="true"/> "37team" docBase="/application/tomcat/webapps/memtest" debug="0" reloadable="false" crossContext="true"/>
6、tomcat多实例
cd /application/tools/ tar xf apache-tomcat-8.0.27.tar.gz cp -a apache-tomcat-8.0.27 tomcat8_1 cp -a apache-tomcat-8.0.27 tomcat8_2 sed -i 's#8005#8011#;s#8080#8081#' tomcat8_1/conf/server.xml sed -i 's#8005#8012#;s#8080#8082#' tomcat8_2/conf/server.xml cp -a tomcat8_1 tomcat8_2 /application/ #tar zcf muti.tomcat.tar.gz ./tomcat8_1 ./tomcat8_2打包,以后用直接解压
7、nginx反向代理tomcat
#yum安装nginx yum -y install nginx /usr/share/nginx/html #yum安装nginx的站点目录 /etc/nginx/nginx.conf #yum安装nginx的配置文件 vim /etc/nginx/nginx.conf #配置nginx负载均衡 upstream web_pools { server 172.19.5.10:8081; server 172.19.5.10:8082; } server { listen 80; server_name localhost; location / { index index.jsp index.html index.htm; proxy_pass http://web_pools; } }
模拟数据
echo 8081 >/application/tomcat8_1/webapps/ROOT/index.jsp echo 8082 >/application/tomcat8_2/webapps/ROOT/index.jsp
测试负载均衡效果
8、jpress部署
yum -y install mariadb-server#数据库在7上面已改名 systemctl start mariadb.service mysql#登录mysql create database jpress DEFAULT CHARACTER SET utf8; grant all on jpress.* to jpress@'localhost' identified by '123456'; #上传代码到站点目录 /application/tomcat8_1/webapps mv jpress-web-newest jpress mv jpress-web-newest.war /tmp/ http://172.19.5.10:8081/jpress/install http://172.19.5.10:8081/jpress/admin/login
war包手动解压:
jar -xf memtest.war
9、tomcat监控
①直接获取tomcat的pid值
jps -lvm #快速获取Java进程的pid,不用ps和grep
②windows监控tomcat工具
C:\Program Files\Java\jdk1.8.0_31\bin jconsole.exe jvisualvm.exe
③配置tomcat支持远监控功能
vim /application/tomcat/bin/catalina.sh 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=172.19.5.10" netstat -lntup|grep 12345
遇到报错
错误: 代理抛出异常错误: java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: tomcat: tomcat: unknown error
解决:主机名域名解析解决tomcat无法启动
④使用zabbix监控tomcat
yum install -y zabbix-java-gateway#前面已安装 #配置
/etc/init.d/zabbix-java-gateway start sed -i -e '217a JavaGateway=127.0.0.1' -e '225a JavaGatewayPort=10052' -e '235a StartJavaPollers=5' /etc/zabbix/zabbix_server.conf /etc/init.d/zabbix-server restart
⑤zabbix网页添加监控tomcat
四、tomcat优化
1、安全优化
①关闭管理端口保护
#修改默认的8005端口,"SHUTDOWN"字符串也要改
②注释ajp连接端口保护
#注释此行
③禁用管理端
#webapps下面只保留一个ROOT空目录,其余的都清除
④tomcat降权启动
#降权,降低权限,像nginx,tomcat等都支持以普通用户的方式启动
useradd tomcat cp -a /application/tools/tomcat8_1 /home/tomcat/ chown -R tomcat.tomcat /home/tomcat/tomcat8_1/ su -c '/home/tomcat/tomcat8_1/bin/startup.sh' tomcat
⑤生产环境关闭自动部署功能
#true改为false
2、tomcat性能优化
Tomcat是最消耗内存的,如果有无限大的内存,tomcat就跑的特别快,优化方法如下:
①加大内存
②减少内存的消耗量:开发写得代码只是不停消耗内存,但是从来不释放
垃圾回收机制:定期扫描内存使用情况,找到未被使用的内存,回收周期性,一定内存使用量,每次垃圾回收都会使业务停止文档,jvm优化主要从垃圾回收策略开始阐述
③定期重启Tomcat
3、修改tomcat使用内存,tomcat默认消耗系统内存的1/4
vim /home/tomcat/tomcat8_1/bin/catalina.sh #97行 JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms3072m -Xmx3072m -XX:NewSize=1536m -XX:MaxNewSize=1536m -XX:PermSize=1536m -XX:MaxPermSize=1536m"
访问测试
http://172.19.5.10:8081/meminfo.jsp
4、Tomcat获取用户IP地址
①tomcat配置文件/conf/server.xml
下配置
"org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t %r %s %b" />
②前面有负载均衡的时候,获取真实IP可以使用下面的配置
"org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%{X-Forwarded-For}i %h %l %u %t %r %s %b" />
五、tomcat启动脚本开发
[root@tomcat scripts]# cat tomcat.sh
#!/bin/sh
Tomcat_Path=/application/tomcat/
usage() {
echo "usage: $0 [start|stop|status|restart]"
}
status_tomcat() {
ps aux|grep java|grep tomcat|grep -v grep
}
start_tomcat() {
/application/tomcat/bin/startup.sh
echo "tomcat is starting"
}
stop_tomcat() {
TomcatPid=$(ps aux|grep java|grep tomcat|grep -v grep|awk '{print $2}')
kill -9 $TomcatPid
sleep 5
TSTAT=$(ps aux|grep java|grep tomcat|grep -v grep|awk '{print $2}')
if [ -z $TSTAT ];then
echo "tomcat is stopped"
else
kill -9 $TSTAT
fi
cd ${Tomcat_Path}
rm -fr temp/*
rm -fr work/*
}
main() {
case $1 in
start)
start_tomcat;;
stop)
stop_tomcat;;
status)
status_tomcat;;
restart)
stop_tomcat && start_tomcat;;
*)
usage;
esac
}
main $1
六、tomcat在CentOS7系统上启动慢
1、tomcat启动慢原因现象
在CentOS7启动Tomcat时,启动过程很慢,需要几分钟,经过查看日志,发现耗时在这里:是session引起的随机数问题导致的
2、解决办法
①方法
vim $JAVA_HOME/jre/lib/security/java.security securerandom.source=file:/dev/random #改为 securerandom.source=file:/dev/urandom
②方法
vim $TOMCAT_HOME/bin/catalina.sh if [[ "$JAVA_OPTS" != *-Djava.security.egd=* ]]; then JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/urandom" fi
③方法
yum install rng-tools -y
systemctl start rngd.service
systemctl enable rngd.service
七、Jar包介绍
很多时候开发会给我们一个jar包,让我们运行,启动命令如下:
nohup java -jar jar包名称.jar > user.out &