tomcat介绍与安装使用

文章目录

    • Tomcat介绍
    • 安装jdk
    • 安装Tomcat
    • 配置Tomcat监听80端口
    • 配置Tomcat的虚拟主机
    • Tomcat日志

Tomcat介绍

目前有很多网站使用jsp的程序编写,所以解析jsp的程序就必须要有相关的软件来完成。Tomcat就是用来解析jsp程序的一个软件,Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache、Sun和其他一些公司及个人共同开发而成。因为Tomcat技术先进、性能稳定,而且免费,因而深受Java爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Tomcat是一个轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache服务器,可利用它响应对HTML 页面的访问请求。实际上Tomcat 部分是Apache服务器的扩展,但它是独立运行的,所以当你运行tomcat时,它实际上作为一个与Apache 独立的进程单独运行的

  • java程序写的网站用tomcat+jdk来运行
  • tomcat是一个中间件,真正起作用的,解析java脚本的是jdk
  • jdk(java development kit)是整个java的核心,它包含了java运行环境和一堆java相关的工具以及java基础库。

安装jdk

浏览器打开:

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

存放在/usr/local/src中

解压:

[root@zhangfei src]# tar zxvf jdk-8u144-linux-x64.tar.gz

查看目录大小:

[root@zhangfei src]# du -sh jdk1.8.0_144/
363M    jdk1.8.0_144/

移动目录:

[root@zhangfei src]# mv jdk1.8.0_144/ /usr/local/jdk1.8

定义环境变量:

[root@zhangfei 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

配置生效:

[root@zhangfei src]# source /etc/profile

检查jdk是否安装成功:

[root@zhangfei src]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

出现以上结果代表安装成功

若结果不同,则要搜索是否安装过openjdk:

[root@zhangfei src]# rpm -qa  |grep openjdk

安装Tomcat

下载tmocat:

[root@zhangfei src]# wget http://apache.fayea.com/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz

解压:

[root@zhangfei src]# tar zxvf apache-tomcat-8.5.20.tar.gz

移动目录:

[root@zhangfei src]# mv apache-tomcat-8.5.20 /usr/local/tomcat

启动(二进制包不用编译):

[root@zhangfei 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.

查看是否启动成功:

[root@zhangfei src]# ps aux | grep java
root      2557 31.8  8.1 2296136 82288 pts/0   Sl   22:48   0:17 /usr/local/jdk1.8/bin/java -java.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 -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      2601  0.0  0.0 112664   972 pts/0    S+   22:49   0:00 grep --color=auto java

不支持restart,必须要先stop然后start

[root@zhangfei 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@zhangfei src]# !ps
ps aux | grep java
root      2638  0.0  0.0 112664   976 pts/0    S+   22:51   0:00 grep --color=auto java
[root@zhangfei 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.
[root@zhangfei src]# netstat -lntp |grep java
tcp6       0      0 :::8080                 :::*                    LISTEN      2653/java           
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      2653/java           
tcp6       0      0 :::8009                 :::*                    LISTEN      2653/java
  • 8080端口是提供web服务的端口
  • 8005端口为管理端口
  • 8009端口为第三方服务调用的端口

清空规则:

[root@zhangfei src]# iptables -F

然后在浏览器输入192.168.137.20:8080 则会看到tomcat的界面

配置Tomcat监听80端口

tomcat支持自定义端口

配置tomcat监听端口为80:

[root@zhangfei ~]# vim 
/usr/local/tomcat/conf/server.xml
    

重启服务:

[root@zhangfei ~]# /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@zhangfei ~]# /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@zhangfei ~]# netstat -lntp |grep java
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      2636/java           
tcp6       0      0 :::8009                 :::*                    LISTEN      2636/java

没有80端口

查看80端口:

[root@zhangfei ~]# netstat -lntp |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1573/nginx: master  
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      2636/java           
tcp6       0      0 :::8009                 :::*                    LISTEN      2636/java

可以看到80端口被nginx占用

停掉nginx:

[root@zhangfei ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl):                            [  确定  ]

再次查看80端口是被占用:

[root@zhangfei ~]# !net
netstat -lntp |grep 80
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      2636/java           
tcp6       0      0 :::8009                 :::*                    LISTEN      2636/java

重启tomcat:

[root@zhangfei ~]# /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@zhangfei ~]# /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@zhangfei ~]# !net
netstat -lntp |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      2766/java           
tcp6       0      0 :::8009                 :::*                    LISTEN      2766/java

配置Tomcat的虚拟主机

打开配置文件:

[root@zhangfei ~]# vim /usr/local/tomcat/conf/server.xml
  • 最后面Host这部分就是一个虚拟主机
  • tomcat需要提供一个war的包,包含运行这个网站的一些文件,包裹配置、代码、数据库相关的(图片),需要放到webapp里面
  • unpackWARs=“true” 意思是是否自动解压这个包
  • 也可以不用war包,可以自己定义一个目录,即docbase
    在原Host后面再创建一个虚拟主机:

在Host这一块后面增加:

    


第一行的"www.123.cn"后面原本有个;

网站的程序放在/data/wwwroot/123.cn/里

  • docBase,这个参数用来定义网站的文件存放路径,如果不定义,默认是在appBase/ROOT下面,定义了docBase就以该目录为主了,其中appBase和docBase可以一样。在这一步操作过程中很多同学遇到过访问404的问题,其实就是docBase没有定义对。
  • appBase为应用存放目录,通常是需要把war包直接放到该目录下面,它会自动解压成一个程序目录
    下面我们通过部署一个java的应用来体会appBase和docBase目录的作用

下载zrlog:

[root@zhangfei src]# wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war

拷贝到自动解压的目录:

[root@zhangfei src]# cp zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/

查看是否自动解压:

[root@zhangfei src]# ls /usr/local/tomcat/webapps/
docs      host-manager  ROOT                         zrlog-1.7.1-baaecb9-release.war
examples  manager       zrlog-1.7.1-baaecb9-release

改名:

[root@zhangfei webapps]# mv zrlog-1.7.1-baaecb9-release zrlog
[root@zhangfei webapps]# ls
docs      host-manager  ROOT   zrlog-1.7.1-baaecb9-release
examples  manager       zrlog  zrlog-1.7.1-baaecb9-release.war

可以看到,此时压缩包又解压了一次
在浏览器访问http://192.168.137.20/zrlog/

查看mysql是否启动:

[root@zhangfei webapps]# ps aux | grep mysql
root      2091  0.0  0.1 115376  1680 ?        S    21:49   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/zhangfei.pid
mysql     2284  0.1 45.2 1046784 456148 ?      Sl   21:49   0:06 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/zhangfei.err --pid-file=/data/mysql/zhangfei.pid --socket=/tmp/mysql.sock
root      3125  0.0  0.0 112664   972 pts/0    R+   23:22   0:00 grep --color=auto mysql

进入到数据库:

[root@zhangfei webapps]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

创建数据库:

mysql> create database zrlog;
Query OK, 1 row affected (0.05 sec)

创建用户:

mysql> grant all on zrlog.* to 'zrlog'@127.0.0.1 identified by '94264334';
Query OK, 0 rows affected (0.00 sec)

不想在指里显示zrlog

[root@zhangfei webapps]# mv /usr/local/tomcat/webapps/zrlog/* /data/wwwroot/123.cn/

在windows的hosts绑定 www.123.cn

重启服务:

[root@zhangfei webapps]# /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@zhangfei webapps]# /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@zhangfei webapps]# netstat -lntp |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      3297/java           
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      3297/java           
tcp6       0      0 :::8009                 :::*                    LISTEN      3297/java

自定义的appbase下也应该有个ROOT目录

Tomcat日志

  • 其中catalina开头的日志为Tomcat的综合日志,它记录Tomcat服务相关信息,也会记录错误日志。 其中catalina.2017-xx-xx.log和catalina.out内容相同,前者会每天生成一个新的日志。
  • host-manager和manager为管理相关的日志,其中host-manager为虚拟主机的管理日志。
  • localhost和localhost_access为虚拟主机相关日志,其中带access字样的日志为访问日志,不带access字样的为默认虚拟主机的错误日志。
  • 访问日志默认不会生成,需要在server.xml中配置一下。

查看日志:

[root@zhangfei webapps]# ls /usr/local/tomcat/logs/
catalina.2017-08-28.log      host-manager.2017-08-29.log          localhost_access_log.2017-08-29.txt
catalina.2017-08-29.log      localhost.2017-08-28.log             manager.2017-08-28.log
catalina.out                 localhost.2017-08-29.log             manager.2017-08-29.log
host-manager.2017-08-28.log  localhost_access_log.2017-08-28.txt

你可能感兴趣的:(基础篇)