小編給大家分享一下CentOS7怎么安裝Tomcat并設(shè)置Tomcat為開機(jī)啟動(dòng)項(xiàng),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)是專業(yè)的南譙網(wǎng)站建設(shè)公司,南譙接單;提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行南譙網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
Tomcat有Tomcat7,Tomcat8和Tomcat9等版本,目前企業(yè)使用較多的是Tomcat8,所以這里以Tomcat8為列
進(jìn)入Tomcat8下載網(wǎng)址:Tomcat8下載網(wǎng)址https://tomcat.apache.org/download-80.cgi
點(diǎn)擊左側(cè)Download下的對(duì)應(yīng)版本,這里我下載的是apache-tomcat-8.5.47.tar.gz,即Linux環(huán)境的壓縮包
tar.gz:Linux環(huán)境下的壓縮包,免安裝
Windows.zip:Windows壓縮包,免安裝,解壓即用,同時(shí)注意根據(jù)自己電腦是64位系統(tǒng)還是32位系統(tǒng)下載對(duì)應(yīng)的壓縮包
Windows Service Installer:Windows安裝包,32位和64位版本的Windows系統(tǒng)都適用
將下載好的apache-tomcat-8.5.47.tar.gz
放到指定目錄,我這里放到/usr/local/tomcat
,如下圖所示
進(jìn)入/usr/local/tomcat
目錄,解壓Tomcat壓縮包
# 進(jìn)入/usr/local/tomcat目錄 cd /usr/local/tomcat # 解壓Tomcat壓縮包 tar -zxvf apache-tomcat-8.5.47.tar.gz
進(jìn)入Tomcat的bin目錄,啟動(dòng)Tomcat
# 進(jìn)入Tomcat的bin目錄,啟動(dòng)Tomcat cd apache-tomcat-8.5.47/bin/ # 啟動(dòng)Tomcat ./startup.sh
查看Tomcat是否啟動(dòng)成功
# 查看Tomcat是否啟動(dòng)成功執(zhí)行 ps -ef | grep tomcat # 如果輸出如下,說(shuō)明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
使用瀏覽器訪問(wèn)Tomcat,地址Linux的ip:8080,我這里的ip端口是http://47.106.106.158:8080/,如下圖說(shuō)明在Linux(CentOS7)環(huán)境安裝啟動(dòng)Tomcat成功,為自己點(diǎn)一個(gè)贊
注意:開放8080端口或者關(guān)閉防火墻
附加:開放8080端口或者關(guān)閉防火墻,如果是阿里云只能在阿里云控制臺(tái)配置開放端口
# 開啟8080端口 firewall-cmd --zone=public --add-port=8080/tcp --permanent # 查詢端口號(hào)8080是否開啟 firewall-cmd --query-port=8080/tcp # 重啟防火墻 firewall-cmd --reload # 查看開放端口列表 firewall-cmd --list-port # 命令含義 --zone #作用域 --add-port=8080/tcp #添加端口,格式為:端口/通訊協(xié)議 --permanent #永久生效,沒(méi)有此參數(shù)重啟后失效 # 關(guān)閉防火墻 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall開機(jī)啟動(dòng)
按照上面的方式啟動(dòng)Tomcat,如果我們的虛擬機(jī)或者服務(wù)器關(guān)閉了,重啟服務(wù)器后Tomcat是關(guān)閉的,但是我們希望虛擬機(jī)或者服務(wù)器重啟后,Tomcat可以自己?jiǎn)?dòng),所以我們需要設(shè)置Tomcat為開機(jī)啟動(dòng)項(xiàng)
創(chuàng)建setenv.sh文件,為Tomcat添加啟動(dòng)參數(shù)
catalina.sh在執(zhí)行的時(shí)候會(huì)調(diào)用同級(jí)路徑下的setenv.sh來(lái)設(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選項(xiàng) 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就會(huì)報(bào)如下的錯(cuò)誤
[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的目的是讓開機(jī)啟動(dòng)不處理tomcat啟動(dòng)超時(shí),保證tomcat耗時(shí)過(guò)長(zhǎng)時(shí)不會(huì)被系統(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加入開機(jī)自啟動(dòng)
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.
查看開機(jī)啟動(dòng)列表命令
systemctl list-unit-files | grep enabled
查看Tomcat是否設(shè)置為開機(jī)啟動(dòng)項(xiàng),如果顯示為enabled,說(shuō)明設(shè)置成功
參數(shù)說(shuō)明
static:表示該服務(wù)與其他服務(wù)相關(guān)聯(lián),不能單獨(dú)設(shè)置該服務(wù)的啟動(dòng)狀態(tài)
disabled:表示禁止開機(jī)啟動(dòng)
enabled:表示允許開機(jī)啟動(dòng)
[root@JourWon ~]# systemctl list-unit-files | grep tomcat tomcat.service enabled
以上是“CentOS7怎么安裝Tomcat并設(shè)置Tomcat為開機(jī)啟動(dòng)項(xiàng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!