# cd /usr/local/tomcat/bin # ./daemon.sh Unknown command: `' Usage: daemon.sh ( commands ... ) commands: run Start Tomcat without detaching from console start Start Tomcat stop Stop Tomcat version What version of commons daemon and Tomcat are you running? # ./daemon.sh start
这时可以查看下当前Tomcat进程的用户:
# ps aux | grep tomcat root 16770 0.0 0.0 10432 408 ? Ss 11:09 0:00 jsvc.exec -java-home /usr/java/jdk1.7.0_72 -user tomcat -pidfile /usr/local/tomcat/logs/catalina-daemon.pid -wait 10 -outfile /usr/local/tomcat/logs/catalina-daemon.out -errfile &1 -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/commons-daemon.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs= -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap tomcat 16771 2.2 14.2 959544 71428 ? Sl 11:09 0:05 jsvc.exec -java-home /usr/java/jdk1.7.0_72 -user tomcat -pidfile /usr/local/tomcat/logs/catalina-daemon.pid -wait 10 -outfile /usr/local/tomcat/logs/catalina-daemon.out -errfile &1 -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/commons-daemon.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs= -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap
注:正常情况下就是有两个进程,参数都是一样,但属主不一样,一个是root,一个是tomcat。
可以看到现在Tomcat使用的是tomcat用户权限在运行了。
如果要关闭Tomcat,则可以使用命令:
# cd /usr/local/tomcat/bin # ./daemon.sh stop
另外,使用Daemon后台启动有个要注意的地方就是必须保证权限,必须保证以下几个命令:
# chown -R tomcat:tomcat /usr/local/tomcat-7.0.57 # chown -R tomcat:tomcat /user/local/tomcat # chmod -R 777 /usr/local/tomcat-7.0.57/conf/Catalina # chmod -R 777 /usr/local/tomcat-7.0.57/conf/Catalina/localhost
当然,我们还有一种方案是直接以普通用户tomcat去执行startup.sh来启动Tomcat,但是这个弊端是非root用户只能监听大于1024的端口,这种情况就无法使得Tomcat去监听80/443端口,并不是一种比较完美的方案。
#Tomcat7的安全配置#
1、删除/usr/local/tomcat/webapps下的所有目录。
2、禁止Tomcat服务器进行目录遍历,以防止泄漏系统信息及服务器信息:
# vim /usr/local/tomcat/conf/web.xml <servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
3、开启Tomcat的日志记录功能:
# vim /usr/local/tomcat/conf/server.xml <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host>
4、禁止显示错误信息:
# vim /usr/local/tomcat/conf/web.xml <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page>