Ubuntu Server 18.04 LTS install and configure Tomcat Tomcat service management systemctl

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/choimroc/article/details/102759655

Software Environment

  • System Version: Ubuntu Server 18.04.1 LTS
  • JDK versions: Java SE Development Kit 8u231
  • Tomcat版本:Tomcat 9.0.27 Released

Download and install JDK

1. Download and install

Since JDK download need to log in with a wgetdownload is not convenient, it is recommended to download the other machine is now good and then copied to Ubuntuthe.

The JDK downloaded archive into the specified directory

tar -zxvf jdk-8u231-linux-x64.tar.gz -C ~/server

JDK directory is decompressed/home/ubuntu/server/jdk1.8.0_231

PS: Because Linux permissions problems, it is recommended to install the JDK directory does not require root privileges. I am here is to create a directory server at home

2. Configure JDK environment variables

If there is no vim installed, you need to install

sudo apt-get install vim

Use vim editing environment variable file

sudo vi /etc/profile

In the final document added JDK environment variables

#set Java environment
export JAVA_HOME=/home/ubuntu/server/jdk1.8.0_231
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

The environment variables to take effect

source /etc/profile

View JDK version

java -version
#结果
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

Download and install Tomcat

Tomcat do not need to download logged in, you can directly wgetdownload

wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz

Unzip to a specific directory

tar -zxvf apache-tomcat-9.0.27.tar.gz ~/server

After extracting the directory where Tomcat is/home/ubuntu/server/apache-tomcat-9.0.27

PS: Many online tutorials say you want to configure the environment variable Tomcat, in fact, do not need


Systemctl Tomcat configuration management service

1. Create setenv.sh file in the bin directory of tomcat

vi ~/server/apache-tomcat-9.0.27/bin/setenv.sh

#setenv.sh文件内容
CATALINA_PID="$CATALINA_BASE/tomcat.pid"

2. Add tomcat.service file

sudo vi /lib/systemd/system/tomcat.service

#tomcat.service文件内容
[Unit]
Description=Tomcat
After=network.target remote-fs.target syslog.target

[Service]
Type=forking
PIDFile=/home/ubuntu/server/apache-tomcat-9.0.27/tomcat.pid
ExecStart=/home/ubuntu/server/apache-tomcat-9.0.27/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

3. overloaded systemctl daemon

#重载守护进程
systemctl daemon-reload

#启动服务
sudo systemctl start tomcat
#停止服务
sudo systemctl stop tomcat
#重启服务
sudo systemctl restart tomcat
#开机自启动
sudo systemctl enable tomcat
#取消自启动
sudo systemctl disable tomcat

Record, share and exchange.

Guess you like

Origin blog.csdn.net/choimroc/article/details/102759655
Recommended