Tomcat 虚拟主机实现多站点与热部署
Tomcat部署有两种,要么是war包,要么是jar包。这两种方式都可以。目前war包用的最多。很多时候线上将代码写好编译完了一输出就输出了一个war包。然后开发给你这个war包要你上线。对于我们来说只需要丢到站点目录下面就行了
[root@localhost ~]# ls
anaconda-ks.cfg zrlog.war
这里准备好了一个zrlog war包,我们将不破坏默认的站点,再给其添加一个新的站点,新添加站点只需要关注host这几行,默认域名是localhost,这里我们需要修改域名。
- appBase改为站点目录/code
- unpackWARs="true" 是否自动解压war包
- autoDeploy="true" 自动部署,回头你只要往我的站点目录下/code丢一个war包,不需要重启tomcat,自动帮你解压,然后就可以访问了
- 记录access log,这个日志默认放在/usr/local/tomcat/logs/目录下,这个我们不需要修改。我们需要修改日志的前缀来区分不同的网站,按照域名来区分。
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
再往下面是日志的格式pattern="%h %l %u %t "%r" %s %b" />
现在就相当于新增加了一个站点
</Host>
<Host name="blog.stack.com.cn" appBase="/code"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="blog_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
现在创建站点目录
[root@localhost ~]# mkdir -p /code
现在将war包放进来就行,不需要自己解压,等会tomcat会帮你解压
[root@localhost code]# cp /root/zrlog.war .
[root@localhost code]# ls
zrlog.war
第一个站点目录war包必须叫ROOT.War,比如这个目录下面只有一个站点,那么一定要有ROOT的war包,或者一个ROOT目录,然后这个ROOT下面再放文件。如果你要定义其他的location那么就再写上其他目录。
[root@localhost code]# mv zrlog.war ROOT.war
[root@localhost code]# ls
ROOT.war
站点目录和war包准备好了,server.xml也修改了。因为server.xml修改了所以需要重启tomcat生效(先关闭再启动)
[root@localhost code]# /usr/local/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
..............................................................................................
[root@localhost code]# /usr/local/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
................................................................................................
看看启动是否成功
#日志如下:
10-Oct-2020 10:14:15.262 INFO [blog.stack.com.cn-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /code/ROOT.war
10-Oct-2020 10:14:20.299 INFO [blog.stack.com.cn-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
WARN 2020-10-10 10:14:20,642 - Not found lock file(/code/ROOT/WEB-INF/install.lock), Please visit the http://yourHostName:port/install start installation
10-Oct-2020 10:14:21.557 INFO [blog.stack.com.cn-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /code/ROOT.war has finished in 6,282 ms
10-Oct-2020 10:14:21.590 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
10-Oct-2020 10:14:21.635 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
10-Oct-2020 10:14:21.664 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 10208 ms
[root@localhost code]# netstat -tpln | grep 80
tcp6 0 0 :::8080 :::* LISTEN 1387/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1387/java
tcp6 0 0 :::8009 :::* LISTEN 1387/java
[root@localhost code]# ls
ROOT ROOT.war
接下来访问需要域名访问 C:\Windows\System32\drivers\etc\hosts
192.168.179.102 blog.stack.com.cn
查看日志可以看到
[root@localhost ~]# tail -f //usr/local/tomcat/logs/blog_access_log.2020-09-30.txt
192.168.179.4 - - [30/Sep/2020:12:03:46 +0800] "GET /install HTTP/1.1" 200 6288
192.168.179.4 - - [30/Sep/2020:12:03:46 +0800] "GET /assets/css/bootstrap.min.css HTTP/1.1" 200 140884
可以看到多站点配置成功
[root@localhost ~]# yum install mariadb-server -y
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
MariaDB [(none)]> create database zrlog;
MariaDB [(none)]> grant all on zrlog.* to zrlog@'%' identified by 'zrlog';
到这一步可以看到连接到数据库了,最后这样
热部署
热部署和热加载是类似的,都是在不重启Tomcat的情况下,使得应用的最新代码生效。
- 热部署表示重新部署应用,它的执行主体是Host,表示主机。
- 热加载表示重新加载class,它的执行主体是Context,表示应用。
我这里没有zrlog的新版本的war包,我这里假定jforum-2.5.0.war为zrlog的新版本,这个时候你需要更新之前部署的网站了,那么需要热部署了,那么只需要将jforum-2.5.0.war重命名为ROOT.war并且拷贝到网站发布目录下tomcat会热部署
[root@localhost code]# cp /root/jforum-2.5.0.war ROOT.war
cp: overwrite ?.OOT.war?. yes
[root@localhost code]# ls
ROOT ROOT.war
部署完之后再来看看tomcat日志
30-Sep-2020 14:11:47.261 INFO [blog.stack.com.cn-startStop-5] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
30-Sep-2020 14:11:48.076 INFO [blog.stack.com.cn-startStop-5] org.apache.catalina.core.ApplicationContext.log
*****************************************************
* Owasp.CsrfGuard Properties
*
* Logger: net.jforum.csrf.CsrfLogger
* NewTokenLandingPage: /index.jsp
* PRNG: SHA1PRNG
* SessionKey: OWASP_CSRFTOKEN
* TokenLength: 32
* TokenName: OWASP_CSRFTOKEN
* Ajax: true
* Rotate: false
* Javascript cache control: null
* Javascript domain strict: false
* Javascript inject attributes: false
* Javascript inject forms: false
* Javascript referer pattern: null
* Javascript referer match domain: false
* Javascript source file: null
* Javascript X requested with: null
* Protected methods: Empty HashSet
* Protected pages size: 0
* Unprotected methods: Empty HashSet
* Unprotected pages size: 91
* TokenPerPage: false
* Enabled: true
* ValidateWhenNoSessionExists: true
* Action: org.owasp.csrfguard.action.Log
* Parameter: Message = potential cross-site request forgery (CSRF) attack thwarted (user:%user%, ip:%remote_ip%, method:%request_method%, uri:%request_uri%, error:%exception_message%)
* Action: org.owasp.csrfguard.action.Rotate
* Action: org.owasp.csrfguard.action.Redirect
* Parameter: Page = /error.html
*****************************************************
30-Sep-2020 14:11:48.089 INFO [blog.stack.com.cn-startStop-5] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /code/ROOT.war has finished in 49,563 ms
可以看到热部署成功
目录 返回
首页