1. Connecting to the server
Connect to your VPS server using SSH. You can use PuTTY on Windows and Terminal on Linux and macOS.
1. Update system and installing Java
Before you start, you should update system packages. In the terminal, type the following:
`sudo apt update`
`sudo apt upgrade`
To install Tomcat on a VPS server, you will need to have Java installed. You can check if you have Java installed by typing:
`java -version`
If Java is not installed, you can install it by typing:
`sudo apt install default-jdk`
1. Creating a user for Tomcat
It is recommended to run Tomcat under a user without root privileges. To create a new user, type the following:
`sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat`
1. Installing Tomcat
The following steps will download and install Tomcat:
1. Go to the Tomcat download page at the Apache website, then copy the link address for the .tar.gz package under the Binary Distributions Core section.
1. Use the ‘wget’ command followed by the copied link to download the Tomcat package. For example:
`wget http://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.22/bin/apache-tomcat-9.0.22.tar.gz`
1. Extract the package to the /opt/tomcat directory:
`sudo tar xf apache-tomcat-9*.tar.gz -C /opt/tomcat`
1. Setting permissions
Set the correct permissions for the tomcat user over the tomcat installation:
`sudo chown -R tomcat: /opt/tomcat/latest`
`sudo sh -c ‘chmod +x /opt/tomcat/latest/bin/*.sh’`
1. Creating a Systemd Service File
Miscellaneously, create a new service file by typing:
`sudo nano /etc/systemd/system/tomcat.service`
Then, paste the following into the file:
```
[Unit]
Description=Tomcat 9 servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=“JAVA_HOME=/usr/lib/jvm/default-java“
Environment=“JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true”
Environment=“CATALINA_BASE=/opt/tomcat/latest“
Environment=“CATALINA_HOME=/opt/tomcat/latest“
Environment=“CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid“
Environment=“CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC”
ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
```
Use Ctrl+X to exit and save the file. Then reload the systemd daemon:
`sudo systemctl daemon-reload`
1. Starting Tomcat
Start the Tomcat service by typing:
`sudo systemctl start tomcat`
Then, enable it to start on boot by typing:
`sudo systemctl enable tomcat`
1. Restart the Server
Finally, reboot your server to make sure that all changes take effect:
`sudo reboot`
1. Accessing Tomcat
After the server has rebooted, verify that Tomcat is working by accessing it through a web browser at http://your_server_IP:8080
Replace “your_server_IP” with your server’s IP address. You should see the Tomcat default home page.
That’s it! You have successfully installed Tomcat on your VPS server.