部署问题集合linux设置Tomcat、Docker,以及使用脚本开机自启(亲测)
在Linux环境下设置Tomcat、Docker,并使用脚本实现开机自启动的步骤如下:
- 安装Tomcat:
# 安装Java
sudo apt-get update
sudo apt-get install default-jdk
# 下载Tomcat
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz
# 解压Tomcat
tar -xvf apache-tomcat-9.0.65.tar.gz
# 移动Tomcat到指定目录
sudo mv apache-tomcat-9.0.65 /opt/tomcat
# 设置环境变量
echo "export CATALINA_HOME=/opt/tomcat" >> ~/.bashrc
echo "export PATH=\$CATALINA_HOME/bin:\$PATH" >> ~/.bashrc
source ~/.bashrc
- 安装Docker:
# 更新软件包索引
sudo apt-get update
# 允许APT使用通过HTTPS来传输仓库
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
# 添加Docker的官方GPG密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 设置稳定版仓库
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装Docker Engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
- 创建Tomcat启动脚本:
# 创建脚本文件
sudo nano /etc/init.d/tomcat
# 添加以下内容到脚本中
#!/bin/sh
# chkconfig: 2345 20 80
# description: Tomcat server init script
CATALINA_HOME=/opt/tomcat
case $1 in
start)
echo "Starting Tomcat..."
$CATALINA_HOME/bin/startup.sh
;;
stop)
echo "Stopping Tomcat..."
$CATALINA_HOME/bin/shutdown.sh
;;
restart)
echo "Restarting Tomcat..."
$CATALINA_HOME/bin/shutdown.sh
sleep 2
$CATALINA_HOME/bin/startup.sh
;;
esac
exit 0
# 保存脚本并设置执行权限
sudo chmod +x /etc/init.d/tomcat
- 设置Tomcat开机自启:
# 添加到启动级别
sudo update-rc.d tomcat defaults
- 使用Docker运行Tomcat容器:
# 拉取官方Tomcat镜像
docker pull tomcat:latest
# 运行Tomcat容器
docker run --name my-tomcat -p 8080:8080 -d tomcat
- 设置Docker开机自启:
# 添加Docker服务到开机自启
sudo systemctl enable docker
# 启动Docker服务
sudo systemctl start docker
注意:以上步骤可能需要根据您的Linux发行版和环境进行适当的调整。
评论已关闭