1. Tomcat简介
Tomcat是Apache软件基金会的Jakarta项目中的一个核心项目,由Apache、Sun和其他公司及个人共同开发而成。
Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。
Tomcat和Nginx、Apache(httpd)、lighttpd等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Nginx/Apache服务器。
2. Tomcat安装
2.1 把预先下载好的包拉进来
[root@localhost ~]# rz ---没有rz命令的话用‘yum install lrzsz’安装
[root@localhost ~]# ls
anaconda-ks.cfg apache-tomcat-8.5.38.tar.gz jdk-8u201-linux-x64.tar.gz
2.2 部署java环境
[root@localhost ~]# mkdir /application/
[root@localhost ~]# tar xf jdk-8u201-linux-x64.tar.gz -C /application/
[root@localhost ~]# ln -s /application/jdk1.8.0_201/ /application/jdk ---创建软链接
[root@localhost ~]# sed -i.ori '$a export JAVA_HOME=/application/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile ---修改环境变量
[root@localhost ~]# source /etc/profile
[root@localhost ~]# java -version ---能显示java版本证明安装成功了
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
2.3 安装Tomcat
[root@localhost ~]# tar xf apache-tomcat-8.5.38.tar.gz -C /application/
[root@localhost ~]# ln -s /application/apache-tomcat-8.5.38/ /application/tomcat
[root@localhost ~]# echo 'export TOMCAT_HOME=/application/tomcat' >> /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# chown -R root.root /application/jdk /application/tomcat/ ---重新授权,因为解压出来后权限是不正确的
[root@localhost ~]# tail -4 /etc/profile
export JAVA_HOME=/application/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
export TOMCAT_HOME=/application/tomcat
3. Tomcat目录介绍
[root@localhost ~]# cd /application/tomcat/bin
[root@localhost bin]# ls
bootstrap.jar commons-daemon-native.tar.gz digest.sh startup.bat tool-wrapper.sh
catalina.bat configtest.bat setclasspath.bat startup.sh version.bat
catalina.sh configtest.sh setclasspath.sh tomcat-juli.jar version.sh
catalina-tasks.xml daemon.sh shutdown.bat tomcat-native.tar.gz
commons-daemon.jar digest.bat shutdown.sh tool-wrapper.bat
[root@localhost 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.5.38
Server built: Feb 5 2019 11:42:42 UTC
Server number: 8.5.38.0
OS Name: Linux
OS Version: 3.10.0-123.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_201-b09
JVM Vendor: Oracle Corporation
[root@localhost ~]# ll /application/tomcat/webapps/
total 8
drwxr-x---. 14 root root 4096 Mar 1 04:50 docs ---对应主页的Documentation选项
drwxr-x---. 6 root root 78 Mar 1 04:50 examples ---对应主页的examples选项
drwxr-x---. 5 root root 82 Mar 1 04:50 host-manager
drwxr-x---. 5 root root 97 Mar 1 04:50 manager
drwxr-x---. 3 root root 4096 Mar 1 04:50 ROOT
[root@localhost ~]# ll /application/tomcat/webapps/ROOT/
total 188
-rw-r-----. 1 root root 27235 Feb 5 19:45 asf-logo-wide.svg
-rw-r-----. 1 root root 713 Feb 5 19:44 bg-button.png
-rw-r-----. 1 root root 1918 Feb 5 19:44 bg-middle.png
-rw-r-----. 1 root root 1392 Feb 5 19:44 bg-nav-item.png
-rw-r-----. 1 root root 1401 Feb 5 19:44 bg-nav.png
-rw-r-----. 1 root root 3103 Feb 5 19:44 bg-upper.png
-rw-r-----. 1 root root 21630 Feb 5 19:44 favicon.ico
-rw-r-----. 1 root root 12290 Feb 5 19:45 index.jsp ---tomcat默认访问页面
-rw-r-----. 1 root root 7142 Feb 5 19:45 RELEASE-NOTES.txt
-rw-r-----. 1 root root 5581 Feb 5 19:45 tomcat.css
-rw-r-----. 1 root root 2066 Feb 5 19:44 tomcat.gif
-rw-r-----. 1 root root 5103 Feb 5 19:44 tomcat.png
-rw-r-----. 1 root root 2376 Feb 5 19:44 tomcat-power.gif
-rw-r-----. 1 root root 67795 Feb 5 19:45 tomcat.svg
drwxr-x---. 2 root root 20 Mar 1 04:50 WEB-INF
4. 启动Tomcat
[root@localhost ~]# /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@localhost ~]# netstat -antp | grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 17196/java
tcp6 0 0 :::8009 :::* LISTEN 17196/java
tcp6 0 0 :::8080 :::* LISTEN 17196/java
5. 访问网站
[root@localhost ~]# firewall-cmd --add-port=8080/tcp ---加了“--permanent”参数是不会立刻生效的,所以需要先不加该参数执行一下,即时放行
success
[root@localhost ~]# firewall-cmd --add-port=8080/tcp --permanent ---永久放行8080端口
success
网址:192.168.1.160:8080
[root@localhost ~]# ll /application/tomcat/logs/
total 24
-rw-r-----. 1 root root 6838 Mar 1 05:09 catalina.2019-03-01.log ---catalina.{yyyy-MM-dd}.log是tomcat自己运行的一些日志,不包含应用向console输出的日志
-rw-r-----. 1 root root 6838 Mar 1 05:09 catalina.out ---存储tomcat运行自己输出的日志以及应用里向console输出的日志(一直累积,文件会越来越大,需要定期清空)
-rw-r-----. 1 root root 0 Mar 1 05:08 host-manager.2019-03-01.log
-rw-r-----. 1 root root 459 Mar 1 05:09 localhost.2019-03-01.log
-rw-r-----. 1 root root 829 Mar 1 05:13 localhost_access_log.2019-03-01.txt
-rw-r-----. 1 root root 0 Mar 1 05:08 manager.2019-03-01.log
[root@localhost ~]# tailf /application/tomcat/logs/catalina.out
01-Mar-2019 05:09:22.885 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/application/apache-tomcat-8.5.38/webapps/docs] has finished in [17] ms
01-Mar-2019 05:09:22.886 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/application/apache-tomcat-8.5.38/webapps/examples]
01-Mar-2019 05:09:23.237 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/application/apache-tomcat-8.5.38/webapps/examples] has finished in [351] ms
01-Mar-2019 05:09:23.237 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/application/apache-tomcat-8.5.38/webapps/host-manager]
01-Mar-2019 05:09:23.266 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/application/apache-tomcat-8.5.38/webapps/host-manager] has finished in [28] ms
01-Mar-2019 05:09:23.266 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/application/apache-tomcat-8.5.38/webapps/manager]
01-Mar-2019 05:09:23.292 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/application/apache-tomcat-8.5.38/webapps/manager] has finished in [26] ms
01-Mar-2019 05:09:23.313 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
01-Mar-2019 05:09:23.327 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
01-Mar-2019 05:09:23.331 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 37269 ms ---看到 startup in xxxx ms表示启动成功