4.啟動停止tomcat服務 # cd /usr/local/tomcat6/bin # ./start.sh //啟動tomcat # ./shutdown.sh //停止tomcat 查看tomcat的進程: # ps -e | grep java 出現了java這個進程的話,說明你的tomcat差不多了。 5.讓tomcat開機運行: # cd /usr/local/tomcat6/bin/jsvc-src/native/ # cp Tomcat.sh /etc/init.d/tomcat # 這裡以tomcat4的自動運行腳本來做修改 # vi tomcat 修改後內容如下: #×××××××××××××××××××××××××××××× # Linux下該腳本使用方法 # # 1.configure時切記加java的jdk路徑,否則無法使用普通用戶開機自動啟動tomcat # ./configure --with-java=/usr/java/jdk1.6.0_01 # # 2.先為tomcat生成catalina.out,catalina.err日誌文件,存放在 # 路徑:/usr/local/tomcat6/logs, # 方便查看腳本調試時候出現的錯誤信息 # cd /usr/local/tomcat6 # 先進入tomcat6的安裝目錄,再執行以下命令,生成日誌文件! # ./bin/jsvc -cp ./bin/bootstrap.jar \ # -outfile ./logs/catalina.out -errfile ./logs/catalina.err \ # org.apache.catalina.startup.Bootstrap # # 3.更改tomcat安裝目錄許可權 # 切記:tomcat安裝目錄tomcat6文件許可權應該屬於運行tomcat的用戶 # 這裡更改整個tomcat6的文件夾及其下的文件為xiutuo用戶和xiutuo組! # chown -R xiutuo:xiutuo /usr/local/tomcat6 # 不放心的話再執行一次 # chown -R xiutuo:xiutuo /usr/local/tomcat6/* # chmod -R 755 /usr/local/tomcat6 # # cd /usr/local/tomcat6/bin/jsvc-src/native/ # 4.自動啟動腳本編輯、賦權 # cp tomcat5.sh /etc/init.d/tomcat # chown xiutuo:xiutuo tomcat # chmod 711 tomcat # # 建立軟連接 # # ln -s /etc/init.d/tomcat /etc/rc.d/rc3.d/K01tomcat # ln -s /etc/init.d/tomcat /etc/rc.d/rc3.d/S99tomcat # ln -s /etc/init.d/tomcat /etc/rc.d/rc5.d/K01tomcat # ln -s /etc/init.d/tomcat /etc/rc.d/rc5.d/S99tomcat # #--------------------------------------------- # 5.如果查看該腳本已經工作了,方法如下: # 啟動腳本:/etc/init.d/tomcat start # a.通過web瀏覽器查看能不能訪問tomcat的管理頁面 # b.通過查看有沒有jsvc進程 ps -e | grep jsvc # 如果看到倆個jsvc進程,恭喜,你成功啦! # # 6.腳本無法正常工作的解決方法: # 啟動腳本:/etc/init.d/tomcat start # 查看/usr/local/tomcat6/logs/目錄下的倆個日誌文件: # catalina.out,catalina.err, 使用cat查看。 # a.錯誤 # Cannot find daemon loader org/apache/commons/daemon/support/DaemonLoader # 解決:更改tomcat安裝目錄許可權為所有用戶可以讀,並屬於xiutuo用戶和xiutuo組 # 命令:chmod -R 755 /usr/locat/tomcat6 # chown -R xiutuo:xiutuo /usr/locat/tomcat6 # b.錯誤 # Cannot open PID file /var/run/jsvc.pid # 解決:chown xiutuo:xiutuo /var/run/jsvc.pid # chown 744 /var/run/jsvc.pid JAVA_HOME=/usr/java/jdk1.6.0_01 #改成你java安裝目錄 CATALINA_HOME=/usr/local/tomcat6/ #改成你tomcat安裝目錄 DAEMON_HOME=/usr/local/tomcat6/bin/jsvc-src/ #改成jsvc程序所在目錄 TOMCAT_USER=xiutuo #改成啟動tomcat使用的普通用戶 JAVA_HOME=/usr/java/jdk1.6.0_01 #改成你java安裝目錄 CATALINA_HOME=/usr/local/tomcat6 #改成你tomcat安裝目錄 DAEMON_HOME=/usr/local/tomcat6/bin #改成jsvc程序所在目錄,記住該進程是開機tomcat自動啟動進程 #測試該腳本時候,使用/etc/init.d/tomcat start #如果順利啟動的話,通過ps -e可以看到倆個jsvc進程 TOMCAT_USER=xiutuo #改成啟動tomcat使用的普通用戶 TMP_DIR=/var/tmp CATALINA_OPTS= #這個環境變數不管 CLASSPATH=\ $JAVA_HOME/lib/tools.jar:\ $DAEMON_HOME/commons-daemon.jar:\ $CATALINA_HOME/bin/bootstrap.jar # classpath這個很重要,一定要確保這三個 # tools.jar、commons-daemon.jar、bootstrap.jar # 的正確路徑。 case "$1" in start) # # Start Tomcat # $DAEMON_HOME/jsvc \ -user $TOMCAT_USER \ -home $JAVA_HOME \ -Dcatalina.home=$CATALINA_HOME \ -Djava.io.tmpdir=$TMP_DIR \ -outfile $CATALINA_HOME/logs/catalina.out \ -errfile '&1' \ $CATALINA_OPTS \ -cp $CLASSPATH \ org.apache.catalina.startup.Bootstrap # # To get a verbose JVM #-verbose \ # To get a debug of jsvc. #-debug \ ;; stop) # # Stop Tomcat # PID=`cat /var/run/jsvc.pid` kill $PID ;; *) echo "Usage tomcat.sh start/stop" exit 1;; esac # # #××××××××××××××××××××××××××××× ----------------------------------------------------------- 註釋引用官方自動啟動說明: Tomcat can be run as a daemon using the jsvc tool from the commons-daemon project. Source tarballs for jsvc are included with the Tomcat binaries, and need to be compiled. Before running the script, the JAVA_HOME environment variable should be set to the base path of the JDK. Alternately, when calling the ./configure script, the path of the JDK may be specified using the --with-java parameter, such as ./configure --with-java=/usr/java.
Tomcat can then be run as a daemon using the following commands. cd $CATALINA_HOME ./bin/jsvc -cp ./bin/bootstrap.jar \ -outfile ./logs/catalina.out -errfile ./logs/catalina.err \ org.apache.catalina.startup.Bootstrap
The file $CATALINA_HOME/bin/jsvc/native/tomcat.sh can be used as a template for starting Tomcat automatically at boot time from /etc/init.d. The file is currently setup for running Tomcat 4.1.x, so it is necessary to edit it and change the classname from BootstrapService to Bootstrap. Note that the Commons-Daemon JAR file must be on your runtime classpath to run Tomcat in this manner. The Commons-Daemon JAR file is in the Class-Path entry of the bootstrap.jar manifest, but if you get a ClassNotFoundException or a NoClassDefFoundError for a Commons-Daemon class, add the Commons-Daemon JAR to the -cp argument when launching jsvc. ------------------------------------------------------------ 三:配置tomcat應用實列 瀏覽器輸入:http://localhost:8080/ 點擊manager:輸入剛才配置的tomcat用戶:用戶admin和密碼admin 配置虛擬目錄test,並通過 http://localhost:8080/test/來訪問 1.手動配置 在 /usr/local/tomcat6/webapps目錄下, 建立testdir目錄,把相關jsp原代碼放入該目錄。 新建test.xml,並配置context和連接池 # cd /usr/local/tomcat6/webapps/ # vi test.xml 內容如下: <Context path="/test" docBase="/usr/local/tomcat6/webapps/testdir" debug="5" reloadable="true" crossC> <Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@192.168.1.159:1521:db01" username="oracele_db_user" password="db_owner_password" maxActive="20" maxIdle="10" maxWait="-1"/> </Context> 其中: context path="/test"----就是http://localhost:8080/test/里的test虛擬目錄 <Resource name="jdbc/myoracle" ---- myoracle連接池名 url="jdbc:oracle:thin:@192.168.1.159:1521:db01" --192.168.1.159:1521:db01是資料庫ip:埠:SID, username="oracele_db_owner_user" password="db_owner_password" ---分別是資料庫用戶和用戶密碼 打開http://localhost:8080/,點擊manager,進入后, 在Deploy 這一欄里 Deploy directory or WAR file located on server Context Path (optional): /test XML Configuration file URL: /usr/local/tomcat6/testdir/test.xml WAR or Directory URL: 留空即可 點擊deploy按鈕。即配置了一個web應用 2.通過war文件來配置。 在WAR file to deploy這一欄里 Select WAR file to upload:通過瀏覽把war文件路徑添加上, 就可以架設應用事例了,可以參照官方文檔Sample應用實例 ---------------------------------------------------- Sample Application The example app has been packaged as a war file and can be downloaded here (Note: make sure your browser doesn't change file extension or append a new one). The easiest way to run this application is simply to move the war file to your CATALINA_HOME/webapps directory. Tomcat will automatically expand and deploy the application for you. You can view it with the following URL (assuming that you're running tomcat on port 8080 as is the default): http://localhost:8080/sample If you just want to browse the code you can unpack the war file with the jar command. jar -xvf sample.war --------------------------------------------------- ************************************** 補充: 當然可以把test.xml的內容放在 /usr/local/tomcat6/conf/server.xml中的<host>與</host>之間 然後這個應用的程序的文件夾里新建 WEB-INF/web.xml 內容如下: <resource-ref> <description>Oracle Datasource example</description> <res-ref-name>jdbc/myoracle</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 測試jdbc代碼: <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <sql:query var="rs" dataSource="jdbc/TestDB"> select id, foo, bar from testdata </sql:query> <html> <head> <title>DB Test</title> </head> <body> <h2>Results</h2>