- 16.4 配置Tomcat监听80端口
- 16.5/16.6/16.7 配置Tomcat虚拟主机
- 16.8 Tomcat日志
- 扩展
- 邱李的tomcat文档 https://www.linuser.com/forum.php?mod=forumdisplay&fid=37
- JAR、WAR包区别 http://blog.csdn.net/lishehe/article/details/41607725
- tomcat常见配置汇总 http://blog.sina.com.cn/s/blog_4ab26bdd0100gwpk.html
- resin安装 http://fangniuwa.blog.51cto.com/10209030/1763488/
16.4 配置Tomcat监听80端口
- tomcat监听的是8080端口
- 如果想直接访问,使用默认的web服务,需要改动端口为80,如果不是80端口那么访问页面的时候需要指定端口才能访问,tomcat也是支持自定义端口的,比如把8080改为80端口
- 编辑配置文件vim /usr/local/tomcat/conf/server.xml, 搜索8080
[[email protected] src]# vim /usr/local/tomcat/conf/server.xml <!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> --> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL/TLS HTTP/1.1 Connector on port 8080 --> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 This connector uses the NIO implementation with the JSSE engine. When 67,58 39%
- 把这个位置改为80
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
- 改完之后重启服务,tomcat不支持restart,只能先shutdown 再startup
[[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.8Using CLASSPATH: /usr/local/tomcat/bin/boot strap.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/boot strap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [[email protected] src]#
- 进程是有的,端口还么有出现
[[email protected] src]# !netnetstat -lntp |grep java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 3433/java tcp6 0 0 :::8009 :::* LISTEN 3433/java [[email protected] src]# ps aux |grep javaroot 3433 6.9 7.9 2267320 79480 pts/0 Sl 21:28 0:07 /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 -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 3455 0.0 0.0 112680 976 pts/0 R+ 21:30 0:00 grep --color=auto java [[email protected] src]#
- 来搜下到底有没有80,
[[email protected] src]# netstat -lntp |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1968/nginx: master tcp6 0 0 127.0.0.1:8005 :::* LISTEN 3433/java tcp6 0 0 :::8009 :::* LISTEN 3433/java [[email protected] src]#
- 发现有80 但是是nginx ,nginx 占用着80端口,如果nginx占用了,tomcat就不能用,必须停掉nginux
[[email protected] src]# /etc/init.d/nginx stop Stopping nginx (via systemctl): [ 确定 ] [[email protected] src]# [[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.shUsing CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [[email protected] src]#
- 再来看下,80端口 tomcat 出来了,
[[email protected] src]# !net netstat -lntp |grep 80tcp6 0 0 :::80 :::* LISTEN 3543/java tcp6 0 0 :::8009 :::* LISTEN 3543/java [[email protected] src]#
- 8005 属于管理的端口需要过一会儿,会稍微慢一点
[[email protected] src]# !netnetstat -lntp |grep 80 tcp6 0 0 :::80 :::* LISTEN 3543/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 3543/java tcp6 0 0 :::8009 :::* LISTEN 3543/java [[email protected] src]#
- 已经启动了,再去浏览器那里访问下 直接访问ip地址,不需要加端口
16.5 配置Tomcat的虚拟主机(上)
- 在之前的学习,Apache还是nginx都属于webServer,都支持虚拟主机配置文件,虚拟主机配置文件一台服务器上或者一个webserver上,支持一个IP下可以访问多个域名,美国域名代表一个网站,所以可以配置多个网站,每个网站都称之为一个虚拟主机,tomcat同样也支持这个概念
- 开启虚拟机
- 编辑配置文件vim /usr/local/tomcat/conf/server.xml
- 搜索 Host
- tomcat的配置文件格式为 xml,语法是“<>开头,</>结尾” 虚拟主机配置文件内容 其中<Host>和</Host>之间的配置为虚拟主机配置部分,name定义域名, appBase定义应用的目录,Java的应用通常是一个jar的压缩包,你只需要将jar的压缩包放到appBase目录下面即可。刚刚阿铭访问的Tomcat默认页其实就是在appBase目录下面,不过是在它子目录ROOT里。
- 下面进入这个配置文件server.xml,搜索host
<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> </Engine> </Service></Server> 162,1 底端
- 添加如下内容
<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> <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> </Engine>:wq [[email protected] ~]# vim /usr/local/tomcat/conf/server.xml [[email protected] ~]#
16.5 配置Tomcat的虚拟主机(中)
- docBase,这个参数用来定义网站的文件存放路径,如果不定义,默认是在appBase/ROOT下面,定义了docBase就以该目录为主了,其中appBase和docBase可以一样。在这一步操作过程中很多同学遇到过访问404的问题,其实就是docBase没有定义对。
- appBase为应用存放目录,通常是需要把war包直接放到该目录下面,它会自动解压成一个程序目录
- 下面我们通过部署一个java的应用来体会appBase和docBase目录的作用
- 下载zrlog wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war
[[email protected] src]# wget --2017-11-06 21:21:07-- 正在解析主机 dl.zrlog.com (dl.zrlog.com)... 183.158.35.59 正在连接 dl.zrlog.com (dl.zrlog.com)|183.158.35.59|:80... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK长度:7122027 (6.8M) [application/zip] 正在保存至: “zrlog-1.7.1-baaecb9-release.war”100%[===================================================>] 7,122,027 2.21MB/s 用时 3.1s 2017-11-06 21:21:11 (2.21 MB/s) - 已保存 “zrlog-1.7.1-baaecb9-release.war” [7122027/7122027]) [[email protected] ~]# du -sh zrlog-1.7.1-baaecb9-release.war 6.8M zrlog-1.7.1-baaecb9-release.war
- appBase为应用存放目录(实际上是一个相对路径,相对于 /usr/local/tomcat/ 路径),通常是需要把war包直接放到该目录下面,它会自动解压成一个程序目录 为了方便测试,下载一个zrlog(java写的blog站点应用,轻量),进行测试
- appbase自动解压war包
[[email protected] ~]# ls /usr/local/tomcat/webapps/ docs examples host-manager manager ROOT [[email protected] ~]# [email protected] ~]# [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# cp zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/ [[email protected] ~]# !lsls /usr/local/tomcat/webapps/ docs host-manager ROOT zrlog-1.7.1-baaecb9-release.war examples manager zrlog-1.7.1-baaecb9-release [[email protected] ~]#
- 把root下的 zrlog文件移到src目录下
[[email protected] ~]# ls aming.txt anaconda-ks.cfg zrlog-1.7.1-baaecb9-release.war [[email protected] ~]# mv zrlog-1.7.1-baaecb9-release.war /usr/local/src [[email protected] ~]# ls aming.txt anaconda-ks.cfg [[email protected] ~]# cd /usr/local/src [[email protected] src]# rm -f /usr/local/tomcat/webapps/zrlog-1.7.1-baaecb9-release.war [[email protected] src]# ls apache-tomcat-8.5.8.tar.gz nginx-1.12.1apr-1.6.2 nginx-1.12.1.tar.gz apr-1.6.2.tar.gz php-5.6.30apr-util-1.6.0 php-5.6.30.tar.gz apr-util-1.6.0.tar.gz php-7.1.6httpd-2.4.27 php-7.1.6.tar.bz2 httpd-2.4.27.tar.gz phpredis-develop jdk-8u101-linux-x64.tar.gz phpredis-develop.zip logs zrlog-1.7.1-baaecb9-release.war mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz [[email protected] src]#
- 刚刚把webapps目录下的 zrlog 删掉了,解压的目录页跟着没了,其实我们只是想删包,留着目录
[[email protected] src]# cd /usr/local/tomcat/webapps/ [[email protected] webapps]# lsdocs examples host-manager manager ROOT [[email protected] webapps]#
- 再把它拷贝回来,再改下名字
[[email protected] webapps]# cp /usr/local/src/zrlog-1.7.1-baaecb9-release.war . [[email protected] webapps]# ls docs host-manager ROOT zrlog-1.7.1-baaecb9-release.war examples manager zrlog-1.7.1-baaecb9-release [[email protected] webapps]# [[email protected] webapps]# mv zrlog-1.7.1-baaecb9-release zrlog [[email protected] webapps]# ls docs host-manager ROOT zrlog-1.7.1-baaecb9-release examples manager zrlog zrlog-1.7.1-baaecb9-release.war [[email protected] webapps]#
- 发现改完名字了,又自动生成一个 解压文件了,只要war包存在,过会儿 又会自动生成一个解压的文件
- 现在要访问这个zrlog ,怎么访问呢,现在 就可以登录站点,尝试访问了
- 会出来一个安装向导,实际上就是让我们去配置数据库
- 之前也讲过怎么去创建一个用户,创建一个库
- 先来检查下mysql服务有没有启动
[[email protected] webapps]# ps aux |grep mysql root 2126 0.0 0.1 115392 1616 ? S 18:36 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/aming-01.pid mysql 2294 0.0 45.5 1300800 455660 ? Sl 18:36 0:10 /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/aming-01.err --pid-file=/data/mysql/aming-01.pid --socket=/tmp/mysql.sock root 4140 0.0 0.0 112684 980 pts/1 R+ 22:03 0:00 grep --color=auto mysql [[email protected] webapps]#
- 有启动的话就进mysql
[[email protected] webapps]# mysql -uroot -paminglinux 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 1Server 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>
- 创建数据库
mysql> create database zrlog;Query OK, 1 row affected (0.00 sec)mysql>
- 再创建一个用户
mysql> grant all on zrlog.* to ‘zrlog‘@127.0.0.1 identified by ‘aminglinux‘; Query OK, 0 rows affected (0.05 sec)mysql> mysql> mysql> quitBye
- 使用这个新创建的用户登录mysql,查看数据库,只要有zrlog就ok
[[email protected] webapps]# mysql -uzrlog -h127.0.0.1 -paminglinux 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 2Server 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 itsaffiliates. 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.03 sec) mysql>
- 再浏览器里面填写信息
- 填写信息
- 点击完成查看
- 可以去后台发些文章
- 这个时候就可以保存,查看了文章已经发表出来了
16.7 配置Tomcat虚拟主机(下)
- 打开配置文件
[[email protected] webapps]# 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> <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> </Engine> </Service></Server>"/usr/local/tomcat/conf/server.xml" 168L, 7527C 165,0-1 底端
- 测试用docBase指定路径,访问是否生效,前面做了新增虚拟机配置,自然就需要生成对应的目录
- mkdir /data/wwwroot/123.cn/
- 将之前的zrlog的文件移动到当前目录下
[[email protected] webapps]# vim /usr/local/tomcat/conf/server.xml [[email protected] webapps]# mkdir /data/wwwroot/123.cn [[email protected] webapps]# mv /usr/local/tomcat/webapps/zrlog/admin/ error/ include/ META-INF/ assets/ favicon.ico install/ WEB-INF/ [[email protected] webapps]# mv /usr/local/tomcat/webapps/zrlog/* /data/wwwroot/123.cn/ [[email protected] webapps]# [[email protected] webapps]# [[email protected] webapps]#
- 用windows测试下
- 重启服务,之前修改了配置文件,新增了虚拟主机,但并没有重启,所以没有及时生效
[[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.8Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [[email protected] webapps]# [[email protected] webapps]# netstat -lntp |grep 80 tcp6 0 0 :::80 :::* LISTEN 4322/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 4322/java tcp6 0 0 :::8009 :::* LISTEN 4322/java [[email protected] webapps]# [[email protected] webapps]# pwd/usr/local/tomcat/webapps [[email protected] webapps]#
- 把www.123.cn 绑定windows hosts
- 再去浏览器访问www.123.cn
16.8 Tomcat日志
- ls /usr/local/tomcat/logs
- 其中catalina开头的日志为Tomcat的综合日志,它记录Tomcat服务相关信息,也会记录错误日志。
- 其中catalina.2017-xx-xx.log和catalina.out内容相同,前者会每天生成一个新的日志。
- host-manager和manager为管理相关的日志,其中host-manager为虚拟主机的管理日志。
- localhost和localhost_access为虚拟主机相关日志,其中带access字样的日志为访问日志,不带access,字样的为默认虚拟主机的错误日志。
- 访问日志默认不会生成,需要在server.xml中配置一下。
[[email protected] webapps]# pwd /usr/local/tomcat/webapps [[email protected] webapps]# cd .. [[email protected] tomcat]# ls logs catalina.2017-11-05.log host-manager.2017-11-05.log localhost.2017-11-06.log manager.2017-11-05.log catalina.2017-11-06.log host-manager.2017-11-06.log localhost_access_log.2017-11-05.txt manager.2017-11-06.log catalina.out localhost.2017-11-05.log localhost_access_log.2017-11-06.txt [[email protected] tomcat]# [[email protected] tomcat]# cd logs [[email protected] logs]# less catalina.out [[email protected] logs]# [[email protected] logs]# less catalina.2017-11-06.log [[email protected] logs]#
- catalina.out这是一个综合日志
- 具体方法是在对应虚拟主机的<Host></Host>里面加入下面的配置(假如域名为123.cn):
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="123.cn_access" suffix=".log" pattern="%h %l %u %t "%r" %s %b" />
- prefix定义访问日志的前缀,suffix定义日志的后缀,pattern定义日志格式。新增加的虚拟主机默认并不会生成类似默认虚拟主机的那个localhost.日期.log日志,错误日志会统一记录到catalina.out中。关于Tomcat日志,你最需要关注catalina.out,当出现问题时,我们应该第一想到去查看它。
- 扩展
- 邱李的tomcat文档 https://www.linuser.com/forum.php?mod=forumdisplay&fid=37
- JAR、WAR包区别 http://blog.csdn.net/lishehe/article/details/41607725
- tomcat常见配置汇总 http://blog.sina.com.cn/s/blog_4ab26bdd0100gwpk.html
- resin安装 http://fangniuwa.blog.51cto.com/10209030/1763488/
时间: 2024-10-12 17:00:52