用户

useradd -s /sbin/nologin tomcat

安装jdk

设置JAVA_HOME等环境变量

sed -i 's#securerandom.source=file:/dev/random#securerandom.source=file:/dev/./urandom#g' "$JAVA_HOME/jre/lib/security/java.security"

安装tomcat

复制两份tomcat/usr/local/目录下

[root@zrd /usr/local]# ls tomcat*
tomcat1:
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work

tomcat2:
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work

chown -R tomcat /usr/local/tomcat*

配置

  • pid

mkdir -p /var/run/tomcat && chown tomcat.tomcat -R /var/run/tomcat && chmod 600 /var/run/tomcat

sed -i '/cygwin=false/i\CATALINA_PID=/var/run/tomcat/tomcat1.pid' /usr/local/tomcat1/bin/catalina.sh

sed -i '/cygwin=false/i\CATALINA_PID=/var/run/tomcat/tomcat2.pid' /usr/local/tomcat2/bin/catalina.sh

grep CATALINA_PID= /usr/local/tomcat1/bin/catalina.sh

grep CATALINA_PID= /usr/local/tomcat2/bin/catalina.sh

  • logs

tomcat1

sed -i 's#${catalina.base}/logs#/home/weblog/tomcat1#g' /usr/local/tomcat1/conf/logging.properties

grep tomcat1 /usr/local/tomcat1/conf/logging.properties

sed -i 's#CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out#CATALINA_OUT=/home/weblog/tomcat1/catalina.out#g' /usr/local/tomcat1/bin/catalina.sh

grep CATALINA_OUT= /usr/local/tomcat1/bin/catalina.sh

sed -i 's#directory="logs"#directory="/home/weblog/tomcat1"#g' /usr/local/tomcat1/conf/server.xml

grep /home/weblog/tomcat1 /usr/local/tomcat1/conf/server.xml

tomat2

sed -i 's#${catalina.base}/logs#/home/weblog/tomcat2#g' /usr/local/tomcat2/conf/logging.properties

grep tomcat2 /usr/local/tomcat2/conf/logging.properties

sed -i 's#CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out#CATALINA_OUT=/home/weblog/tomcat2/catalina.out#g' /usr/local/tomcat2/bin/catalina.sh

grep CATALINA_OUT= /usr/local/tomcat2/bin/catalina.sh

sed -i 's#directory="logs"#directory="/home/weblog/tomcat2"#g' /usr/local/tomcat2/conf/server.xml

grep /home/weblog/tomcat2 /usr/local/tomcat2/conf/server.xml

  • 监听端口

tomat1

sed -i 's#<Server port="8005" shutdown="SHUTDOWN">#<Server port="18005" shutdown="tomcat1">#g' /usr/local/tomcat1/conf/server.xml
grep shutdown= /usr/local/tomcat1/conf/server.xml

sed -i 's#<Connector port="8080"#<Connector port="18080"#g' /usr/local/tomcat1/conf/server.xml
grep 'Connector port="18080"' /usr/local/tomcat1/conf/server.xml

sed -i 's#redirectPort="8443"#redirectPort="18443"#g' /usr/local/tomcat1/conf/server.xml
grep 'redirectPort=' /usr/local/tomcat1/conf/server.xml

sed -i 's#<Connector port="8009"#<Connector port="18009"#g' /usr/local/tomcat1/conf/server.xml
grep 18009 /usr/local/tomcat1/conf/server.xml

tomat2

sed -i 's#<Server port="8005" shutdown="SHUTDOWN">#<Server port="18006" shutdown="tomcat2">#g' /usr/local/tomcat2/conf/server.xml
grep shutdown= /usr/local/tomcat2/conf/server.xml

sed -i 's#<Connector port="8080"#<Connector port="18081"#g' /usr/local/tomcat2/conf/server.xml
grep 'Connector port="18081"' /usr/local/tomcat2/conf/server.xml

sed -i 's#redirectPort="8443"#redirectPort="18444"#g' /usr/local/tomcat2/conf/server.xml
grep 'redirectPort=' /usr/local/tomcat2/conf/server.xml

sed -i 's#<Connector port="8009"#<Connector port="18010"#g' /usr/local/tomcat2/conf/server.xml
grep 18010 /usr/local/tomcat2/conf/server.xml

启动脚本

tomcat1

cat >/usr/lib/systemd/system/tomcat1.service<< 'EOF' 
[Unit]
Description=tomcat1
After=syslog.target network.target

[Service]
Type=forking

# Environment=JAVA_HOME=/usr/lib/jvm/java-8-oracle/
# Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
# Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

Environment=CATALINA_PID=/var/run/tomcat/tomcat1.pid
Environment=CATALINA_HOME=/usr/local/tomcat1
Environment=CATALINA_BASE=/usr/local/tomcat1


ExecStart=/usr/local/tomcat1/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

# 限制20%以内占用率
# CPUQuota=20%
# 可以使用低于1024端口
# AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
EOF

tomcat2

cat >/usr/lib/systemd/system/tomcat2.service<< 'EOF' 
[Unit]
Description=tomcat2
After=syslog.target network.target

[Service]
Type=forking

# Environment=JAVA_HOME=/usr/lib/jvm/java-8-oracle/
# Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
# Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

Environment=CATALINA_PID=/var/run/tomcat/tomcat2.pid
Environment=CATALINA_HOME=/usr/local/tomcat2
Environment=CATALINA_BASE=/usr/local/tomcat2


ExecStart=/usr/local/tomcat2/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

# 限制20%以内占用率
# CPUQuota=20%
# 可以使用低于1024端口
# AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

systemctl cat tomcat1

systemctl cat tomcat2

tomcat 项目存放目录

mkdir -pv /home/wwwroot/{tomcat1,tomcat2}

chown -R tomcat.tomcat /home/wwwroot