码迷,mamicode.com
首页 > 其他好文 > 详细

以普通身份运行tomcat

时间:2015-08-20 12:34:20      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

1:由于非root用户不能使用1024以下端口,将tomcat端口使用iptables做转发


iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
iptables -t nat -A PREROUTING -p udp -m udp --dport 80 -j REDIRECT --to-ports 8080

2:更改网站权限

chown -R tomcat:tomcat /usr/local/tomcat/


3:写个服务启动脚本指定tomcat服务运行的用户

  1 #!/bin/bash
  2 #
  3 # chkconfig: - 95 15 
  4 # description: Tomcat start/stop/status script
  5 
  6 #Location of JAVA_HOME (bin files)
  7 JAVA_HOME=/usr/java/jdk1.7.0_79
  8 export JAVA_HOME
  9 
 10 #Add Java binary files to PATH
 11 PATH=$JAVA_HOME/bin:$PATH
 12 export PATH
 13 
 14 #CATALINA_HOME is the location of the configuration files of this instance of Tomcat
 15 CATALINA_HOME=/usr/local/tomcat
 16 
 17 #TOMCAT_USER is the default user of tomcat
 18 TOMCAT_USER=tomcat
 19 
 20 #TOMCAT_USAGE is the message if this script is called without any options
 21 TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
 22 
 23 #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
 24 SHUTDOWN_WAIT=20
 25 
 26 tomcat_pid() {
 27 echo `ps -ef | grep $CATALINA_HOME | grep -v grep | tr -s " "|cut -d" " -f2`
 28 }
 29 
 30 start() {
 31 pid=$(tomcat_pid)
 32 if [ -n "$pid" ];then
 33 echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
 34 else
 35 echo -e "\e[00;32mStarting tomcat\e[00m"
 36 if [ `user_exists $TOMCAT_USER` = "1" ];then
 37 su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
 38 else
 39 $CATALINA_HOME/bin/startup.sh
 40 fi
 41 status
 42 fi
 43 return 0
 44 }
 45 "/etc/init.d/tomcat" 111L, 2301C 18,1 Top
 46 #!/bin/bash
 47 #
 48 # chkconfig: - 95 15 
 49 # description: Tomcat start/stop/status script
 50 
 51 #Location of JAVA_HOME (bin files)
 52 JAVA_HOME=/usr/java/jdk1.7.0_79
 53 export JAVA_HOME
 54 
 55 #Add Java binary files to PATH
 56 PATH=$JAVA_HOME/bin:$PATH
 57 export PATH
 58 
 59 #CATALINA_HOME is the location of the configuration files of this instance of Tomcat
 60 CATALINA_HOME=/usr/local/tomcat
 61 
 62 #TOMCAT_USER is the default user of tomcat
 63 TOMCAT_USER=tomcat
 64 
 65 #TOMCAT_USAGE is the message if this script is called without any options
 66 TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
 67 
 68 #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
 69 SHUTDOWN_WAIT=20
 70 
 71 tomcat_pid() {
 72 echo `ps -ef | grep $CATALINA_HOME | grep -v grep | tr -s " "|cut -d" " -f2`
 73 }
 74 
 75 start() {
 76 pid=$(tomcat_pid)
 77 if [ -n "$pid" ];then
 78 echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
 79 else
 80 echo -e "\e[00;32mStarting tomcat\e[00m"
 81 if [ `user_exists $TOMCAT_USER` = "1" ];then
 82 su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
 83 else
 84 $CATALINA_HOME/bin/startup.sh
 85 fi
 86 status
 87 fi
 88 return 0
 89 }
 90 
 91 status(){
 92 pid=$(tomcat_pid)
 93 if [ -n "$pid" ];then
 94 echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
 95 else
 96 echo -e "\e[00;31mTomcat is not running\e[00m"
 97 fi
 98 }
 99 
100 stop() {
101 pid=$(tomcat_pid)
102 if [ -n "$pid" ];then
103 echo -e "\e[00;31mStoping Tomcat\e[00m"
104 $CATALINA_HOME/bin/shutdown.sh
105 
106 let kwait=$SHUTDOWN_WAIT
107 count=0;
108 until [ `ps -p $pid | grep -c $pid` = 0 ] || [ $count -gt $kwait ]
109 do
110 echo -n -e "\e[00;31mwaiting for processes to exit\e[00m\n";
111 sleep 1
112 let count=$count+1;
113 done
114 
115 if [ $count -gt $kwait ];then
116 echo -n -e "\n\e[00;31mkilling processes which didn‘t stop after $SHUTDOWN_WAIT seconds\e[00m"
117 kill -9 $pid
118 fi
119 else
120 echo -e "\e[00;31mTomcat is not running\e[00m"
121 fi
122 
123 return 0
124 }
125 
126 user_exists(){
127 if id -u $1 >/dev/null 2>&1; then
128 echo "1"
129 else
130 echo "0"
131 fi
132 }
133 
134 case $1 in
135 start)
136 start
137 ;;
138 
139 stop)
140 stop
141 ;;
142 
143 restart)
144 stop
145 start
146 ;;
147 
148 status)
149 status
150 ;;
151 
152 *)
153 echo -e $TOMCAT_USAGE
154 ;;
155 esac
156 exit 0

 

以普通身份运行tomcat

标签:

原文地址:http://www.cnblogs.com/ylion/p/4744357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!