1、安装JDK
$ sudo apt-get install default-jdk
查看java版本:
$ java -version
2、创建用户
为了安全,不建议使用root用户运行Tomcat。
创建新用户:
$ sudo useradd -r tomcat --shell /bin/false
3、安装Tomcat
下载Tomcat最新版,我安装时的最新版本是9.0.0.M6:
$ cd /opt
$ sudo wget http://mirror.symnds.com/software/Apache/tomcat/tomcat-9/v9.0.0.M9/bin/apache-tomcat-9.0.0.M9.tar.gz
解压下载的tar包:
$ sudo tar -zxf apache-tomcat-*.tar.gz
$ sudo mv apache-tomcat-9* tomcat
$ sudo rm apache-tomcat-*.tar.gz
修改目录权限:
$ sudo chown -hR tomcat:tomcat tomcat
把Tomcat注册为systemd服务,方便启动和停止Tomcat:
$ sudo vim /etc/systemd/system/tomcat.service
写入内容:
[Unit]
Description=Tomcat9
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=CATALINA_PID=/opt/tomcat/tomcat9.pid
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment="CATALINA_OPTS=-Xms512m -Xmx512m"
Environment="JAVA_OPTS=-Dfile.encoding=UTF-8 -Dnet.sf.ehcache.skipUpdateCheck=true -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+UseParNewGC"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
启动Tomcat服务:
$ sudo systemctl daemon-reload
$ sudo systemctl start tomcat
$ sudo systemctl enable tomcat # 开启自启
4、测试
使用浏览器访问:http://your_server_IP:8080