CentOS7部署安裝Tomcat
阿新 • • 發佈:2018-12-29
本文內容主要用於個人的學習,所以一些本人已經爛熟於心的東西會省略掉。
1、配置JAVA相關內容。
這裡選擇了最新了JDK9,JDK9裡面有個新特性,那就是移除了JRE,所以為了保證軟體能夠正常執行,因此JDK和JRE都下載了。這裡貼一下JDK和JRE的配置檔案內容:
JAVA_HOME=/home/jdk-9.0.4
JRE_HOME=/home/jre-9.0.4
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JRE_HOME/lib/ext:$JAVA_HOME/lib/tools.jar
export PATH JAVA_HOME CLASSPATH
2、下載解壓Tomcat,這裡下載的是8.0的版本。
3、設定Tomcat服務的啟動和關閉
編輯建立檔案:vi /etc/rc.d/init.d/tomcat
檔案內容:
#!/bin/bash
# /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="/home/apache-tomcat-8.5.24" #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
修改檔案許可權:chmod 755 /etc/rc.d/init.d/tomcat
新增到服務中:chkconfig --add /etc/rc.d/init.d/tomcat
編輯tomcat中的catalina.sh檔案:cat /home/apache-tomcat-8.5.24/bin/catalina.sh
新增內容在檔案開頭:
export JAVA_HOME=/home/jdk-9.0.4
export CATALINA_HOME=/home/apache-tomcat-8.5.24
export CATALINA_BASE=/home/apache-tomcat-8.5.24
export CATALINA_TMPDIR=/home/apache-tomcat-8.5.24/temp
4、設定Tomcat開機啟動:
export JAVA_HOME=/home/jdk-9.0.4
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
export CATALINA_HOME=/usr/local/kencery/tomcat/
/usr/local/kencery/tomcat/bin/startup.sh
5、配置防火牆。
關閉firewall服務:
systemctl status firewalld.service #檢測是否開啟了firewall
systemctl stop firewalld.service #關閉firewall
sytsemctl disable firewalld.service #禁止firewall開機自啟
安裝iptables-services服務:
yum install iptables-services
vi /etc/sysconfig/iptables
iptables的內容:
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT #這裡是開放的埠
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
重啟iptables服務
systemctl restart iptables.service
systemctl enable iptables.service
6、重啟系統,瀏覽器訪問 http://ip:8080 如果成功看到Tomcat配置介面表示伺服器配置成功