<\/span><\/h2>\n\n\n\nLog in to your Ubuntu 20.04 VPS using SSH:<\/p>\n\n\n\n
ssh root@IP_Address -p Port_number<\/pre>\n\n\n\nMake sure to replace \u201cIP_Address\u201d and \u201cPort_number\u201d with your server\u2019s actual IP address and SSH port number.<\/p>\n\n\n\n
Once logged in, you can check whether you have the proper Ubuntu version installed on your server with the following command:<\/p>\n\n\n\n
# lsb_release -a<\/pre>\n\n\n\nYou should get this as your output:<\/p>\n\n\n\n
Distributor ID: Ubuntu\nDescription: Ubuntu 20.04 LTS\nRelease: 20.04\nCodename: focal<\/pre>\n\n\n\nThen, run the following command to make sure that all installed packages on the server are updated to the latest available version.<\/p>\n\n\n\n
# apt update && apt upgrade -y<\/pre>\n\n\n\n<\/span>2. Install Java<\/span><\/h2>\n\n\n\nTomcat 9 requires Java version 8 or higher. We can check if Java is already installed using this command:<\/p>\n\n\n\n
# which java<\/pre>\n\n\n\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 the default JDK, version 11, which is available on the built-in Ubuntu 20.04 repositories by using the following command:<\/p>\n\n\n\n
# apt install default-jdk -y<\/pre>\n\n\n\nOnce installed, we can check the version using this command:<\/p>\n\n\n\n
# java --version<\/pre>\n\n\n\nroot@ubuntu20:~# java --version\nopenjdk 11.0.7 2020-04-14\nOpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)\nOpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)<\/pre>\n\n\n\n<\/span>3. Install Tomcat 9<\/span><\/h2>\n\n\n\nWhen writing this tutorial, the latest stable Tomcat to download is version 9.0.35. You can navigate to https:\/\/tomcat.apache.org\/download-90.cgi for more recent version, if any. To proceed with the installation, let’s download the binary distribution file first.<\/p>\n\n\n\n
# wget https:\/\/downloads.apache.org\/tomcat\/tomcat-9\/v9.0.35\/bin\/apache-tomcat-9.0.35.tar.gz -O tomcat.9.0.35.tar.gz<\/pre>\n\n\n\nIn this article, we will install Tomcat into the \/opt\/tomcat<\/code> directory. Let’s create the directory and decompress the downloaded file there.<\/p>\n\n\n\n# mkdir \/opt\/tomcat<\/pre>\n\n\n\n# tar xzvf tomcat.9.0.35.tar.gz -C \/opt\/tomcat --strip-components=1<\/pre>\n\n\n\n<\/span>4. Add a Tomcat user<\/span><\/h2>\n\n\n\nIt is not a good idea to run Tomcat as root, so for security reasons we will create a new system user:<\/p>\n\n\n\n
# useradd -r tomcat -d \/opt\/tomcat --shell \/bin\/false<\/pre>\n\n\n\nOnce the Tomcat system user has been created, we need to update the ownership of the \/opt\/tomcat<\/code> directory to tomcat user:<\/p>\n\n\n\n# chown tomcat: -R \/opt\/tomcat\/<\/pre>\n\n\n\n<\/span>5. Create a Systemd Service File for Tomcat<\/span><\/h2>\n\n\n\nCheck the path of Tomcat\u2019s home by running this command:<\/p>\n\n\n\n
# update-java-alternatives -l<\/pre>\n\n\n\nOutput<\/p>\n\n\n\n
root@ubuntu20:~# update-java-alternatives -l\njava-1.11.0-openjdk-amd64 1111 \/usr\/lib\/jvm\/java-1.11.0-openjdk-amd64<\/pre>\n\n\n\nWe need the value of the third column for our Tomcat systemd file to set the JAVA_HOME<\/code> environment variable.<\/p>\n\n\n\n# nano \/etc\/systemd\/system\/tomcat.service<\/pre>\n\n\n\nPaste the following into the systemd service file, then save it.<\/p>\n\n\n\n
[Unit]\nDescription=Apache Tomcat\nAfter=network.target\n\n[Service]\nType=forking\n\nUser=tomcat\nGroup=tomcat\n\nEnvironment=JAVA_HOME=\/usr\/lib\/jvm\/java-11-openjdk-amd64\nEnvironment=CATALINA_PID=\/opt\/tomcat\/tomcat.pid\nEnvironment=CATALINA_HOME=\/opt\/tomcat\nEnvironment=CATALINA_BASE=\/opt\/tomcat\nEnvironment=\"CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC\"\n\nExecStart=\/opt\/tomcat\/bin\/startup.sh\nExecStop=\/opt\/tomcat\/bin\/shutdown.sh\n\nExecReload=\/bin\/kill $MAINPID\nRemainAfterExit=yes\n\n[Install]\nWantedBy=multi-user.target\n<\/pre>\n\n\n\nSave the changes, then start Tomcat.<\/p>\n\n\n\n
# systemctl daemon-reload<\/pre>\n\n\n\n# systemctl start tomcat<\/pre>\n\n\n\nTomcat should be started now, you can check it with this command:<\/p>\n\n\n\n
root@ubuntu20:~# systemctl status tomcat\n\u25cf tomcat.service - Apache Tomcat\nLoaded: loaded (\/etc\/systemd\/system\/tomcat.service; disabled; vendor preset: enabled)\nActive: active (running) since Mon 2020-06-01 03:43:31 CEST; 1h 14min ago\nProcess: 12975 ExecStart=\/opt\/tomcat\/bin\/startup.sh (code=exited, status=0\/SUCCESS)\nMain PID: 12997 (java)\nTasks: 30 (limit: 2286)\nMemory: 253.4M\nCGroup: \/system.slice\/tomcat.service\n\u2514\u250012997 \/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/java -Djava.util.logging.config.file=\/opt\/tomcat\/conf\/logging.properties -Djava.util.logging.manager=org.ap>\n\nJun 01 03:43:31 ubuntu20 systemd[1]: Starting Apache Tomcat...\nJun 01 03:43:31 ubuntu20 startup.sh[12975]: Tomcat started.\nJun 01 03:43:31 ubuntu20 systemd[1]: Started Apache Tomcat.\n<\/pre>\n\n\n\nNow, you should be able to access Tomcat at http:\/\/your-server-IP:8080<\/code> .<\/p>\n\n\n\n <\/figure><\/div>\n\n\n\nIn order to make Tomcat automatically run at boot, we need to enable the systemd service file. You can do so like this:<\/p>\n\n\n\n
# systemctl enable tomcat<\/pre>\n\n\n\n