部署到Tomcat
当我们打包好一个Springboot项目,如:springboot.war包,将war包放到tomcat的webapps目录之后,笔者的webapps路径为:/var/lib/tomcat8/webapps。重启tomcat。
sudo service tomcat8 start
重启tomcat之后,你就会发现webapps目录下多了一个springboot目录,其实就是tomcat解压了springboot.war。
Note: 如果重启之后没有看到springboot目录,那么可能是springboot.war权限不对,可以配置一些读写权限就可以了
访问测试
这时候,我们去浏览器上进行访问测试,输入URL,如:http://localhost:8080/springboot/test
需要在路径上加上你的项目名,笔者这里是加上了springboot,其实就是解压后的那个文件夹名
那么,如何才能避免在url上输入项目名来进行访问呢
修改配置文件server.xml
打开配置文件server.xml,笔者的路径是/etc/tomcat8/server.xml,找到标签,在标签下添加一个节点
<Context docBase="/var/lib/tomcat8/webapps/springboot" path="" reloadable="true" crossContext="true"/>
其中,docBase指向解压之后的项目目录的绝对路径,path为空即可
如下,为完整的的配置:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<Context docBase="/var/lib/tomcat8/webapps/springboot" path="" reloadable="true" crossContext="true"/>
<!--
<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>
保存修改之后,重启tomcat,再次到浏览器行进行测试:
http://localhost:8080/test
这时候,就不需要加上项目名了。