新公司,开发用java,跑在tomcat里,一台机器会跑多个tomcat,原来的运维人员是复制了多份tomcat放到目录下。这样做我觉得管理不方便,遂改为superviosr来控制,同时将tomcat本身与工程文件分开放置。即一台主机只有一份tomcat可执行文件,这样也方便以后升级tomcat,下边简要流水账一下,不解释细节,都能看懂。另外,我把安装过程写成了ansible
playbook,需要的可以去我的github上下载,https://github.com/polygun2000
1.安装Server JRE
]#
mkdir /usr/local/java/
]# tar
xvzf
server-jre-8u121-linux-x64.tar.gz --strip-components=1
-C /usr/local/java
2.安装supervisor
]# yum
localinstall supervisor-3.3.1-2.el7.noarch.rpm
\
python2-setuptools-22.0.5-1.el7.noarch.rpm \
python2-meld3-1.0.2-4.el7.noarch.rpm
3.安装tomcat
]# mkdir
/usr/local/tomcat
]#
groupadd tomcat
]#
useradd -s /bin/nologin -d /usr/local/tomcat -M tomcat
]# tar
xvzf
apache-tomcat-8.0.43.tar.gz --strip-components=1 -C
/usr/local/tomcat
]# cd
/usr/local/tomcat
]# rm
-rf webapps logs temp work
4.准备各独立的tomcat实例目录,这里只举一个website1的例子,更多只需要重复这步即可。
]# mkdir
-p
/data/apps/website1/{bin,webapps,lib,logs,temp,work}
]# cp -r
/usr/local/tomcat/conf /data/apps/website1/
]# cp
/usr/local/tomcat/bin/*.sh
/data/apps/website1/bin
]# cd
/data/apps/website1
]# chmod
g+w temp logs work
]# vi
/data/apps/website1/bin/setenv.sh
#!/bin/sh
CATALINA_HOME=/usr/local/tomcatCATALINA_BASE=/data/apps/website1/CATALINA_PID=/data/apps/website1/temp/tomcat.pidJAVA_HOME=/usr/local/java
]# chmod
755 /data/apps/website1/bin/setenv.sh
]# vi
/data/apps/website1/bin/supervisord-wrapper.sh
#!/bin/bash# Source: http://serverfault.com/questions/425132/controlling-tomcat-with-supervisorfunction shutdown(){dateecho "Shutting down Tomcat"unset CATALINA_PID # Necessary in some cases/data/apps/website1/bin/catalina.sh stop}dateecho "Starting Tomcat". /data/apps/website1/bin/catalina.sh start -security# Allow any signal which would kill a process to stop Tomcattrap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTPecho "Waiting for `cat $CATALINA_PID`"wait `cat $CATALINA_PID`
]# chmod
755 /data/apps/website1/bin/supervisord-wrapper.sh
]# chown
-R tomcat.tomcat /data/apps/website1
配置catalina.out日志文件的轮转,以免硬盘被写满
]# vi
/etc/logrotate.d/tomcat-website1
/data/apps/website1/logs/catalina.out {copytruncatedailyrotate 7compressmissingoksize 10Mdateext}
5.配置supervisor的program
]# vi
/etc/supervisord.d/website1.ini
[program:website1]directory = /data/apps/website1command = /data/apps/website1/bin/supervisord-wrapper.shenvironment=HOME="/usr/local/tomcat",USER="tomcat"stdout_logfile = /data/apps/website1/logs/catalina.outautostart = truestartsecs = 10stopsignal = QUITstopasgroup = trueuser = tomcatredirectstderr = true
6.配置supervisor,打开内置的http
server,以便用web方式启动,停止tomcat
]#
vi /etc/supervisord.conf,取消如下两行的注释:
;[inet_http_server]
;port=*:9001
;
(ip_address:port specifier, *:port for all iface)
7.启动supervisor,会自动启动website1的tomcat
]#
systemctl start supervisord
]#
systemctl enable supervisord
参考文档:
https://blog.tankywoo.com/2014/12/05/supervisor-run-program-as-nonroot.html
https://tomcat.apache.org/tomcat-7.0-doc/RUNNING.txt
http://blog.e-zest.com/how-to-run-multiple-tomcat-instances-on-single-linux-server/
https://dzone.com/articles/running-multiple-tomcat
http://www.ramkitech.com/2011/07/running-multiple-tomcat-instances-on.html
https://www.mulesoft.com/tcat/tomcat-webapp