<div id="toc" class="toc-article">
<strong class="toc-title">文章目录</strong>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-Java-应用服务器市场份额"><span class="toc-text">1.Java 应用服务器市场份额</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-安装-Tomcat"><span class="toc-text">2 安装 Tomcat</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-下载与安装-9M"><span class="toc-text">2.1 下载与安装(9M)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-用户配置"><span class="toc-text">2.2 用户配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-配置-Tomcat"><span class="toc-text">2.3 配置 Tomcat</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-脚本"><span class="toc-text">2.4 脚本</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-5-日志"><span class="toc-text">2.5 日志</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-6-防火墙放行(直接对外服务时才需要)"><span class="toc-text">2.6 防火墙放行(直接对外服务时才需要)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-7-配置为-systemd-服务"><span class="toc-text">2.7 配置为 systemd 服务</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-8-部署-WEB-应用"><span class="toc-text">2.8 部署 WEB 应用</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-Tomcat-单机多实例"><span class="toc-text">3 Tomcat 单机多实例</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-关键点"><span class="toc-text">3.1 关键点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-准备各实例"><span class="toc-text">3.2 准备各实例</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-1-准备实例目录"><span class="toc-text">3.2.1 准备实例目录</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-2-替换各实例的监听端口"><span class="toc-text">3.2.2 替换各实例的监听端口</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-3-调整-JVM-参数"><span class="toc-text">3.2.3 调整 JVM 参数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-配置各实例的服务"><span class="toc-text">3.3 配置各实例的服务</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-1-创建多实例-service-文件"><span class="toc-text">3.3.1 创建多实例 service 文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-2-设置为开机自动运行"><span class="toc-text">3.3.2 设置为开机自动运行</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-实用脚本"><span class="toc-text">3.4 实用脚本</span></a></li></ol></li></ol>
</div>
<p>Tomcat 是最流行的 Java 应用服务器(2017年市场份额64%)。本文以最新的 Apache Tomcat 7.0.90(Tomcat7 的最新版本) 为例,总结了 CentOS 下 Tomcat 服务器的安装和必要配置,并演示了 Tomcat 单机多实例的部署方法。<br><a id="more"></a><br><strong>作者:</strong>王克锋<br><strong>出处:</strong><a href="https://kefeng.wang/2016/12/26/tomcat-installation/">https://kefeng.wang/2016/12/26/tomcat-installation/</a><br><strong>版权:</strong><a href="https://creativecommons.org/licenses/by-nc-nd/3.0/deed.zh" target="_blank" rel="noopener">自由转载-非商用-非衍生-保持署名</a>,转载请标明作者和出处。</p>
1.Java 应用服务器市场份额
google “site:plumbr.io Java application servers”
https://plumbr.io/blog/java/most-popular-java-application-servers-2017-edition
2017年市场份额: Tomcat 为 64%,JBoss 14%, Jetty 9%, Glassfish 6%, Weblogic 4%
2 安装 Tomcat
选择 Tomcat7 的原因:
- Tomcat9: 首个 beta 版本刚刚发布(2017-11-15),不稳定;
- Tomcat8.5: 不支持第三方组件 redis sessions manager;
- Tomcat8.0: 支持 redis session manager,但官方声明即将结束支持(2018年6月30日);
- Tomcat7.0: 支持 redis session manager,且无结束支持的声明。
2.1 下载与安装(9M)
1 2 3 4
| ## https://tomcat.apache.org/download-70.cgi ## http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.90/bin/apache-tomcat-7.0.90-windows-x64.zip wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.90/bin/apache-tomcat-7.0.90.tar.gz sudo tar -zxf apache-tomcat-7.0.90.tar.gz -C /opt
|
2.2 用户配置
1 2 3 4 5 6
| ## sudo vim /etc/profile ## 所有用户有效 export CATALINA_HOME=/opt/apache-tomcat-7.0.90 ## bin,lib export CATALINA_BASE=/opt/apache-tomcat-7.0.90 ## conf,logs,webapps,work,temp export PATH=$PATH:$CATALINA_HOME/bin
alias slg='sudo tail -f $CATALINA_BASE/logs/catalina.out'
|
2.3 配置 Tomcat
1 2 3 4 5 6 7 8 9 10
| #
1catalina.org.apache.juli.AsyncFileHandler.formatter = java.util.logging.SimpleFormatter 2localhost.org.apache.juli.AsyncFileHandler.formatter = java.util.logging.SimpleFormatter 3manager.org.apache.juli.AsyncFileHandler.formatter = java.util.logging.SimpleFormatter 4host-manager.org.apache.juli.AsyncFileHandler.formatter = org.apache.juli.OneLineFormatter
# java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.SimpleFormatter.format=%1$tT,%1$tL %4$s [%3$s] - %5$s%6$s%n
|
1 2 3 4 5
| sudo vim $CATALINA_BASE/conf/tomcat-users.xml ## 用户 # sudo vim $CATALINA_BASE/conf/server.xml ## 服务器【修改端口为 80/443】 sudo sed -i 's/8080/80/g' $CATALINA_BASE/conf/server.xml sudo sed -i 's/8443/443/g' $CATALINA_BASE/conf/server.xml sudo vim $CATALINA_BASE/conf/context.xml ## 站点
|
1 2 3 4 5
| # JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/./urandom" JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelOldGC -Xms512M -Xmx512M -Xmn256M -XX:SurvivorRatio=8 -XX:PermSize=64M -XX:MaxPermSize=128M" JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:$CATALINA_BASE/logs/gc.log" CATALINA_OUT=/dev/null
|
2.4 脚本
1 2 3 4 5 6 7
| sudo mkdir $CATALINA_HOME/bin/batch/ sudo mv $CATALINA_HOME/bin/*.bat $CATALINA_HOME/bin/batch/
sudo $CATALINA_HOME/bin/version.sh ## 查看系统版本、JDK、Tomcat 版本 sudo $CATALINA_HOME/bin/shutdown.sh ## 停止服务 sudo $CATALINA_HOME/bin/startup.sh ## 启动服务 #
|
2.5 日志
1 2 3 4 5 6
| sudo chmod 755 $CATALINA_BASE/logs sudo chmod 644 $CATALINA_BASE/logs/* sudo tail -f $CATALINA_BASE/logs/catalina.out sudo tail -f $CATALINA_BASE/logs/catalina.`date +%Y-%m-%d`.log sudo tail -f $CATALINA_BASE/logs/localhost.`date +%Y-%m-%d`.log sudo tail -f $CATALINA_BASE/logs/localhost_access_log.`date +%Y-%m-%d`.txt
|
2.6 防火墙放行(直接对外服务时才需要)
1 2 3 4
| ### sudo vim /etc/sysconfig/iptables ### 重启生效: sudo systemctl restart iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
|
2.7 配置为 systemd 服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ## sudo vim /usr/lib/systemd/system/tomcat.service [Unit] Description=Apache Tomcat After=syslog.target network.target
[Service] Type=forking Environment="CATALINA_HOME=/opt/apache-tomcat-7.0.90" Environment="CATALINA_BASE=/opt/apache-tomcat-7.0.90" ExecStart=/opt/apache-tomcat-7.0.90/bin/startup.sh ExecStop=/opt/apache-tomcat-7.0.90/bin/shutdown.sh
[Install] WantedBy=multi-user.target
|
1 2 3
| sudo systemctl daemon-reload sudo systemctl enable tomcat # 设置为开机自动启动 sudo systemctl start tomcat # 立即启动
|
2.8 部署 WEB 应用
1 2 3 4
| cd $CATALINA_BASE/webapps sudo rz -y ## site.war, 自动部署,无需重启 tomcat # sudo unzip -q site.zip -d $CATALINA_BASE/webapps/site ## curl http://localhost/site
|
3 Tomcat 单机多实例
与 MySQL 单机多实例类似,Tomcat 的单机多实例,也是指单个 Linux 的单个 Tomcat 安装,启动多个 Tomcat 服务进程,分别监听不同的端口,对外提供不同的 HTTP 服务。
3.1 关键点
Tomcat 有两个相关的环境变量:
- CATALINA_HOME: 是 Tomcat 的安装目录,所有实例共用一份,相关子目录是 bin|lib;
- CATALINA_BASE: 是 Tomcat 的工作目录,各个实例各自一份,相关子目录是 conf|webapps|work|logs|temp;
对于单实例,只需设置 CATALINA_HOME(此时认为 CATALINA_BASE 取相同值),多实例才必须设置 CATALINA_BASE。
3.2 准备各实例
3.2.1 准备实例目录
1 2 3 4 5 6 7
| sudo systemctl stop tomcat sudo systemctl disable tomcat sudo cp -R $CATALINA_HOME $CATALINA_HOME.bak
sudo mkdir -p $CATALINA_HOME@{1,2}/{logs,temp,webapps,work} sudo cp -R $CATALINA_HOME/{conf} $CATALINA_HOME@1/ sudo mv -f $CATALINA_HOME/{conf} $CATALINA_HOME@2/
|
3.2.2 替换各实例的监听端口
- HTTP 端口 8080 分别替换为 8081/8082
- HTTPS 端口 8443 分别替换为 8031/8032
- SHUTDOWN 端口 8005 分别替换为 8051/8052
- AJP 端口 8009 分别替换为 8091/8092(也可以直接删除该配置)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| sudo sed -i 's/8080/8081/g' $CATALINA_HOME@1/conf/server.xml sudo sed -i 's/8080/8082/g' $CATALINA_HOME@2/conf/server.xml
sudo sed -i 's/8443/8031/g' $CATALINA_HOME@1/conf/server.xml sudo sed -i 's/8443/8032/g' $CATALINA_HOME@2/conf/server.xml
sudo sed -i 's/8005/8051/g' $CATALINA_HOME@1/conf/server.xml sudo sed -i 's/8005/8052/g' $CATALINA_HOME@2/conf/server.xml
sudo sed -i 's/8009/8091/g' $CATALINA_HOME@1/conf/server.xml sudo sed -i 's/8009/8092/g' $CATALINA_HOME@2/conf/server.xml
# sudo sed -i '/8009/d' $CATALINA_HOME@1/conf/server.xml # sudo sed -i '/8009/d' $CATALINA_HOME@2/conf/server.xml
|
3.2.3 调整 JVM 参数
由于 Tomcat 实例数目增多,需要规划 JVM 内存容量,否则可能因为指定的容量过大而启动失败。
sudo vim $CATALINA_HOME/bin/catalina.sh
3.3 配置各实例的服务
3.3.1 创建多实例 service 文件
环境变量 JAVA_OPTS 与 CATALINA_OPTS 的区别:
- JAVA_OPTS: 所有Java程序共用(包括Tomcat),Tomcat 执行 start/run/stop 时用到;
- CATALINA_OPTS: 仅仅Tomcat用到,Tomcat 执行 start/run 时用到(不包括 stop);
所以,最佳方式是使用环境变量 CATALINA_OPTS,只影响 Tomcat 的启动选项。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ## sudo vim /usr/lib/systemd/system/tomcat@.service ## 其中的 %i 将来会被替换为具体的 1/2 [Unit] Description=Apache Tomcat After=syslog.target network.target
[Service] Type=forking Environment="CATALINA_HOME=/opt/apache-tomcat-7.0.90" Environment="CATALINA_BASE=/opt/apache-tomcat-7.0.90@%i" ExecStart=/opt/apache-tomcat-7.0.90/bin/startup.sh ExecStop=/opt/apache-tomcat-7.0.90/bin/shutdown.sh
[Install] WantedBy=multi-user.target
|
3.3.2 设置为开机自动运行
1 2 3 4 5 6 7 8 9
| sudo systemctl daemon-reload sudo systemctl enable tomcat@{1,2} sudo systemctl start tomcat@{1,2}
## curl http://localhost:8081/check.htm ## curl http://localhost:8082/check.htm
## sudo systemctl stop tomcat@{1,2} ## sudo systemctl disable tomcat@{1,2}
|
3.4 实用脚本
1 2 3
| ## vim ~/bin/tomcat-start.sh sudo rm -rf $CATALINA_HOME*/logs/* sudo systemctl start tomcat@{1,2}
|
1 2
| ## vim ~/bin/tomcat-stop.sh sudo systemctl stop tomcat@{1,2}
|
1 2 3
| ## vim ~/bin/tomcat-restart.sh sudo rm -rf $CATALINA_HOME*/logs/* sudo systemctl restart tomcat@{1,2}
|
1 2
| ## vim ~/bin/tomcat-see.sh sudo jps -lvm | grep Bootstrap | grep -v grep
|
</div>