================================
©Copyright 蕃薯耀 2022-01-06
https://www.cnblogs.com/fanshuyao/
一、下载tomcat9,并上传到服务器
下载地址:
官网下载地址: https://tomcat.apache.org/download-90.cgi 官网tomcat9直接下载地址: https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz
将tomcat的压缩包(apache-tomcat-9.0.56.tar.gz)上传到服务的文件夹中,如:/java
二、tomcat9 安装配置,开机启动
1、解压文件
cd /java tar -zxvf apache-tomcat-9.0.56.tar.gz
2、重命名文件夹
修改短一点,更新容易记。
mv apache-tomcat-9.0.56 tomcat9
3、修改server.xml文件,修改tomcat端口
cd /java/tomcat9/conf/
vi /java/tomcat9/conf/server.xml
将端口由默认的 8080 端口修改成9000:port="9000"
<Connector port="9000" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
4、修改tomcat的context.xml文件,增加缓存大小
不修改此配置,因缓存过小,无法正常加载文件,启动时可能会报错
cd /java/tomcat9/conf vi context.xml
增加的内容如下:
<Resources cacheMaxSize="512000" cachingAllowed="true"/>
具体如下:
<Context> <!-- Default set of monitored resources. If one of these changes, the --> <!-- web application will be reloaded. --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <Resources cacheMaxSize="512000" cachingAllowed="true"/> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> </Context>
5、Centos开放tomcat端口
开放端口,如:9000:
sudo firewall-cmd --zone=public --add-port=9000/tcp --permanent
使用了普通用户,前面增加了sudo,如果是root用户,则不需要
让端口生效:
sudo firewall-cmd --reload
查看防火墙所有开放的端口
sudo firewall-cmd --zone=public --list-ports
6、Centos配置Tomcat的环境变量
编辑/etc/profile,方便在任何地方用Tomcat命令
sudo vi /etc/profile
增加Tomcat变量配置
CATALINA_HOME=/java/tomcat9 CATALINA_BASE=/java/tomcat9 PATH=$PATH:$CATALINA_BASE/bin export CATALINA_HOME CATALINA_BASE PATH
重新编译,让配置生效
source /etc/profile
7、修改tomcat运行的jvm
setenv.sh默认是没有的,直接增加
vi /java/tomcat9/bin/setenv.sh
增加的内容:
#add tomcat pid CATALINA_PID="$CATALINA_BASE/tomcat-9000.pid" #add java opts JAVA_OPTS="-server -XX:PermSize=1024m -XX:MaxPermSize=1024m -Xms512m -Xmx512m -XX:MaxNewSize=256m"
添加权限确保文件setenv.sh可执行
chmod +x /java/tomcat9/bin/setenv.sh
8、修改tomcat的setclasspath.sh,配置Java路径
在setclasspath.sh的头加上下面的配置,不然会报错(Neither the JAVA_HOME nor the JRE_HOME environment variable is defined):
vi /java/tomcat9/bin/setclasspath.sh
在Set JAVA_HOME or JRE_HOME这句话下面增加,增加的内容:
# ----------------------------------------------------------------------------- # Set JAVA_HOME or JRE_HOME if not already set, ensure any provided settings # are valid and consistent with the selected start-up options and set up the # endorsed directory. # ----------------------------------------------------------------------------- export JAVA_HOME=/java/jdk1.8 export JRE_HOME=/java/jdk1.8/jre
三、tomcat启动运行
1、启动tomcat
cd /java/tomcat9/bin ./startup.sh
2、关闭Tomcat服务
./shutdown.sh
3、tomcat开机启动,tomcat自动启动
增加tomat服务启动文件:tomcat.9000.service
cd /usr/lib/systemd/system sudo vi /usr/lib/systemd/system/tomcat.9000.service
tomcat.9000.service文件内容:
[Unit] Description=Tomcat-9000 After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=oneshot ExecStart=/java/tomcat9/bin/startup.sh ExecStop=/java/tomcat9/bin/shutdown.sh ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID RemainAfterExit=yes [Install] WantedBy=multi-user.target
root用户开启tomcat开机启动服务:
systemctl enable tomcat.9000.service
root用户启动Tomcat服务:
systemctl start tomcat.9000.service
root用户查看服务状态:
systemctl status tomcat.9000.service
其它命令:
root用户关闭tomcat开机自启: systemctl disable tomcat.9000.service root用户停止服务: systemctl stop tomcat.9000.service root用户重启服务: systemctl restart tomcat.9000.service
4、重启Centos服务器
重启Centos服务器,验证tomcat是否开机启动
reboot -f
四、防火墙的一些命令
查看防火墙状态: firewall-cmd --state 关闭防火墙 systemctl stop firewalld.service 禁止防火墙开机启动 systemctl disable firewalld.service
(时间宝贵,分享不易,捐赠回馈,^_^)
================================
©Copyright 蕃薯耀 2022-01-06
https://www.cnblogs.com/fanshuyao/