CentOS 安装 tomcat 并设置 开机自启动
要在CentOS上安装Tomcat并设置开机自启,请按照以下步骤操作:
- 安装Java环境,因为Tomcat是一个基于Java的应用服务器。
sudo yum install java-1.8.0-openjdk-devel
- 添加Tomcat用户
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
- 下载并安装Tomcat
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.62/bin/apache-tomcat-9.0.62.tar.gz
sudo tar xf apache-tomcat-9.0.62.tar.gz -C /opt/tomcat
sudo ln -s /opt/tomcat/apache-tomcat-9.0.62 /opt/tomcat/latest
sudo chown -RH tomcat:tomcat /opt/tomcat/latest
sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'
- 创建systemd服务文件
创建一个名为tomcat.service
的文件:
sudo nano /etc/systemd/system/tomcat.service
添加以下内容:
[Unit]
Description=Tomcat 9 servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"
ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
- 重新加载systemd以读取新的服务文件,启用并启动Tomcat服务:
sudo systemctl daemon-reload
sudo systemctl enable tomcat
sudo systemctl start tomcat
- 验证Tomcat是否正在运行:
sudo systemctl status tomcat
或者在浏览器中访问 http://your_server_ip:8080
来检查Tomcat是否安装并运行正常。
评论已关闭