16.4 配置Tomcat监听80端口
tomcat默认监听8080端口,但是用户输入域名必须要写上端口号,这样体验不友好。
可以把tomcat的监听端口改为80,域名中的80端口默认可以省略。
自定义端口:8080改为80,编辑配置文件server.xml
[root@nginx ~]# vim /usr/local/tomcat/conf/server.xml
<Connector port="80" protocol="HTTP/1.1" //port="8080"改为"80"
connectionTimeout="20000"
redirectPort="8443" />
如果安装后nginx,先停止服务,nginx也是监听80端口,会冲突(生产中别停nginx,可以用nginx去代理tomcat)。再重启tomcat服务
[root@nginx ~]# /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@nginx ~]# /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.
访问验证:直接输入192.168.87.128,不加端口号
16.5/16.6/16.7 配置Tomcat虚拟主机
一个ip下有多个域名,一个域名访问一个网站,所以一个ip可以访问多个网站,通过配置tomcat的虚拟主机实现
编辑tomcat配置文件:<Host> </Host> 配置段
vim /usr/local/tomcat/conf/server.xml
<Host name="www.123.com" appBase="webapps" //name是网站域名 appBase是应用项目的程序:在/tomcat/webapps/ROOT目录下,是提供一个war包压缩文件,不像html和php文件
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<context path="" docBase="/data/wwwroot/123.com/" //如果不是指定appBase的war包,而是单独指定一个目录,存放jsp文件,就用docBase来定义。
debug="0" reloadable="true" crossContext="true">
</Host>
##war包 包含了运行网站的文件,配置,jsp代码,数据库,静态文件等。
用zrlog博客演示:如果定义了appBase,把war包放在webapps目录下,war包会自动解压成目录,删除war包,解压的目录也会自动删除。
rz //Windows下载,rz工具传到linux系统
[root@nginx src]# cp ROOT.war /usr/local/tomcat/webapps/zrlog.war //拷贝war包到定义的appBase下
[root@nginx src]# cd /usr/local/tomcat/webapps/
[root@nginx webapps]# ls
docs examples host-manager manager ROOT zrlog zrlog.war //自动解压了zrlog的war包
访问zrlog:
进入了zrlog的安装向导,设置数据库等信息:
[root@nginx webapps]# mysql -uroot -p7826078
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> create database zrlog; //创建zrlog的库
Query OK, 1 row affected (0.02 sec)
mysql> grant all on zrlog.* to 'zrlog'@127.0.0.1 identified by '7826078'; //创建zrlog数据库的用户,授权进行操作库
Query OK, 0 rows affected (0.01 sec)
[root@nginx webapps]# mysql -uzrlog -h127.0.0.1 -p7826078 //用zrlog登录mysql
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 8
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.01 sec)
填写数据库信息:
设置管理后台用户名和密码,之后就完成安装,即可使用zrlog了:
登录后台后,编写一篇博客:
根据之前server.xml的虚拟主机配置 123.com,我们可以把zrlog目录放到docBase下,就可以直接访问w ww.123.com 域名访问zrlog博客了。
[root@nginx ~]# mv /usr/local/tomcat/webapps/zrlog /data/wwwroot/123.com
[root@nginx ~]# cd /data/wwwroot/123.com/
[root@nginx 123.com]# ls
admin assets error favicon.ico include META-INF WEB-INF
修改配置文件后,重启tomcat服务
[root@nginx 123.com]# /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@nginx 123.com]# /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.
把www .123.com 加入Windows的hosts文件,通过浏览器访问 www .123.com 就可以访问zrlog博客了
localhost可以访问 /usr/local/tomcat/webapps/ROOT下的网站
16.8 Tomcat日志
[root@nginx ~]# cd /usr/local/tomcat/logs/ //tomcat日志目录
[root@nginx logs]# ls
catalina.2018-06-26.log host-manager.2018-06-26.log localhost.2018-06-27.log manager.2018-06-26.log
catalina.2018-06-27.log host-manager.2018-06-27.log localhost_access_log.2018-06-26.txt manager.2018-06-27.log
catalina.out localhost.2018-06-26.log localhost_access_log.2018-06-27.txt
catalina开头的日志是综合日志,记录服务相关的信息的,也会记录错误信息(启动失败等):关注“严重”的信息
catalina.2018-xx-xx.log和catalina.out内容相同,前者会每天生成一个新的日志
host-manager.2018-06-27.log和manager.2018-06-27.log是管理相关的日志:host-manager是虚拟主机管理日志
localhost.2018-06-26.log和localhost_access_log.2018-06-26.txt是虚拟主机相关日志
localhost_access是虚拟主机访问日志,localhost是默认虚拟主机的错误日志
虚拟主机的访问日志不会自动生成,需要在配置文件里定义:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt" //prefix日志名前缀、suffix日志名后缀
pattern="%h %l %u %t "%r" %s %b" /> //日志格式
新增加的虚拟主机默认不会生成localhost.日期.log日志,错误日志则记录在catalina.out日志中
Tomcat中最需要关注的是catalina.out日志,出问题的时候第一时间去查看它。