Monday, 19 June 2017

Start Tomcat and Apache at Boot Time

Make sure there is a valid user "tomcat" and that this user has rw permissions in the $CATALINA_HOME/conf and $CATALINA_HOME/logs directories. Also make sure that $JAVA_HOME is set. You will start Tomcat as user "tomcat" to avoid running it as root. The Apache server is started as root because it uses port 80 (lower than 1024) but it spawns processes that run as "nobody".

Save the following scripts as /etc/init.d/tomcat and /etc/init/apache. They will automatically be read and run at boot time. Check the log files if it does not start properly.




Make a link to it from /etc/rc5.d such as:

cd /etc/rc5.d
sudo ln -s ../init.d/tomcat S71tomcat
sudo ln -s ../init.d/apache S72apache

----------------------------  /etc/init.d/tomcat  ------------------------
#!/bin/bash
#
# tomcat      
#
# chkconfig:
# description: Start up the Tomcat servlet engine.

# Source function library.
. /etc/init.d/functions


RETVAL=$?
CATALINA_HOME="/usr/apps/apache/tomcat/jakarta-tomcat-4.0.4"

case "$1" in
 start)
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
   echo $"Starting Tomcat"
            /bin/su tomcat $CATALINA_HOME/bin/startup.sh
        fi
;;
 stop)
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
   echo $"Stopping Tomcat"
            /bin/su tomcat $CATALINA_HOME/bin/shutdown.sh
        fi
  ;;
 *)
  echo $"Usage: $0 {start|stop}"
exit 1
;;
esac

exit $RETVAL


-----------------------  end of /etc/init.d/tomcat  ----------------------


----------------------------  /etc/init.d/apache  ------------------------
#!/bin/bash
#
# apache      
#
# chkconfig:
# description: Start up the Apache web server.

# Source function library.
. /etc/init.d/functions


RETVAL=$?
APACHE_HOME="/usr/apps/apache/apache"

case "$1" in
 start)
if [ -f $APACHE_HOME/bin/apachectl ]; then
   echo $"Starting Apache"
        $APACHE_HOME/bin/apachectl start
    fi
;;
 stop)
if [ -f $APACHE_HOME/bin/apachectl ]; then
   echo $"Stopping Apache"
        $APACHE_HOME/bin/apachectl stop
    fi
  ;;
 *)
  echo $"Usage: $0 {start|stop}"
exit 1
;;
esac

exit $RETVAL
-----------------------  end of /etc/init.d/apache  ----------------------
Tomcat Installation:



1) Create a user called "tomcat" to own the Tomcat installation.

useradd tomcat


2) Install the JDK (7 or 8) from the tarball under the tomcat user.

# su - tomcat

$ tar xzf /tmp/jdk-7u79-linux-x64.tar.gz

# or

$ tar xzf /tmp/jdk-8u77-linux-x64.tar.gz

Install Tomcat from the tarball under the home directory of the "tomcat" user.

3) tar xzf /tmp/apache-tomcat-7.0.68.tar.gz


Set the following environment variables and append them to the "/home/tomcat/.bash_profile" so they are set for subsequent logins. Remember to set the desired JAVA_HOME correctly.

#export JAVA_HOME=/home/tomcat/jdk1.7.0_79
export JAVA_HOME=/home/tomcat/jdk1.8.0_77
export CATALINA_HOME=/home/tomcat/apache-tomcat-7.0.68
export CATALINA_BASE=$CATALINA_HOME


Start and stop Tomcat using the following scripts.

$ $CATALINA_HOME/bin/startup.sh
$ $CATALINA_HOME/bin/shutdown.sh
The Tomcat logs are written to the "$CATALINA_HOME/logs/" directory by default.

Once Tomcat is started, the following URL should be available. Configuration for the management URLs is discussed below.

http://localhost:8080/
http://localhost:8080/manager/html
http://localhost:8080/manager/status
Remember to open up the port on the firewall if you want to access the site from other servers on the network. Information about the Linux firewall is available here.

Checking the Status of Tomcat

There are several ways to check the status of the service.

$ netstat -nlp | grep 8080
tcp        0      0 :::8080                     :::*                        LISTEN      19034/java


$ ps -ef | grep tomcat
root      3198  3062  0 14:56 pts/0    00:00:00 su - tomcat
tomcat    3199  3198  0 14:56 pts/0    00:00:00 -bash
tomcat    3601     1  6 15:08 pts/0    00:00:02 /home/tomcat/jdk1.8.0_77/bin/java -Djava.util.logging.config.file=/home/tomcat/apache-tomcat-7.0.68/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/home/tomcat/apache-tomcat-7.0.68/endorsed -classpath /home/tomcat/apache-tomcat-7.0.68/bin/bootstrap.jar:/home/tomcat/apache-tomcat-7.0.68/bin/tomcat-juli.jar -Dcatalina.base=/home/tomcat/apache-tomcat-7.0.68 -Dcatalina.home=/home/tomcat/apache-tomcat-7.0.68 -Djava.io.tmpdir=/home/tomcat/apache-tomcat-7.0.68/temp org.apache.catalina.startup.Bootstrap start
tomcat    3631  3199  0 15:09 pts/0    00:00:00 ps -ef
tomcat    3632  3199  0 15:09 pts/0    00:00:00 grep --color=auto tomcat
$

$ curl -I http://localhost:8080
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=ISO-8859-1
Transfer-Encoding: chunked
Date: Sat, 19 Mar 2016 15:09:53 GMT

$


The status is also available from the HTML management page.

Configuration Files

The main locations of configuration and log information are shown below.

Release Notes      : $CATALINA_HOME
Config                  : $CATALINA_HOME/conf
Bin Directory       : $CATALINA_HOME/bin
Webapps              : $CATALINA_HOME/webapps
Logs                     : $CATALINA_HOME/logs
Enabling HTML Management Access

Edit the "$CATALINA_HOME/conf/tomcat-users.xml" file, adding the following entries inside "tomcat-users" tag. Adjust the password as required.

<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="MyPassw0rd!" roles="manager-gui,admin-gui"/>
Restart Tomcat for the configuration to take effect.

$ $CATALINA_HOME/bin/shutdown.sh
$ $CATALINA_HOME/bin/startup.sh
The management application is now available from the "http://localhost:8080/manager/html" URL.

Deploying Applications

You can get a sample application WAR file to test with from "http://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/".

If this is a redeployment, delete the existing deployment from the "$CATALINA_HOME/webapps" directory.

# rm -Rf $CATALINA_HOME/webapps/sample
Place the "sample.war" file in the "$CATALINA_HOME/webapps" directory and Tomcat with automatically deploy it. You will see a "sample" directory appear.

You don't need to stop and start Tomcat for this to work, but you can if you want.

$ $CATALINA_HOME/bin/shutdown.sh
$ $CATALINA_HOME/bin/startup.sh

Start Tomcat and Apache at Boot Time Make sure there is a valid user "tomcat" and that this user has rw permissions in the $CATA...