您好,登錄后才能下訂單哦!
小編給大家分享一下CentOS7怎么安裝Tomcat并設(shè)置Tomcat為開機啟動項,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
Tomcat有Tomcat7,Tomcat8和Tomcat9等版本,目前企業(yè)使用較多的是Tomcat8,所以這里以Tomcat8為列
進入Tomcat8下載網(wǎng)址:Tomcat8下載網(wǎng)址https://tomcat.apache.org/download-80.cgi
點擊左側(cè)Download下的對應(yīng)版本,這里我下載的是apache-tomcat-8.5.47.tar.gz,即Linux環(huán)境的壓縮包
tar.gz:Linux環(huán)境下的壓縮包,免安裝
Windows.zip:Windows壓縮包,免安裝,解壓即用,同時注意根據(jù)自己電腦是64位系統(tǒng)還是32位系統(tǒng)下載對應(yīng)的壓縮包
Windows Service Installer:Windows安裝包,32位和64位版本的Windows系統(tǒng)都適用
將下載好的apache-tomcat-8.5.47.tar.gz
放到指定目錄,我這里放到/usr/local/tomcat
,如下圖所示
進入/usr/local/tomcat
目錄,解壓Tomcat壓縮包
# 進入/usr/local/tomcat目錄 cd /usr/local/tomcat # 解壓Tomcat壓縮包 tar -zxvf apache-tomcat-8.5.47.tar.gz
進入Tomcat的bin目錄,啟動Tomcat
# 進入Tomcat的bin目錄,啟動Tomcat cd apache-tomcat-8.5.47/bin/ # 啟動Tomcat ./startup.sh
查看Tomcat是否啟動成功
# 查看Tomcat是否啟動成功執(zhí)行 ps -ef | grep tomcat # 如果輸出如下,說明Tomcat安裝成功 root 2381 1 11 22:18 pts/0 00:00:02 /usr/local/jdk1.8.0_152/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/apache-tomcat-8.5.47/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/apache-tomcat-8.5.47/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-8.5.47/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat/apache-tomcat-8.5.47 -Dcatalina.home=/usr/local/tomcat/apache-tomcat-8.5.47 -Djava.io.tmpdir=/usr/local/tomcat/apache-tomcat-8.5.47/temp org.apache.catalina.startup.Bootstrap start root 2513 29060 0 22:18 pts/0 00:00:00 grep --color=auto tomcat
使用瀏覽器訪問Tomcat,地址Linux的ip:8080,我這里的ip端口是http://47.106.106.158:8080/,如下圖說明在Linux(CentOS7)環(huán)境安裝啟動Tomcat成功,為自己點一個贊
注意:開放8080端口或者關(guān)閉防火墻
附加:開放8080端口或者關(guān)閉防火墻,如果是阿里云只能在阿里云控制臺配置開放端口
# 開啟8080端口 firewall-cmd --zone=public --add-port=8080/tcp --permanent # 查詢端口號8080是否開啟 firewall-cmd --query-port=8080/tcp # 重啟防火墻 firewall-cmd --reload # 查看開放端口列表 firewall-cmd --list-port # 命令含義 --zone #作用域 --add-port=8080/tcp #添加端口,格式為:端口/通訊協(xié)議 --permanent #永久生效,沒有此參數(shù)重啟后失效 # 關(guān)閉防火墻 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall開機啟動
按照上面的方式啟動Tomcat,如果我們的虛擬機或者服務(wù)器關(guān)閉了,重啟服務(wù)器后Tomcat是關(guān)閉的,但是我們希望虛擬機或者服務(wù)器重啟后,Tomcat可以自己啟動,所以我們需要設(shè)置Tomcat為開機啟動項
創(chuàng)建setenv.sh文件,為Tomcat添加啟動參數(shù)
catalina.sh在執(zhí)行的時候會調(diào)用同級路徑下的setenv.sh來設(shè)置額外的環(huán)境變量,因此在/usr/local/tomcat/apache-tomcat-8.5.47/bin路徑下創(chuàng)建setenv.sh文件,內(nèi)容如下:
# 設(shè)置Tomcat的PID文件 CATALINA_PID="$CATALINA_BASE/tomcat.pid" # 添加JVM選項 JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"
在/usr/local/tomcat/apache-tomcat-8.5.47/bin/catalina.sh
文件開頭添加JAVA_HOME和JRE_HOME,其中/usr/local/jdk1.8.0_152為jdk的安裝目錄
export JAVA_HOME=/usr/local/jdk1.8.0_152 export JRE_HOME=/usr/local/jdk1.8.0_152/jre
如果在catalina.sh不配置JAVA_HOME和JRE_HOME就會報如下的錯誤
[root@JourWon ~]# systemctl status tomcat ● tomcat.service - Tomcat Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Mon 2019-10-21 19:54:54 CST; 6s ago Process: 8746 ExecStart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh (code=exited, status=1/FAILURE) Oct 21 19:54:54 JourWon systemd[1]: Starting Tomcat... Oct 21 19:54:54 JourWon startup.sh[8746]: Neither the JAVA_HOME nor the JRE_...d Oct 21 19:54:54 JourWon startup.sh[8746]: At least one of these environment ...m Oct 21 19:54:54 JourWon systemd[1]: tomcat.service: control process exited,...=1 Oct 21 19:54:54 JourWon systemd[1]: Failed to start Tomcat. Oct 21 19:54:54 JourWon systemd[1]: Unit tomcat.service entered failed state. Oct 21 19:54:54 JourWon systemd[1]: tomcat.service failed. Hint: Some lines were ellipsized, use -l to show in full.
在/usr/lib/systemd/system路徑下添加tomcat.service文件,內(nèi)容如下:
[Unit] Description=Tomcat After=network.target remote-fs.target nss-lookup.target [Service] Type=forking TimeoutSec=0 PIDFile=/usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid ExecStart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
此外service文件修改后需要調(diào)用systemctl daemon-reload
命令重新加載。
配置TimeoutSec=0的目的是讓開機啟動不處理tomcat啟動超時,保證tomcat耗時過長時不會被系統(tǒng)terminating,如果不配置可能出現(xiàn)下面的情況
Oct 21 20:26:37 JourWon startup.sh[1634]: Existing PID file found during start. Oct 21 20:26:37 JourWon startup.sh[1634]: Removing/clearing stale PID file. Oct 21 20:26:37 JourWon startup.sh[1634]: Tomcat started. Oct 21 20:26:37 JourWon systemd[1]: PID file /usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid not readable (yet?) after start. Oct 21 20:26:38 JourWon polkitd[464]: Unregistered Authentication Agent for unix-process:1628:19013 (system bus name :1.23, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, loca Oct 21 20:28:07 JourWon systemd[1]: tomcat.service start operation timed out. Terminating. Oct 21 20:28:07 JourWon systemd[1]: Failed to start Tomcat.
把Tomcat加入開機自啟動
systemctl enable tomcat.service
reboot
再次連接后,查看服務(wù)狀態(tài)
[root@JourWon ~]# systemctl status tomcat ● tomcat.service - Tomcat Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled) Active: activating (start) since Mon 2019-10-21 20:12:19 CST; 8s ago Process: 9244 ExecStart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh (code=exited, status=0/SUCCESS) CGroup: /system.slice/tomcat.service └─9255 /usr/local/jdk1.8.0_152/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/apache-tomcat-8.5.47/conf/logging.properties -Djava.util.logging.manager=org.apac... Oct 21 20:12:19 JourWon systemd[1]: Starting Tomcat... Oct 21 20:12:19 JourWon startup.sh[9244]: Existing PID file found during start. Oct 21 20:12:19 JourWon startup.sh[9244]: Removing/clearing stale PID file. Oct 21 20:12:19 JourWon startup.sh[9244]: Tomcat started. Oct 21 20:12:19 JourWon systemd[1]: PID file /usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid not readable (yet?) after start.
查看開機啟動列表命令
systemctl list-unit-files | grep enabled
查看Tomcat是否設(shè)置為開機啟動項,如果顯示為enabled,說明設(shè)置成功
參數(shù)說明
static:表示該服務(wù)與其他服務(wù)相關(guān)聯(lián),不能單獨設(shè)置該服務(wù)的啟動狀態(tài)
disabled:表示禁止開機啟動
enabled:表示允許開機啟動
[root@JourWon ~]# systemctl list-unit-files | grep tomcat tomcat.service enabled
以上是“CentOS7怎么安裝Tomcat并設(shè)置Tomcat為開機啟動項”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。