1. 程式人生 > >[CentOS7]安裝tomcat並開啟自啟動

[CentOS7]安裝tomcat並開啟自啟動

安裝jdk

1、CentOS 6.X 和 7.X 自帶有OpenJDK runtime environment (openjdk)。它是一個在linux上實現開源的Java 平臺。
  yum search java | grep -i --color JDK
2、在CentOS linux安裝 JAVA SDK 在命令列終端以root使用者 輸入以下命令yum安裝 OpenSDK :
 yum install java-1.8.0-openjdk  java-1.8.0-openjdk-devel  #安裝openjdk
3、配置環境變數  編輯 
vi /etc/profile
寫入:
  export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
        export CLASSPATH=$JAVA_HOME/lib/*.*
        export PATH=$PATH:$JAVA_HOME/bin
生效:
  source /etc/profile #讓設定立即生效。
確認是否成功:
  echo $JAVA_HOME
        echo $CLASSPATH
        echo $PATH
4、檢視是否安裝成功
java -version    

安裝tomcat

1、安裝wget
yum install wget
2、下載、解壓安裝tomcat
   wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.13/bin/apache-tomcat-9.0.13.tar.gz
tar xvf apache-tomcat-9.0.13.tar.gz -C /usr/local/ ## 解壓 mv /usr/local/apache-tomcat-9.0.13/ /usr/local/tomcat/ ##剪下到tomcat目錄下
3、 把startup.sh和shutdown.sh這兩個腳本里的最前面加上下面一段:
    export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
        export TOMCAT_HOME=/usr/local/tomcat
        export CATALINA_HOME=/usr/local/tomcat
        export CLASS_PATH=$JAVA_HOME/bin/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tool.jar
        export PATH=$PATH:/usr/local/java/bin:/usr/local/tomcat/bin
4、啟動tomcat
 /usr/local/tomcat/bin/startup.sh  

5、開放預設埠8080

firewall-cmd --zone=public --add-port=8080/tcp --permanent
 #重啟
       systemctl restart firewalld.service
       #重新載入載
       firewall-cmd --reload

6、瀏覽器中訪問http://192.168/226/129:8080,出現下面頁面,證明安裝成功

7、設定開機自啟

vi /etc/rc.d/init.d/tomcat

寫入以下內容:

#!/bin/bash
#
# kenny [email protected]
# /etc/rc.d/init.d/tomcat
# init script for tomcat precesses
#
# processname: tomcat
# description: tomcat is a j2se server
# chkconfig: 2345 86 16
# description: Start up the Tomcat servlet engine.

if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
else
echo -e "/atomcat: unable to locate functions lib. Cannot continue."
exit -1
fi

RETVAL=$?
CATALINA_HOME="/usr/local/tomcat"

case "$1" in
start)
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting Tomcat"
$CATALINA_HOME/bin/startup.sh
fi
;;
stop)
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
$CATALINA_HOME/bin/shutdown.sh
fi
;;
*)
echo $"Usage: $0 {start|stop}"
exit 1
;;
esac

exit $RETVAL

8、新增許可權,使得指令碼檔案可執行

chmod 755 /etc/rc.d/init.d/tomcat

9、新增到服務中

chkconfig --add /etc/rc.d/init.d/tomcat

10、在catalina.sh中新增如下內容

#auto startup tomcat config
        export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
        export TOMCAT_HOME=/usr/local/tomcat
        export CATALINA_HOME=/usr/local/tomcat
        export CLASS_PATH=$JAVA_HOME/bin/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tool.jar
        export PATH=$PATH:/usr/local/java/bin:/usr/local/tomcat/bin

重啟tomcat

結語

注意環境變數的配置,jdk的路徑問題。