设为首页 收藏本站
查看: 1238|回复: 0

[经验分享] linux下tomcat作为daemon运行

[复制链接]
累计签到:22 天
连续签到:1 天
发表于 2015-11-14 11:13:23 | 显示全部楼层 |阅读模式
  在linux下如果想让tomcat在开机时自启动,可以将启动代码写到/etc/rc.local里面。但是,这样的话,tomcat将以root权限运行,这是不安全的。因此,要想办法让tomcat以非特权身份作为daemon运行。
  要将tomcat作为linux的daemon运行,需要commons-daemon工程的jsvc工具,tomcat的bin目录里已经自带了这个工具的源码。
  解压commons-daemon-native.tar.gz,进入unix子目录,然后configure。configure的时候需要指定jdk路径或者当前环境中有JAVA_HOME变量。
  接下来在make的时候可能会出现如下错误:
  ar: libservice.a: Malformed archivemake[1]: *** [libservice.a] Error 1
  这是一个已知的bug:
  The file bin/commons-daemon-native.tar.gz contains dirty (already compiled)code. On some systems this causes "make" to return the following error:ar: libservice.a: Malformed archiveThe solution is to run "make clean" before running "make".I believe that "make clean" should be run before creating the tgz file, so thatthere are no compiled/generated files laying around.
  make以后得到jsvc文件,复制到tomcat的bin目录中。另外在native目录下有一个Tomcat5.sh,是用于tomcat自启动的一个模板,我们可以修改它快速得到一个符合要求的启动文件。在这里,我将它修改为如下内容:
  #!/bin/sh################################################################################ Licensed to the Apache Software Foundation (ASF) under one or more# contributor license agreements.  See the NOTICE file distributed with# this work for additional information regarding copyright ownership.# The ASF licenses this file to You under the Apache License, Version 2.0# (the &quot;License&quot;); you may not use this file except in compliance with# the License.  You may obtain a copy of the License at##     http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an &quot;AS IS&quot; BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.################################################################################ Small shell script to show how to start/stop Tomcat using jsvc# If you want to have Tomcat running on port 80 please modify the server.xml# file:##    <!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->#    <Connector className=&quot;org.apache.catalina.connector.http.HttpConnector&quot;#               port=&quot;80&quot; minProcessors=&quot;5&quot; maxProcessors=&quot;75&quot;#               enableLookups=&quot;true&quot; redirectPort=&quot;8443&quot;#               acceptCount=&quot;10&quot; debug=&quot;0&quot; connectionTimeout=&quot;60000&quot;/>## That is for Tomcat-5.0.x (Apache Tomcat/5.0)## Adapt the following lines to your configurationJAVA_HOME=/usr/local/sun-java6-jdkCATALINA_HOME=/usr/local/apache-tomcat-6DAEMON_HOME=$CATALINA_HOMETOMCAT_USER=tomcat6# for multi instances adapt those lines.TMP_DIR=/var/tmpPID_FILE=/var/run/jsvc.pidCATALINA_BASE=$CATALINA_HOME#CATALINA_OPTS=&quot;-Djava.library.path=/home/jfclere/jakarta-tomcat-connectors/jni/native/.libs&quot;CLASSPATH=/$JAVA_HOME/lib/tools.jar:/$CATALINA_HOME/bin/commons-daemon.jar:/$CATALINA_HOME/bin/bootstrap.jarcase &quot;$1&quot; instart)## Start Tomcat#echo &quot;Starting tomcat6...&quot;$DAEMON_HOME/bin/jsvc /-user $TOMCAT_USER /-home $JAVA_HOME /-Dcatalina.home=$CATALINA_HOME /-Dcatalina.base=$CATALINA_BASE /-Djava.io.tmpdir=$TMP_DIR /-wait 10 /-pidfile $PID_FILE /-outfile $CATALINA_HOME/logs/catalina.out /-errfile $CATALINA_HOME/logs/catalina.err /$CATALINA_OPTS /-cp $CLASSPATH /org.apache.catalina.startup.Bootstrap## To get a verbose JVM#-verbose /# To get a debug of jsvc.#-debug /if test $? -eq 0thenexit 0elseecho &quot;Failed to start tomcat6&quot;exit 1fi;;stop)## Stop Tomcat#$DAEMON_HOME/bin/jsvc /-stop /-pidfile $PID_FILE /org.apache.catalina.startup.Bootstrapif test $? -eq 0thenecho &quot;tomcat6 stopped&quot;exit 0elseecho &quot;Failed to stop tomcat6&quot;exit 1fi;;restart)## Restart Tomcat#if $0 stopthen$0 startelseecho &quot;Failed to stop running server, so refusing to try to start.&quot;fiexit 0;;*)echo &quot;Usage: tomcat6 start|stop|restart&quot;exit 1;;esac
  在以的脚本里面指定了TOMCAT_USER,jsvc将先以特权身份启动tomcat,随后切换到指定的用户,这样可以使tomcat以非特权身份监听需要特权的端口。
  将脚本复制到/etc/init.d,改名为tomcat6,然后执行
  update-rc.d tomcat6 defaults
  配置完成以后,tomcat就可以以非特权用户作为daemon运行了。
  
  
         版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-139085-1-1.html 上篇帖子: 转载 tomcat 下使用 shtml 下篇帖子: win7下整合IIS与tomcat
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表