In order to install and run Tomcat on the server, we need to have Java installed. We can check if Java is already installed using this command:<\/p>\n
which java<\/pre>\nIf there is no output, it means that Java is not installed on the server yet. We can install Oracle JDK or OpenJDK. We need to have Java version 8 or higher installed on your system to run Tomcat 9. We can install it using the following command:<\/p>\n
sudo apt-get install default-jdk<\/pre>\nIn order to check the Java version, run the following command on your server:<\/p>\n
java -version<\/pre>\nWe should receive the following output:<\/p>\n
openjdk version \"10.0.2\" 2018-07-17\nOpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)\nOpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)\n<\/pre>\n<\/span>Install Tomcat<\/span><\/h2>\nDownload the latest stable version of Tomcat from the Apache Tomcat official website at http:\/\/tomcat.apache.org\/download-90.cgi and extract it in a directory on your server:<\/p>\n
cd \/opt\nwget -O tomcat9.tar.gz http:\/\/mirror.olnevhost.net\/pub\/apache\/tomcat\/tomcat-9\/v9.0.14\/bin\/apache-tomcat-9.0.14.tar.gz\ntar -xvzf tomcat9.tar.gz\nmv apache-tomcat-9.0.14 tomcat9<\/pre>\nIt is not a good idea to run Tomcat as root\u00a0user, so for security reason,\u00a0we will create a new system user:<\/p>\n
useradd -r tomcat9 -d \/opt\/tomcat9 --shell \/bin\/false<\/pre>\nOnce the tomcat9 system user has been created, change the ownership of the \/opt\/tomcat9 directory to tomcat user:<\/p>\n
chown tomcat9:tomcat9 -R \/opt\/tomcat9\/<\/pre>\nCreate a new systemd file for Tomcat:<\/p>\n
vi \/etc\/systemd\/system\/tomcat9.service<\/pre>\nAdd the following content:<\/p>\n
[Unit]\nDescription=Apache Tomcat\nAfter=network.target\n\n[Service]\nType=forking\n\nUser=tomcat9\nGroup=tomcat9\n\nEnvironment=JAVA_HOME=\/usr\/lib\/jvm\/java-11-openjdk-amd64\nEnvironment=CATALINA_PID=\/opt\/tomcat9\/tomcat9.pid\nEnvironment=CATALINA_HOME=\/opt\/tomcat9\nEnvironment=CATALINA_BASE=\/opt\/tomcat9\nEnvironment=\"CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC\"\n\nExecStart=\/opt\/tomcat9\/bin\/startup.sh\nExecStop=\/opt\/tomcat9\/bin\/shutdown.sh\n\nExecReload=\/bin\/kill $MAINPID\nRemainAfterExit=yes\n\n[Install]\nWantedBy=multi-user.target<\/pre>\nSave the tomcat9.service file. Run the following command to reload the systemd manager configuration:<\/p>\n
systemctl daemon-reload<\/pre>\nNow we can start the Apache Tomcat 9 service:<\/p>\n
systemctl start tomcat9<\/pre>\nIn order to enable the Tomcat service to start on server boot, run:<\/p>\n
systemctl enable tomcat9<\/pre>\nWe can check the status of the Tomcat 9 service and confirm that it is properly installed by executing the following command:<\/p>\n
systemctl status tomcat9<\/pre>\nThe output of this command should be similar to this:<\/p>\n
\u25cf tomcat9.service - Apache Tomcat\n Loaded: loaded (\/etc\/systemd\/system\/tomcat9.service; enabled; vendor preset: enabled)\n Active: active (exited) since Thu 2018-12-27 04:34:47 CST; 3s ago\n Process: 5473 ExecStart=\/opt\/tomcat9\/bin\/startup.sh (code=exited, status=0\/SUCCESS)\n Main PID: 5481 (code=exited, status=0\/SUCCESS)\n Tasks: 45 (limit: 2320)\n CGroup: \/system.slice\/tomcat9.service\n \u2514\u25005482 \/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/java -Djava.util.logging.config.file=\/opt\/tomcat9\/conf\/logging.properties -Djava.util.logging.manager=org.apache.jul\n\nDec 27 04:34:47 for-blog-post.rosehostingtest.com systemd[1]: Starting Apache Tomcat...\nDec 27 04:34:47 for-blog-post.rosehostingtest.com startup.sh[5473]: Tomcat started.\nDec 27 04:34:47 for-blog-post.rosehostingtest.com systemd[1]: Started Apache Tomcat.<\/pre>\nApache Tomcat is listening on port 8080, so open a web browser and navigate to http:\/\/IP_address:8080 and you should see the home page of Apache Tomcat.<\/p>\n
<\/p>\n