配置Tomcat架构
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置Tomcat架构相关的知识,希望对你有一定的参考价值。
[toc]
一、配置Tomcat监听80端口
1.1 查看80端口被谁占用,为了避免端口占用情况,把nginx服务关闭
[[email protected] ~]# netstat -lntp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5700/nginx: master
tcp6 0 0 :::8080 :::* LISTEN 3880/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 3880/java
tcp6 0 0 :::8009 :::* LISTEN 3880/java
[[email protected] ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl): [ 确定 ]
1.2 打开配置文件并修改
[[email protected] ~]# vim /usr/local/tomcat/conf/server.xml
搜索8080
更改80端口:
Connector port="8080" protocol="HTTP/1.1"
修改为:
Connector port="80" protocol="HTTP/1.1"
1.3 重启服务
[[email protected] ~]# /usr/local/tomcat/bin/shutdown.sh
[[email protected] ~]# /usr/local/tomcat/bin/startup.sh
[[email protected] ~]# netstat -lntp |grep 80
tcp6 0 0 :::80 :::* LISTEN 5836/java
tcp6 0 0 :::8009 :::* LISTEN 5836/java
等到8005管理端口启动以后,在网页就能访问了
[[email protected] ~]# netstat -lntp |grep 80
tcp6 0 0 :::80 :::* LISTEN 5836/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 5836/java
tcp6 0 0 :::8009 :::* LISTEN 5836/java
二、配置虚拟主机
虚拟主机也就是之前提到的,一个IP地址可以配置多个站点,绑定多个不同的域名,也就是配置多个webserver。
2.1 server.xml其实就是java的主配置文件
[[email protected] ~]# !vim
vim /usr/local/tomcat/conf/server.xml
<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>
-
[ ] name 定义域名;
-
[ ] appBase 定义应用的目录;
-
[ ] unpackWARs=”true” 是否自动解压;(也是就是说,当我们往站点目录里面直接上传一个war的包,它会自动解压)
- [ ] Java的应用通常是一个jar的压缩包,你只需要将jar的压缩包放到appBase目录下面即可。刚刚访问的Tomcat默认页其实就是在appBase目录下面,不过是在它子目录ROOT里。
2.2 配置新的虚拟主机
增加虚拟主机,编辑server.xml,在</Host>下面增加如下内容:
<Host name="www.dsf.com" appBase=""
unpackWARs= "true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/data/wwwroot/www.dsf.com/" debug="0" reloadable="true" crossContext="true"/>
</Host>
铭哥专业解释:
docBase,这个参数用来定义网站的文件存放路径,如果不定义,默认是在appBase/ROOT下面,定义了docBase就以该目录为主了,其中appBase和docBase可以一样。在这一步操作过程中很多同学遇到过访问404的问题,其实就是docBase没有定义对。
appBase为应用存放目录,通常是需要把war包直接放到该目录下面,它会自动解压成一个程序目录
2.3 部署一个java站点
a. 下载站点程序zrlog:
[[email protected] ~]# wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war
b. 创建站点目录
[[email protected] ~]# mkdir /data/wwwroot/www.dsf.com/
////我们先把站点程序放到appBASE中解压,然后再次全部移动到此目录下
c. 解压缩war包,并移动到站点目录:
[[email protected] ~]# mv zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/
等待个10几秒钟,就会出现一个自动解压出来包
[[email protected] ~]# ls /usr/local/tomcat/webapps/
docs examples host-manager manager ROOT zrlog-1.7.1-baaecb9-release zrlog-1.7.1-baaecb9-release.war
d.检查解压文件,再将其重命名
[[email protected] www.dsf.com]# cd /usr/local/tomcat/webapps/
[[email protected] webapps]# ls
docs examples host-manager manager ROOT zrlog-1.7.1-baaecb9-release zrlog-1.7.1-baaecb9-release.war
[[email protected] webapps]# mv zrlog-1.7.1-baaecb9-release 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
打开外部浏览器查看:http://192.168.72.130/zrlog/
e. 创建数据库
[[email protected] webapps]# ps aux | grep mysql
root 6826 0.0 0.0 112680 972 pts/1 S+ 15:31 0:00 grep --color=auto mysql
//没有开启mysql服务,开启
[[email protected] webapps]# service mysqld start
Starting MySQL.. SUCCESS!
[[email protected] webapps]# mysql -uroot -pxavilinux
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 1
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> create database zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on zrlog.* to ‘zrlog‘@127.0.0.1 identified by ‘xavilinux1‘;
Query OK, 0 rows affected (0.00 sec)
- 测试创建的用户是否正确:
[[email protected] webapps]# mysql -uzrlog -h127.0.0.1 -pxavilinux1
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 2
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)
f. 完成网页设置向导
- 进入后台
三、从新建的虚拟主机中访问
增加一个www.dsf.com的java站点。
docBase=”/data/wwwroot/www.dsf.com/”
这个就和咱们之前配置LAMP和LNMP差不多了。就是把站点的配置文件上传到此处。 有时候配置完毕,再次访问站点会出现404,那么几乎都是这个地方没有定义对,要么就是appBASE也上传了站点文件。(如果不知道如何解压war包,我们可以先把站点的包放到appBASE下,然后等到自动解压完毕,我们再次move站点的包到自定义的位置。)
3.1 创建新的文件夹,也是docBase的地址
[[email protected] webapps]# mkdir /data/wwwroot/www.dsf.com/
3.2 把刚才webapps下的zrlog文件都复制到docbase下
[[email protected] webapps]# mv /usr/local/tomcat/webapps/zrlog/ //这里双击tab建看下有哪些目录文件
admin/ error/ include/ META-INF/
assets/ favicon.ico install/ WEB-INF/
[[email protected] webapps]# mv /usr/local/tomcat/webapps/zrlog/* /data/wwwroot/www.dsf.com/
3.3 绑定hosts,记得关闭保存
- 看是否能ping通
直接访问
3.5 重新配置后,要重启下java服务
[[email protected] zrlog]# cd /data/wwwroot/www.dsf.com/
[[email protected] www.dsf.com]# ls
admin favicon.ico log sim.pid zrlog-1.7.1-baaecb9-release
assets include logs temp
error install META-INF WEB-INF
[[email protected] www.dsf.com]# rm -rf zrlog-1.7.1-baaecb9-release
[[email protected] www.dsf.com]# ls
admin error include log META-INF temp
assets favicon.ico install logs sim.pid WEB-INF
[[email protected] ~]# /usr/local/tomcat/bin/shutdown.sh
[[email protected] ~]# /usr/local/tomcat/bin/startup.sh
[[email protected] webapps]# netstat -lntp |grep 80
tcp6 0 0 :::80 :::* LISTEN 8287/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 8287/java
tcp6 0 0 :::8009 :::* LISTEN 8287/java
3.5 网站的图标和一些重要信息都在/usr/local/tomcat/webapps/ROOT下
[[email protected] www.dsf.com]# cd /usr/local/tomcat/webapps/
[[email protected] webapps]# ls
docs log ROOT zrlog-1.7.1-baaecb9-release
examples logs temp zrlog-1.7.1-baaecb9-release.war
host-manager manager zrlog
[[email protected] webapps]# ls ROOT
asf-logo-wide.svg bg-nav.png RELEASE-NOTES.txt tomcat-power.gif
bg-button.png bg-upper.png tomcat.css tomcat.svg
bg-middle.png favicon.ico tomcat.gif WEB-INF
bg-nav-item.png index.jsp tomcat.png
四、Tomcat日志
在日常运维中,Tomcat用的还是蛮多的,但是一旦出现问题,我们就需要去解决,思路就来自日志文件。
4.1 日志目录位置
[[email protected] webapps]# ls /usr/local/tomcat/logs
catalina.2018-03-31.log localhost.2018-03-31.log
catalina.out localhost_access_log.2018-03-31.txt
host-manager.2018-03-31.log manager.2018-03-31.log
其中catalina开头的日志为Tomcat的综合日志,它记录Tomcat服务相关信息,也会记录错误日志。
其中catalina.2017-xx-xx.log和catalina.out内容相同,前者会每天生成一个新的日志。
host-manager和manager为管理相关的日志,其中host-manager为虚拟主机的管理日志。
localhost和localhost_access为虚拟主机相关日志,其中带access字样的日志为访问日志,不带access字样的为默认虚拟主机的错误日志。
访问日志默认不会生成,需要在server.xml中配置一下。
4.2 给刚才新建的虚拟主机配置日志
在对应虚拟主机的<Host></Host>里面加入下面的配置(刚才的域名是www.dsf.com):
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="dsf.com_access" suffix=".log"
pattern="%h %l %u %t "%r" %s %b" />
prefix 定义访问日志的前缀;
suffix 定义日志的后缀;
pattern 定义日志格式。
新增加的虚拟主机默认并不会生成类似默认虚拟主机的那个localhost.日期.log日志; 错误日志会统一记录到catalina.out中。
关于Tomcat日志,最需要关注catalina.out,当出现问题时,我们应该第一想到去查看它。
以上是关于配置Tomcat架构的主要内容,如果未能解决你的问题,请参考以下文章
在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途