- 目录
- 1.Tomcat介绍
- 2.安装jdk
- 3.安装Tomcat
- 4.配置Tomcat监听80端口
- 5.配置Tomcat的虚拟主机
- 6.Tomcat日志
- 扩展
1.Tomcat介绍
Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache、Sun和其他一些公司及个人共同开发而成。java程序写的网站用tomcat+jdk来运行;tomcat是一个中间件,真正起作用的,解析java脚本的是jdk; jdk(java development kit)是整个java的核心,它包含了java运行环境和一堆java相关的工具以及java基础库。最主流的jdk为sun公司发布的jdk,除此之外,其实IBM公司也有发布JDK,CentOS上也可以用yum安装openjdk 。
2.安装jdk
jdk版本有1.6、1.7、1.8 。
下载安装包http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
传输文件
移动安装包到/usr/local/src 目录下,解压并移动到/usr/local/下重命名为jdk1.8
[root@chenshi src]# mv jdk1.8.0_171/ /usr/local/jdk1.8
修改/etc/profile 增加如下内容
[root@chenshi src]# vim /etc/profile
JAVA_HOME=/usr/local/jdk1.8/
JAVA_BIN=/usr/local/jdk1.8/bin
JRE_HOME=/usr/local/jdk1.8/jre
PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin
CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar
重新加载配置文件,检查java是不是正常安装
[root@chenshi src]# source /etc/profile
[root@chenshi src]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
3.安装Tomcat
下载Tomcat安装包,解压
[root@chenshi src]# mv /root/apache-tomcat-8.5.32.tar.gz .
[root@chenshi src]# tar zxvf apache-tomcat-8.5.32.tar.gz
移动解压后的包,并启动服务
[root@chenshi src]# mv apache-tomcat-8.5.32 /usr/local/tomcat
[root@chenshi src]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
查看是否有java进程
[root@chenshi src]# ps aux|grep java
root 9315 7.8 1.1 5114416 113344 pts/3 Sl 10:33 0:02 /usr/local/jdk1.8/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 9408 0.0 0.0 112724 968 pts/3 S+ 10:34 0:00 grep --color=auto java
关闭tomcat ;不支持restart
[root@chenshi src]# /usr/local/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@chenshi src]# !ps
ps aux|grep java
root 9498 0.0 0.0 112724 972 pts/3 S+ 10:35 0:00 grep --color=auto java
查看监听端口
[root@chenshi src]# !netstat
netstat -lntp|grep java
tcp6 0 0 :::8080 :::* LISTEN 9551/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 9551/java
tcp6 0 0 :::8009 :::* LISTEN 9551/java
三个端口8080为提供web服务的端口,8005为管理端口,8009端口为第三方服务调用的端口,比如httpd和Tomcat结合时会用到
浏览器访问
4.配置Tomcat监听80端口
编辑配置文件
[root@chenshi ~]# vim /usr/local/tomcat/conf/server.xml
搜索 8080
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改端口需要重启服务
需要停用nginx服务,因为80端口被占用了。如果8005端口没有启动访问页面会很慢
[root@chenshi ~]# /usr/local/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@chenshi ~]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@chenshi ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 680/pure-ftpd (SERV
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 15363/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1385/master
tcp6 0 0 :::3306 :::* LISTEN 17950/mysqld
tcp6 0 0 :::80 :::* LISTEN 11707/java
tcp6 0 0 :::21 :::* LISTEN 680/pure-ftpd (SERV
tcp6 0 0 :::22 :::* LISTEN 15363/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1385/master
tcp6 0 0 :::2333 :::* LISTEN 11417/python
tcp6 0 0 :::8009 :::* LISTEN 11707/java
测试
5.配置Tomcat的虚拟主机
编辑配置文件搜索Host
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
表示虚拟主机
在</Host>下增加一台新的虚拟主机(appBase和docBase必须有一个为空,appBase放war包)
<Host name="www.123.cn" appBase=""
unpackWARs= "true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/data/wwwroot/123.cn/(加压后的包所放位置)" debug="0" reloadable="true" crossContext="true"/>
</Host>
下载一个war 包
[root@chenshi src]# wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war
[root@chenshi src]# du -sh zrlog-1.7.1-baaecb9-release.war
6.8M zrlog-1.7.1-baaecb9-release.war
默认appbase在/usr/local/tomcatwebapps下
拷贝后war文件被自动解压
[root@chenshi src]# cp zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/
[root@chenshi src]# cd!$
cd/usr/local/tomcat/webapps/
-bash: cd/usr/local/tomcat/webapps/: 没有那个文件或目录
[root@chenshi src]# cd /usr/local/tomcat/webapps/
[root@chenshi webapps]# ls
docs host-manager ROOT zrlog-1.7.1-baaecb9-release.war
examples manager zrlog-1.7.1-baaecb9-release
改名成zrlog,用浏览器访问会出现安装向导
创建mysql库
[root@chenshi webapps]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on zrlog.* to 'zrlog'@127.0.0.1 identified '123456';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''123456'' at line 1
mysql> grant all on zrlog.* to 'zrlog'@127.0.0.1 identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
检查用户
[root@chenshi webapps]# mysql -uzrlog -h127.0.0.1 -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
| zrlog |
+--------------------+
3 rows in set (0.00 sec)
mysql>
安装完成
docbase是直接放网站程序的,appbase是放war包的。
6.Tomcat日志
[root@chenshi webapps]# ls /usr/local/tomcat/logs/
catalina.2018-07-16.log host-manager.2018-07-16.log localhost_access_log.2018-07-16.txt
catalina.out localhost.2018-07-16.log manager.2018-07-16.log
其中catalina开头的就是日志
[root@chenshi logs]# less catalina.2018-07-16.log
16-Jul-2018 10:33:43.270 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.32
16-Jul-2018 10:33:43.274 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 20 2018 19:50:35 UTC
16-Jul-2018 10:33:43.274 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.32.0
host-manager和manager为管理相关的日志,其中host-manager为虚拟主机的管理日志。
访问日志不会默认生成,要在虚拟主机中增加一段内容
[root@chenshi logs]# vim /usr/local/tomcat/conf/server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="123.cn_access" suffix=".log"
pattern="%h %l %u %t "%r" %s %b" />
错误日志会统一记录到catalina.out中
扩展
java容器比较 http://my.oschina.net/diedai/blog/271367
http://www.360doc.com/content/11/0618/21/16915_127901371.shtml
j2ee、j2se、ejb、javabean、serverlet、jsp之间关系 http://bbs.csdn.net/topics/50015576
tomcat server.xml配置详解 http://blog.csdn.net/yuanxuegui2008/article/details/6056754
tomcat常用数据库连接的方法 http://wjw7702.blog.51cto.com/5210820/1109263
JAR、WAR包区别 http://blog.csdn.net/lishehe/article/details/41607725
tomcat常见配置汇总 http://blog.sina.com.cn/s/blog_4ab26bdd0100gwpk.html
resin安装 http://fangniuwa.blog.51cto.com/10209030/1763488/
1 tomcat 单机多实例
http://www.ttlsa.com/tomcat/config-multi-tomcat-instance/
2 tomcat的jvm设置和连接数设置
http://www.cnblogs.com/bluestorm/archive/2013/04/23/3037392.html
3 jmx监控tomcat
http://blog.csdn.net/l1028386804/article/details/51547408
4 jvm性能调优监控工具jps/jstack/jmap/jhat/jstat
http://blog.csdn.net/wisgood/article/details/25343845
http://guafei.iteye.com/blog/1815222
5 gvm gc 相关
http://www.cnblogs.com/Mandylover/p/5208055.html
http://blog.csdn.net/yohoph/article/details/42041729
tomcat内存溢出
https://blog.csdn.net/ye1992/article/details/9344807