How To Run Tomcat Server At Startup On Ubuntu Server
Every developer knows that for running any web applications it needs a web server. One of the most famous web servers for J2EE is Apache Tomcat. Tomcat runs applications created in JSP, JSF, Javascript, and available languages. To start the webserver you need to run a shell file called startup.sh and if you need to restart your server you need to go to the folder and run the same command again. What if you could run a script for this command to start tomcat every time your system starts? In this article, we will show you how to run apache tomcat server in Ubuntu on startup, but first, let me tell you more about Tomcat.
Apache tomcat
According to Apache website “The Apache Tomcat® software is an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language, and Java WebSocket technologies. The Java Servlet, JavaServer Pages, Java Expression Language, and Java WebSocket specifications are developed under the Java Community Process.â€The last stable release is Tomcat 8.5.15.
How To Install Tomcat Server?
If we were to install on Ubuntu desktop we just had to go to Tomcat download page and choose the format to download it. Since we are using the command line we have to download it using the link to the compressed file. This process was tested on from tomcat7 to tomcat9 alpha. To do it run the command below on the command line:
Step 1:
$ wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.14/bin/apache-tomcat-8.5.14.tar.gz
€‹Step 2:
$ sudo mv apache-tomcat-8.5.14.tar.gz /var/opt/
€‹Step 2.5 (optional): €‹To avoid asking for the password change to the superuser.
$ sudo su
Step 3: €‹Extract the files.
$ tar -xvzf apache-tomcat-8.5.14.tar.gz tomcat
Note that if you didn’t use step 2.5 you will have to use the above command with sudo.
Step 4:
Create the init script in /etc/init.d/tomcat8 and include the information below - #!/bin/bash### BEGIN INIT INFO # Provides: tomcat7 # Required-Start: $network # Required-Stop: $network # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start/Stop Tomcat server ### END INIT INFO
PATH=/sbin:/bin:/usr/sbin:/usr/bin
start() { sh /var/opt/tomcat8/bin/startup.sh }
stop() { sh /var/opt/tomcat8/bin/shutdown.sh }
case $1 in start|stop) $1;; restart) stop; start;; *) echo "Run as $0 "; exit 1;; Esac Step 5:Change its permissions and add the correct symlinks automatically: $ chmod 755 /etc/init.d/tomcat7 $ update-rc.d tomcat7 defaults
Now your tomcat will start when your system starts and you can control it with service tomcat7 <stop|start|restart>Note for starting Tomcat you must have OpenJDK or JDK installed.
Conclusion
‹There are many solutions for running web server applications and this is only one of them. If you are a web developer or server admin, this script could help you keep your server always up, without needing to get worried if you forgot to run in on your system start. Leave your thoughts in the comment section below.