标签:Tomcat
16.4 配置Tomcat监听80端口变更监听端口:
编辑server.xml,将Connector port="8080" protocol="HTTP/1.1"修改为Connector port="80" protocol="HTTP/1.1"后,重启tomcat服务
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml
[root@localhost ~]# /usr/local/tomcat/bin/shutdown.sh ##重启tomcat 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@localhost ~]# /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 [root@localhost ~]# netstat -lntp |grep java ##80端口java没有看到 tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1322/java tcp6 0 0 :::8009 :::* LISTEN 1322/java [root@localhost ~]# netstat -lntp |grep 80 ##80端口被nginx占用了 tcp 0 0 0.0.0.0:43980 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 854/nginx: master p tcp6 0 0 :::43807 :::* LISTEN - tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1322/java tcp6 0 0 :::8009 :::* LISTEN 1322/java [root@localhost ~]# /etc/init.d/nginx stop Stopping nginx (via systemctl): [ 确定 ] [root@localhost ~]# /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@localhost ~]# /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@localhost ~]# netstat -lntp |grep java tcp6 0 0 :::80 :::* LISTEN 1425/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1425/java tcp6 0 0 :::8009 :::* LISTEN 1425/java
因为默认web服务访问的是80端口,此时再访问 http://192.168.65.128/ 就不用跟端口号了
配置虚拟主机:
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml
找到Host,就是虚拟主机
<Host>和</Host>之间的配置为虚拟主机配置部分
name 域名
appBase=" " 定义应用的目录,Java的应用通常是一个war的压缩包
unpackWARs="true" 是否自动解压WAR包,拷贝WAR包到webapps目录中会自动解压
autoDeploy="true"
添加虚拟主机:
<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类似,appBase是把jsp文件放在WAR包中,而如果不想WAR包的存在,不定义appBase,那么可以定义一个目录,目录里面放网站的程序,其实就相当于WAR包解压完的文件放到里面,而用docBase来定义目录位置
注意:appBase和docBase只能选择一个,另一个留空即可
·docBase,这个参数用来定义网站的文件存放路径,如果不定义,默认是在appBase/ROOT下面,定义了docBase就以该目录为主了,其中appBase和docBase可以一样。在这一步操作过程中很多同学遇到过访问404的问题,其实就是docBase没有定义对。
·appBase为应用存放目录,通常是需要把war包直接放到该目录下面,它会自动解压成一个程序目录
·下面我们通过部署一个java的应用来体会appBase和docBase目录的作用
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war [root@localhost src]# mv zrlog-1.7.1-baaecb9-release.war /usr/local/src/ [root@localhost src]# ls apache-tomcat-8.5.29.tar.gz jdk-8u161-linux-x64.tar.gz nginx-1.12.2.tar.gz zrlog-1.7.1-baaecb9-release.war httpd-2.2.34 mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz php-5.6.30 httpd-2.2.34.tar.gz nginx-1.12.2 php-5.6.30.tar.bz2 [root@localhost src]# cp zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/ [root@localhost src]# ls /usr/local/tomcat/webapps/ docs examples host-manager manager ROOT zrlog-1.7.1-baaecb9-release zrlog-1.7.1-baaecb9-release.war [root@localhost src]# rm -rf /usr/local/tomcat/webapps/zrlog-1.7.1-baaecb9-release.war [root@localhost src]# !ls ls /usr/local/tomcat/webapps/ docs examples host-manager manager ROOT zrlog-1.7.1-baaecb9-release [root@localhost src]# mv /usr/local/tomcat/webapps/zrlog-1.7.1-baaecb9-release.war /tmp/ ##移走war包 [root@localhost webapps]# mv zrlog-1.7.1-baaecb9-release zrlog ##改名 [root@localhost webapps]# ls docs examples host-manager manager ROOT zrlog
·浏览器中登录192.168.65.128/zrlog会出现安装向导
·需要在mysql中创建用户和数据库
[root@localhost webapps]# mysql -uroot -p123456 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.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> create database zrlog; Query OK, 1 row affected (0.00 sec) mysql> grant all on zrlog.* to 'zrlog'@127.0.0.1 identified by '123456'; Query OK, 0 rows affected (0.01 sec) mysql> quit Bye [root@localhost webapps]# mysql -uzrlog -h127.0.0.1 -p123456; 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.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.00 sec)
填入配置的信息:
下一步
下一步完成安装
之后就可以进入后台,写文章并进行管理了
之前访问zrlog博客是在192,168,65,128/zrlog,zrlog目录下访问,如果不想域名后有目录,可以按照以下步骤做:
1.在server.xml中新增配置虚拟主机:
[root@localhost webapps]# vim /usr/local/tomcat/conf/server.xml
2.重启tomcat服务:
[root@localhost 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@localhost 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.
3.创建网站目录,移动zrlog目录内容到123.cn中
[root@localhost webapps]# mkdir /data/wwwroot/123.cn [root@localhost webapps]# mv /usr/local/tomcat/webapps/zrlog/* /data/wwwroot/123.cn/
4.在hosts中重定向
5.此时访问www.123.cn,就可以看到之前的博客内容了
不用再进入目录才能看到博客内容
·日志目录:
[root@localhost tomcat]# ls logs/
·其中catalina开头的日志为Tomcat的综合日志,它记录Tomcat服务相关信息,也会记录错误日志。
·其中catalina.2017-xx-xx.log和catalina.out内容相同,前者会每天生成一个新的日志。
·host-manager和manager为管理相关的日志,其中host-manager为虚拟主机的管理日志。
·localhost和localhost_access为虚拟主机相关日志,其中带access字样的日志为访问日志,不带·access字样的为默认虚拟主机的错误日志。
[root@localhost tomcat]# cat logs/catalina.out
·访问日志默认不会生成,需要在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 "%r" %s %b" />
·prefix定义访问日志的前缀,suffix定义日志的后缀,pattern定义日志格式。
新增加的虚拟主机默认并不会生成类似默认虚拟主机的那个localhost.日期.log日志,错误日志会统一记录到catalina.out中。关于Tomcat日志,你最需要关注catalina.out,当出现问题时,我们应该第一想到去查看它。
2018-3-30 14周5次课 配置Tomcat监听端口、虚拟主机、日志
标签:Tomcat
原文地址:http://blog.51cto.com/11530642/2093216