Tomcat监听80端口配置Tomcat虚拟主机Tomcat日志

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat监听80端口配置Tomcat虚拟主机Tomcat日志相关的知识,希望对你有一定的参考价值。

配置Tomcat监听80端口

1.配置Tomcat服务的访问端口

Tomcat默认启动的端口是8080,如果你想修改为80,则需要修改server.xml文件。

[[email protected] src]# vim /usr/local/tomcat/conf/server.xml

按如下需求更改:
找到Connector port="8080" protocol="HTTP/1.1"修改为Connector port="80" protocol="HTTP/1.1"

[[email protected] 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
[[email protected] 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.
[[email protected] src]# ps aux |grep java
root      21382 15.6  8.1 2294080 81052 pts/1   Sl   17:17   0:05 /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 -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      21426  0.0  0.0 112680   972 pts/1    R+   17:18   0:00 grep --color=auto java
[[email protected] src]# netstat -lntp |grep java
tcp6       0      0 :::80                   :::*                    LISTEN      21382/java          
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      21382/java          
tcp6       0      0 :::8009                 :::*                    LISTEN      21382/java          

技术分享图片

配置Tomcat的虚拟主机

1.介绍Tomcat虚拟主机的概念

打开配置文件/usr/local/tomcat/conf/server.xml查看它的结构

技术分享图片

其中<Host>和</Host>之间的配置为虚拟主机配置部分,name定义域名,
appBase定义应用的目录,Java的应用通常是一个war的压缩包,你只需要将war的压缩包放到appBase目录下面即可。

之前浏览器访问的默认页其实就是在appBase目录下面,不过是在它子目录ROOT里:

[[email protected] src]# cd /usr/local/tomcat/webapps/
[[email protected] webapps]# ls ROOT
asf-logo-wide.svg  bg-middle.png    bg-nav.png    favicon.ico  RELEASE-NOTES.txt  tomcat.gif  tomcat-power.gif  WEB-INF
bg-button.png      bg-nav-item.png  bg-upper.png  index.jsp    tomcat.css         tomcat.png  tomcat.svg

其中index.jsp就是Tomcat的默认页面,你也可以用curl命令来访问一下tomcat.gif图片:

[[email protected] webapps]# curl localhost:8080/tomcat.gif -I
HTTP/1.1 200 
Accept-Ranges: bytes
ETag: W/"2066-1511789494000"
Last-Modified: Mon, 27 Nov 2017 13:31:34 GMT
Content-Type: image/gif
Content-Length: 2066
Date: Thu, 18 Jan 2018 07:36:57 GMT
2.增加一个虚拟主机
[[email protected] src]# vim /usr/local/tomcat/conf/server.xml 

增加如下配置内容:

<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>

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

示例如图:

技术分享图片

举例说明:

下面我们通过部署一个java的应用来体会appBase和docBase目录的作用

1.下载zrlog,建立一个博客测试
[[email protected] src]# wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war  //下载zrlog 
[[email protected] src]# mv zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/
[[email protected] src]# mv /usr/local/tomcat/webapps/zrlog-1.7.1-baaecb9-release /usr/local/tomcat/webapps/zrlog
[[email protected] webapps]# ls
docs  examples  host-manager  manager  ROOT  zrlog  zrlog-1.7.1-baaecb9-release  zrlog-1.7.1-baaecb9-release.war
浏览器访问 ip:8080/zrlog

技术分享图片

2.数据库配置
[[email protected] webapps]# ps aux |grep mysql
root       3237  0.0  0.1 113268  1364 ?        S    1月12   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/gary-tao.pid
mysql      3373  0.0 45.3 1301004 453000 ?      Sl   1月12   3:17 /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/gary-tao.err --pid-file=/data/mysql/gary-tao.pid --socket=/tmp/mysql.sock
root      23828  0.0  0.0 112680   976 pts/1    R+   11:23   0:00 grep --color=auto mysql
[[email protected] webapps]# mysql -uroot -pszyino-123
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 46
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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 by ‘szyino-123‘;  //创建一个用户
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
[[email protected] webapps]# mysql -uzrlog -h127.0.0.1 -pszyino-123
//检测创建用户是否可以连接
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 47
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
| zrlog              |
+--------------------+
3 rows in set (0.00 sec)
3.配置zrlog安装

第一步填写数库信息(在浏览器中保存密码)
技术分享图片

第二步填写网站信息(在浏览器中保存密码)
技术分享图片

第三步完成后点击查看
技术分享图片

4.去掉浏览器访问时输入的目录名
[[email protected] webapps]# mkdir /data/wwwroot/123.cn
[[email protected] webapps]# mv /usr/local/tomcat/webapps/zrlog/* /data/wwwroot/123.cn/
[[email protected] 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
[[email protected] 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.
[[email protected] webapps]# !net
netstat -lntp |grep java
tcp6       0      0 :::80                   :::*                    LISTEN      24437/java          
tcp6       0      0 127.0.0.1:55089         :::*                    LISTEN      24457/java          
tcp6       0      0 127.0.0.1:45089         :::*                    LISTEN      24457/java          
tcp6       0      0 127.0.0.1:25089         :::*                    LISTEN      24457/java          
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      24437/java          
tcp6       0      0 :::8009                 :::*                    LISTEN      24437/java          
[[email protected] webapps]# netstat -lntp |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      24437/java          
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      24437/java          
tcp6       0      0 :::8009                 :::*                    LISTEN      24437/java   
本地浏览器测试
  1. 添加绑定本地hosts文件,如172.16.111.100 www.123.cn
  2. 打开本机cmd,ping www.123.cn 是否通信,如下图:
    技术分享图片
  3. 浏览器访问,正常。
    技术分享图片

Tomcat日志

Tomcat的日志目录为/usr/local/tomcat/logs,主要有四大类日志:

[[email protected] tomcat]# ls /usr/local/tomcat/logs
catalina.2018-01-17.log  host-manager.2018-01-17.log  localhost.2018-01-18.log             manager.2018-01-17.log
catalina.2018-01-18.log  host-manager.2018-01-18.log  localhost_access_log.2018-01-17.txt  manager.2018-01-18.log
catalina.out             localhost.2018-01-17.log     localhost_access_log.2018-01-18.txt

解释说明:

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

访问日志默认不会生成,需要在/usr/local/tomcat/conf/server.xml中配置一下,具体方法是在对应虚拟主机的<Host></Host>里面加入下面的配置(假如域名为123.cn):

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
         prefix="123.cn_access" suffix=".log"
         pattern="%h %l %u %t &quot;%r&quot; %s %b" />

示例图如下:

技术分享图片

  1. prefix定义访问日志的前缀;
  2. suffix定义日志的后缀;
  3. pattern定义日志格式。
  4. 新增加的虚拟主机默认并不会生成类似默认虚拟主机的那个localhost.日期.log日志,错误日志会统一记录到catalina.out中。
备注:关于Tomcat日志,你最需要关注catalina.out,当出现问题时,我们应该第一想到去查看它。

以上是关于Tomcat监听80端口配置Tomcat虚拟主机Tomcat日志的主要内容,如果未能解决你的问题,请参考以下文章

配置Tomcat监听80端口配置Tomcat的虚拟主机Tomcat的日志

配置Tomcat监听80端口配置Tomcat的虚拟主机日志配置

配置tomcat监听80端口,配置tomcat的虚拟主机

Tomcat监听80端口配置Tomcat虚拟主机Tomcat日志

58.配置Tomcat监听80端口 配置Tomcat虚拟主机Tomcat日志

配置Tomcat监听80端口 配置Tomcat虚拟主机 Tomcat日志