欢迎关注我的公众号《pencil带你玩转Linux》,回复“Linux学习资料”获取视频教程哦。
1.获取apache-tomcat-9.0.17安装包
wget http://archive.apache.org/dist/tomcat/tomcat-9/v9.0.17/bin//apache-tomcat-9.0.17.tar.gz
2.解压apache-tomcat-9.0.17安装包,并把apache-tomcat-9.0.17安装包移到/etc重命名为tomcat
tar xzvf apache-tomcat-9.0.17.tar.gz && mv apache-tomcat-9.0.17 /etc/tomcat
3.tomcat目录结构
[root@Tomcat ~]# cd /usr/local/tomcat/
[root@Tomcat tomcat]# ls
bin --启动命令目录
conf --配置文件目录 *重点
lib --库文件目录
logs --日志文件目录 *重点
temp --临时缓存文件
webapps --web应用家目录,项目存放的目录 *重点
work --工作缓存目录
4.安装jdk
1.查看是否安装了java
rpm -qa | grep java
2.安装java
yum install -y java-1.8.0-openjdk-devel*
3.把java添加到系统环境变量
[root@Tomcat ~]# vim /etc/profile
export PATH=$PATH:/usr/local/git/bin:/usr/local/mysql/bin/:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
4.重新加载 /etc/profile
source /etc/profile
5.查看java版本
java -version
5.把tomcat默认端口8080加入防火墙信任端口
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload
或者关闭防火墙
systemctl stop firewalld
systemctl distable firewalld
6.如果要更改tomcat端口号:
vim /etc/tomcat/conf/server.xml
找到8080这一行,把8080改成想要的端口
redirectPort="8443" />
并把端口加入防火墙信任端口,如第5步
7.由于启动和关闭tomcat服务得去tomcat目录下的bin下的执行startup.sh或shutdown.sh,所以我把它集合写成命令
进入 /usr/local/bin
vim tomcat.sh
#!/bin/bash
function restart()
{
/etc/tomcat/bin/shutdown.sh &> /dev/null
if [ $? -eq 0 ];then
/etc/tomcat/bin/startup.sh &> /dev/null
sleep 2
echo " tomcat服务重新启动成功!"
else
echo "error : tomcat服务重新启动失败! 请检查……"
fi
}
function start()
{
/etc/tomcat/bin/startup.sh &> /dev/null
if [ $? -eq 0 ];then
sleep 2
echo "tomcat服务启动成功!"
else
echo "error : tomcat服务启动失败! 请检查……"
fi
}
function version()
{
/etc/tomcat/bin/version.sh
}
function stop()
{
/etc/tomcat/bin/shutdown.sh &> /dev/null
if [ $? -eq 0 ];then
sleep 2
echo "error : tomcat服务已关闭"
else
echo "error : tomcat服务无法关闭! 请检查……"
fi
}
function help()
{
/etc/tomcat/bin/version.sh
echo ""
echo " 参数: "
echo " restart | 重启tomcat服务"
echo " start | 启动tomcat服务"
echo " stop | 关闭tomcat服务"
echo " --version | 查看tomcat版本"
echo " --help | 重启tomcat服务帮助"
echo " -h | 重启tomcat服务帮助"
echo " status | 查看tomcat服务运行状态"
}
function status()
{
ps -ef | grep tomcat | grep -v grep | grep -v status &> /dev/null
if [ $? -eq 0 ];then
sleep 2
echo "tomcat服务已启动"
else
echo "tomcat服务未启动"
fi
}
case $1 in
"start")
start
;;
"restart")
restart
;;
"stop")
stop
;;
"--version")
version
;;
"--help" | "-h")
help
;;
"status")
status
;;
* )
#echo "start|restart|stop"
help
;;
esac
给tomcat.sh执行权限
chmod +x tomcat.sh
把执行tomcat.sh脚本设置成别名,并加入 /root/.bashrc
vim /root/.bashrc
alias tomcat='/usr/local/bin/tomcat.sh'
重新加载/root/.bashrc
source /root/.bashrc
验证命令是否成功
[root@Tomcat ~]#tomcat --version
Using CATALINA_BASE: /etc/tomcat
Using CATALINA_HOME: /etc/tomcat
Using CATALINA_TMPDIR: /etc/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /etc/tomcat/bin/bootstrap.jar:/etc/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/9.0.17
Server built: Mar 13 2019 15:55:27 UTC
Server number: 9.0.17.0
OS Name: Linux
OS Version: 3.10.0-957.1.3.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_201-b09
JVM Vendor: Oracle Corporation
启动tomcat
[root@Tomcat ~]#tomcat start
tomcat服务启动成功!
这时tomcat已经启动成功了!
把tomcat加入systemd服务
vim /usr/lib/systemd/system/tomcat9.service
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=oneshot
ExecStart=/etc/tomcat/bin/startup.sh
ExecStop=/etc/tomcat/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
命令
systemctl start tomcat 启动tomcat
systemctl enable tomcat 开启启动
systemctl stop tomcat 关闭tomcat
systemctl restart tomcat 重新启动tomcat
8. 火狐打开或者curl访问tomcat
firefox ip:8080
或
curl ip:8080
9.由于项目是放在tomcat目录下的webapps目录下,所以可以在此目录下创建测试项目
1.进入webapps目录下,创建test
mkdir test
2.进入目录
cd test
3.创建index.jsp
vim index.jsp
welcome to here!
4. 火狐打开或者curl访问tomcat的test项目
firefox ip:8080/test
或
curl ip:8080/test
自此tomcat搭建成功