1. 安装JDK和tomcat,以及相关配置。


点击打开链接


2. 部署项目


i. 将需要发布的web项目打成war包(eclipse -> Export -> war)。

ii. 复制到 /usr/local/tomcat/webapps/ 中。

iii. 配置server.xml(如下<Context>位置,添加项目路径)。

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
	<Context path="" docBase="/usr/local/tomcat/webapps/webExample"></Context>

        <!-- 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>


3. 测试项目


i. 启动tomcat

ng@linux:/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.0_151
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
ii. 查看日志(tomcat是否成功启动以及排障)

ng@linux:/usr/local/tomcat$ cd logs
ng@linux:/usr/local/tomcat/logs$ cat catalina.out
07-Jan-2018 12:29:47.587 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.48
07-Jan-2018 12:29:47.594 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Nov 30 2017 16:26:50 UTC
07-Jan-2018 12:29:47.594 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.48.0
07-Jan-2018 12:29:47.594 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
07-Jan-2018 12:29:47.594 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.10.0-28-generic
07-Jan-2018 12:29:47.595 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
07-Jan-2018 12:29:47.595 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/jdk1.8.0_151/jre
07-Jan-2018 12:29:47.595 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_151-b12
07-Jan-2018 12:29:47.595 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
此处省略。。。。。。。。。。。
07-Jan-2018 16:14:10.937 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
07-Jan-2018 16:14:10.959 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
07-Jan-2018 16:14:10.979 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 11251 ms
启动成功,现在就可以用“localhost:8080/webExample”或者“本机ip:8080/webExample”访问自己的项目啦!

iii. 关掉tomcat

ng@linux:/usr/local/tomcat/logs$ cd ..
ng@linux:/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.0_151
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
ng@linux:/usr/local/tomcat$ 
同样可以查看日志,是否关闭成功。

ng@linux:/usr/local/tomcat$ cd logs
ng@linux:/usr/local/tomcat/logs$ cat catalina.out
07-Jan-2018 12:29:47.587 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.48
07-Jan-2018 12:29:47.594 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Nov 30 2017 16:26:50 UTC
此处省略。。。。。。。。。。。。。。
07-Jan-2018 16:25:03.042 信息 [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"]
07-Jan-2018 16:25:03.044 信息 [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"]
07-Jan-2018 16:25:03.046 信息 [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
07-Jan-2018 16:25:03.048 信息 [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]
关闭成功。

注意:命令“cat catalina.out”是将以往所运行过的(tomcat启动关闭)日志全部打印出来,可以根据每条信息前面的时间戳进行区分。


4. tomcat启动不成功的一些问题。


i. 端口占用(8080或8009)

07-Jan-2018 15:45:56.895 严重 [main] org.apache.coyote.AbstractProtocol.init Failed to initialize end point associated with ProtocolHandler ["ajp-nio-8009"]
 java.net.BindException: 地址已在使用
	at sun.nio.ch.Net.bind0(Native Method)
	at sun.nio.ch.Net.bind(Net.java:433)
	at sun.nio.ch.Net.bind(Net.java:425)
解决方法

这里是端口8009被占用,8080被占用处理方法同。

a. 将server.xml中该端口替换为其他端口(8888,9999,8010。。。)。

点击打开链接

b. 查出占用该端口的进程,杀进程。

ng@linux:/usr/local/tomcat/logs$ netstat -apn|grep 8009
(并非所有进程都能被检测到,所有非本用户的进程信息将不会显示,如果想看到所有信息,则必须切换到 root 用户)
tcp6       0      0 :::8009                 :::*                    LISTEN      23918/java  

ng@linux:/usr/local/tomcat/logs$ ps -aux|grep 23918
ng     23918  2.2 17.5 4020184 331876 ?      Sl   15:33   0:32 /usr/local/jdk1.8.0_151/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 -Dignore.endorsed.dirs= -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
ng     24393  0.0  0.0  21312   980 pts/4    S+   15:57   0:00 grep --color=auto 23918
ng@linux:/usr/local/tomcat/logs$ kill -9 23918
ng@linux:/usr/local/tomcat/logs$
ii. 80端口“权限不够”

07-Jan-2018 16:53:10.470 严重 [main] org.apache.coyote.AbstractProtocol.init Failed to initialize end point associated with ProtocolHandler ["http-nio-80"]
 java.net.SocketException: 权限不够
	at sun.nio.ch.Net.bind0(Native Method)
	at sun.nio.ch.Net.bind(Net.java:433)
	at sun.nio.ch.Net.bind(Net.java:425)
	at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
解决方法

点击打开链接

iii. 项目路径不全

07-Jan-2018 16:59:20.717 严重 [Catalina-startStop-1] org.apache.catalina.core.ContainerBase.startInternal A child container failed during start
 java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
	at java.util.concurrent.FutureTask.report(FutureTask.java:122)
	at java.util.concurrent.FutureTask.get(FutureTask.java:192)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
	at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
	... 6 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@17315fa8]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
	at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4968)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5101)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	... 6 more
Caused by: java.lang.IllegalArgumentException: The main resource set specified [/webExample] is not valid
	at org.apache.catalina.webresources.StandardRoot.createMainResourceSet(StandardRoot.java:746)
	at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:704)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	... 9 more

解决方法

修改server.xml项目路径

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
	<Context path="" docBase="/usr/local/tomcat/webapps/webExample"></Context>

        <!-- 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>

参考博客:

https://www.cnblogs.com/xdp-gacl/p/4097608.html

http://blog.csdn.net/zhuying_linux/article/details/6583096/

http://blog.csdn.net/reblue520/article/details/52586185

Logo

更多推荐