Windows server adds multiple tomcats to the service

Confusion of Concept Areas

First of all, it is necessary to declare that the relationship between apache and TOMCAT is simple to understand, apache is a static server container, and tomcat is a java environment service container.

 

Tomcat startup analysis

Open the bin directory of the downloaded tomcat

Open the batch file startup.bat used to start Tomcat with a text editor and read it carefully. In this file, first determine whether the CATALINA_HOME environment variable is empty, if it is empty, set the current directory to the value of CATALINA_HOME. Then judge whether bin\catalina.bat exists in the current directory. If the file does not exist, set the parent directory of the current directory to the value of CATALINA_HOME. According to the hierarchical structure of the Tomcat installation directory on the author's machine, the value of CATALINA_HOME is finally set to the Tomcat installation directory. If the environment variable CATALINA_HOME already exists, call the "catalina.bat start" command in the bin directory through this environment variable. Through this analysis, we learned two pieces of information. First, when Tomcat starts, you need to find the environment variable CATALINA_HOME. If you call startup.bat in Tomcat's bin directory, Tomcat will automatically and correctly set CATALINA_HOME; the second is to execute startup. The bat command actually executes the "catalina.bat start" command.

 

If we do not call startup.bat when Tomcat's bin directory is the current directory, the error message shown in the following figure will appear (except when it is called in the parent directory of the bin directory).

 

If you want to start Tomcat in any directory, you need to set the CATALINA_HOME environment variable. You can add CATALINA_HOME to the environment variable of the Windows XP system, and its value is the installation directory of Tomcat. On the author's machine, the installation directory of Tomcat is C:\apache\tomcat1. The process of adding the CATALINA_HOME environment variable is the same as the previous process of adding the JAVA_HOME environment variable. If you don't want to add it in the system's environment variables, you can also set it directly in the startup.bat file. Here is the file snippet after setting CATALINA_HOME in the startup.bat file:

 

rem Unless required by applicable law or agreed to in writing, software

rem distributed under the License is distributed on an "AS IS" BASIS,

rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

rem See the License for the specific language governing permissions and

rem limitations under the License.

rem ---------------------------------------------------------------------------

rem Start script for the CATALINA Server

rem ---------------------------------------------------------------------------

setlocal

 

set

JAVA_HOME=C:\Program Files\Java\jdk1.7.0_51

set

CATALINA_HOME=C:\apache\tomcat1

 

rem Guess CATALINA_HOME if not defined

set "CURRENT_DIR=%cd%"

if not "%CATALINA_HOME%" == "" goto gotHome

set "CATALINA_HOME=%CURRENT_DIR%"

if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome

cd ..

set "CATALINA_HOME=%cd%"

cd "%CURRENT_DIR%"

:gotHome

if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome

echo The CATALINA_HOME environment variable is not defined correctly

echo This environment variable is needed to run this program

goto end

:okHome

 

注意以粗体显示的这句代码的作用就是设置CATALINA_HOME环境变量,在它的下面是判断CATALINA_HOME是否为空的语句。如果找不准位置,干脆将设置CATALINA_HOME环境变量的这句代码放到文件的第一行。JAVA_HOME环境变量也可以采用同样的方式进行设置。不过,如果要在其他目录下利用shutdown.bat来关闭Tomcat服务器,则需要在shutdown.bat文件中设置CATALINA_HOME和JAVA_HOME这两个环境变量,设置变量的位置和startup.bat文件一样,都是在判断CATALINA_HOME是否为空之前。当然,为了一劳永逸,避免重装Tomcat后还要进行设置(需要是同一版本的Tomcat安装在同一位置),我们最好还是将CATALINA_HOME和JAVA_HOME这两个环境变量添加到Windows XP系统的环境变量中。

有的读者可能会对Tomcat安装目录的环境变量的名字是CATALINA_HOME而感到奇怪,按照其他环境变量的设置来看,JAVA_HOME表示JDK的安装目录,那么应该用TOMCAT_HOME来表示Tomcat的安装目录,可为什么要使用CATALINA_HOME呢?实际上,在Tomcat 4以前,用的就是TOMCAT_HOME来表示Tomcat的安装目录,在Tomcat 4以后,采用了新的Servlet容器Catalina,所以环境变量的名字也改为了CATALINA_HOME。

在Windows系统下环境变量的名字是与大小写无关的,也就是说,JAVA_HOME和java_home是一样的。

了解了startup.bat文件以后,我们再来看看真正负责启动Tomcat服务器的catalina.bat文件。通过分析catalina.bat文件,我们发现它还调用了一个文件setclasspath.bat。在setclasspath.bat文件中,它检查JAVA_HOME环境变量是否存在,并通过JAVA_HOME环境变量,找到java.exe,用于启动Tomcat。在这个文件中,还设置了其他的一些变量,代表调用Java的标准命令,有兴趣的读者可以自行分析一下这个文件。在执行完setclasspath.bat之后,catalina.bat剩下的部分就开始了Tomcat服务器的启动进程。

 

 

安装多TOMCAT服务操作步骤

1.下载Zip版Tomcat;

      选择:apache-tomcat-7.0.70 Windows zip(pgp,md5)下载解压文件到指定目录,如:D:/ProgramFiles/Tomcat6

进入D:/ProgramFiles/Tomcat6/bin目录:双击运行:startup.bat

待启动完成后,在浏览器中输入:http://localhost:8080

     如果出来tomcat首页,表示安装成功

    先关闭刚才启动的Tomcat,可以在刚才打开的面板中按Ctrl + C 结束运行

 

2修改startup.bat   在第一行前加入如下内容:

SET
JAVA_HOME=D:\ProgramFiles\tool\Java\jdk6

SET
CATALINA_HOME=D:\ProgramFiles\Tomcat6

JAVA_HOME就是环境变量中配置的内容.CATALINA_HOME就是Tomcat的目录

 

3.修改shutdown.bat

    在第一行前加入如下内容:

SET JAVA_HOME=D:\ProgramFiles\tool\Java\jdk6

SET
CATALINA_HOME=D:\ProgramFiles\Tomcat6

 

4.修改service.bat

  在第一行前加:

set
CATALINA_HOME=D:\ProgramFiles\Tocmat6

 

SET JAVA_HOME=D:\ProgramFiles\tool\Java\jdk6

 

找到配置文件中修改默认服务名

set SERVICE_NAME=【你想修改的服务名】

 

set DISPLAYNAME= Tomcat 7.0 %SERVICE_NAME%

 

第一行自不必说,第二行是服务的名字,在命令行中通过该名字进行服务的控制(启动/关闭)

第三行是服务的显示名称,即在服务管理器中显示的名称.

 

5.添加到服务

在DOS界面下,cd进入Tomcat解压目录的bin目录,

如果已经有tomcat6服务,则先输入命令:

service remove tomcat6

如果没有tomcat6服务 则直接输入:service.bat install  或 service.bat install tomcat6

如果安装成功,会提示:The service 'Tomcat6(或者你修改一后的SERVICE_NAME)' has
been installed  到你的启动服务里将会看到启动apache tomcat7 tomcat6这个服务。

 

注意:如上步骤可添加第多个tomcat服务,需注意的是tomcat配置文件端口不要和之前配置的tomcat端口产生冲突。

那么请到conf 目录下的 server.xml 配置文件,把那些端口全都修改了,比如 8080 改成 9080 或者你改成 8090,只要跟第一个其他端口不冲突就行,其他的还有8005 改成9005,8009 改成9009,修改完以后,保存关闭该文件,这样就可以在服务里启动多个tomcat了。

 

如果端口被占用,使用DOS命令查下端口号。以下为网上整理

1.输入命令:netstat -ano,列出所有端口的情况。在列表中我们观察被占用的端口。

2.查看被占用端口对应的PID,输入命令:netstat -aon|findstr "49157",回车,记下最后一位数字,即PID,这里是2720

3.继续输入tasklist|findstr "2720",回车,查看是哪个进程或者程序占用了2720端口,结果是:svchost.exe

4.结束该进程:在任务管理器中选中该进程点击”结束进程“按钮,或者是在cmd的命令窗口中输入:taskkill /f /t /im Tencentdl.exe。

 

以上方法我在自己电脑上试验,一切OK ,在服务器上试验,看到日志里报错,感觉好一阵郁闷,一时间也没有办法,错误日志如下:

[2016-12-14 22:06:23] [info]  [ 1936] Commons Daemon procrun (1.0.15.0 64-bit) started

[2016-12-14 22:06:24] [info]  [ 1936] Running 'Ipsearch' Service...

[2016-12-14 22:06:24] [info]  [ 1532] Starting service...

[2016-12-14 22:06:24] [error] [ 1532] %1 不是有效的 Win32 应用程序。

[2016-12-14 22:06:24] [error] [ 1532] Failed creating java C:\Java\jdk1.8\jre\bin\server\jvm.dll

[2016-12-14 22:06:24] [error] [ 1532] %1 不是有效的 Win32 应用程序。

[2016-12-14 22:06:24] [error] [ 1532] ServiceStart returned 1

[2016-12-14 22:06:24] [error] [ 1532] %1 不是有效的 Win32 应用程序。

[2016-12-14 22:06:25] [info]  [ 1936] Run service finished.

[2016-12-14 22:06:25] [info]  [ 1936] Commons Daemon procrun finished

 

怎么办呢,只能找度娘,各种答案啊,有怀疑是JDK的问题的,于是我把JDK换成了1.7版本。

依然如此,没有效果。有的说是把jdk\bin目录下的msvcr71.dll复制到tomcat安装目录的\bin下,我也试了没结果,冷静一下,想想这应该是一个很简单的配置,不应该如此麻烦。既然,JDK没有问题,端口也没有问题,我只能怀疑,安装到服务的方法不对了。于是我在startup.bat和shutdown.bat 只加了

SET "CATALINA_HOME=C:\Tomcat\tomcatIpsearch"   注意这里有引号。(有引号 没引号到底有没有关系我就没验证了),set JAVA_HOME 我删除掉了,因为环境变量里已经配置了。

service.bat文件里不添加任何内容,保持TOMCAT 初始的样子。

 

这里注册服务我缓存.bat文件执行,在桌面上添加一个

install.bat文件 文件里写上下面三行内容

set JAVA_HOME=C:\Program Files\java\jdk1.7.0_51
set CATALINA_HOME=C:\Tomcat\tomcatIpsearch
C:\Tomcat\tomcatIpsearch\bin\service.bat install web-Ipsearche

 在桌面上双击这个配置,执行完毕。

再打开服务看到有tomcat 7 web-Ipsearche这个服务。点击启动,竟然成功了。以此方法类推安装多个不同应用名的tomcat服务。

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326605789&siteId=291194637