需要Java环境
https://blog.csdn.net/weixin_43525993/article/details/124174235
官网下载安装包
wget http://archive.apache.org/dist/tomcat/tomcat-9/v9.0.0.M18/bin/apache-tomcat-9.0.0.M18.tar.gz
解压
tar -zxvf apache-tomcat-9.0.0.M18.tar.gz
移动目录
sudo mv ~/apache-tomcat-9.0.0.M18 /usr/local/tomcat
配置自启动,切换至Tomcat的bin目录执行vi setenv.sh,并按i进入编辑模式,拷贝以下代码粘贴
#add tomcat pid
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
#add java opts
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"
添加权限
sudo chmod +x setenv.sh
配置service
sudo vi /usr/lib/systemd/system/tomcat.service
加入如下内容
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
配置环境变量
sudo vi /etc/profile
加入如下代码
export CATALINA_HOME=/usr/local/tomcat
export PATH=$TOMCAT_HOME/bin:$PATH
使其生效
source /etc/profile
关闭防火墙
systemctl stop firewalld.service
修改端口
sudo vi /usr/local/tomcat/conf/server.xml
将8080改为你想要的端口即可
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
配置gui-manager项目部署方式
sudo vi /usr/local/tomcat/conf/tomcat-users.xml
拷贝以下代码放置标签之间(注意将password和username换成你自己的)
<role rolename="manager"/>
<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<user username="king" password="kingpass" roles="admin-gui,manager-gui"/>
注释标签
sudo vi /usr/local/tomcat/webapps/manager/META-INF/context.xml
将Value标签的内容注释
<Context antiResourceLocking="false" privileged="true" >
<!--注释掉此标签
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
</Context>
启动
service tomcat start
如果报错:启动tomcat 报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
[root@localhost META-INF]# systemctl start tomcat
Job for tomcat.service failed because the control process exited with error code. See "systemctl status tomcat.service" and "journalctl -xe" for details.
[root@localhost META-INF]# systemctl status tomcat.service
● tomcat.service - Tomcat
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Tue 2019-02-19 12:12:06 EST; 11s ago
Process: 7551 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=1/FAILURE)
Feb 19 12:12:06 localhost.localdomain systemd[1]: Starting Tomcat...
Feb 19 12:12:06 localhost.localdomain startup.sh[7551]: Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
Feb 19 12:12:06 localhost.localdomain startup.sh[7551]: At least one of these environment variable is needed to run this program
Feb 19 12:12:06 localhost.localdomain systemd[1]: tomcat.service: control process exited, code=exited status=1
Feb 19 12:12:06 localhost.localdomain systemd[1]: Failed to start Tomcat.
Feb 19 12:12:06 localhost.localdomain systemd[1]: Unit tomcat.service entered failed state.
Feb 19 12:12:06 localhost.localdomain systemd[1]: tomcat.service failed.
打开tomcat的bin目录下的setclasspath.sh,手动加入两个环境变量
关闭
service tomcat stop
访问如下连接即可看到tomcat首页,记得换IP
http://172.18.101.128:8080/
配置开机启动
systemctl enable tomcat
启动tomcat
systemctl start tomcat
停止tomcat
systemctl stop tomcat
重启tomcat
systemctl restart tomcat