Automate Apache Tomcat Installation on Linux Server Using Shell Script


My work involves installing & managing a lot of Linux servers. At times I have to do same work on numerous servers. One such task is installing Tomcat. In these posts I showed you how to install Apache Tomcat & how to check its version on Linux servers.
In this post, I will show you how you can automate the installation process across any number of servers using a shell script πŸ™‚

My approach is to : –

  • Setup password-less SSH on all servers from one jumpbox machine. Jumpbox is a server from which you will execute this script.
  • Create a list of servers in a text file. All servers are typed line by line.
  • Copy Java & Tomcat installers to all servers in one go from this jumpbox.
  • And finally execute the script.

Below are the commands & script through which I accomplished this. All of these are self-explanatory πŸ˜‰

for hst in `cat /home/shashank/hosts.txt`; do scp ~shashank/jdk-8u25-linux-x64.tar.gz $hst:/home/shashank; done
for hst in `cat /home/shashank/hosts.txt`; do scp ~shashank/apache-tomcat-8.0.15.tar.gz $hst:/home/shashank; done

What it does above? For loop reads the contents of text file (server IPs) line by line & copies tarballs there.

for hst in `cat /home/shashank/hosts.txt`; do ssh $hst -t 'bash -s' < ~shashank/tomcat_installer.sh; done

The trick above is to use ‘bash -s’. It will accept the script as an argument & execute it. After execution, it will move to next server. Below is my installer script.

Do let me know if it was helpful or not πŸ™‚ I will come up with some more posts soon πŸ™‚

#Apache Tomcat Installer Script
#Author : Shashank Srivastava
#set -x
echo "Logged into `hostname`. Installing here."
#Checking if installer tarballs are present or not. If they are not found, script will print error message & quit.
if [ -f /home/shashank/jdk-8u25-linux-x64.tar.gz ] && [ -f /home/shashank/apache-tomcat-8.0.15.tar.gz ]
then
echo "Unpacking Java installer tarball.......";
sleep 2s
#waiting for 2 seconds to show you what is being done.
sudo tar -xzvf /home/shashank/jdk-8u25-linux-x64.tar.gz;
echo ""
echo "Java tarball unpacked.";
echo ""
echo "Unpacking Tomcat installer tarball.......";
sleep 2s;
sudo tar -xzvf /home/shashank/apache-tomcat-8.0.15.tar.gz;
echo ""
echo "Tomcat tarball unpacked.";
echo ""
echo "Installing Java & Tomcat to /opt/app directory....";
sudo cp -rp /home/shashank/jdk1.8.0_25 /opt/app;
sudo cp -rp /home/shashank/apache-tomcat-8.0.15 /opt/app;
echo ""
echo "Exporting necessary variables......";
export JAVA_HOME=/opt/app/jdk1.8.0_25;
export PATH=$PATH:/opt/app/jdk1.8.0_25/bin:$PATH;
export CATALINA_HOME=/opt/app/apache-tomcat-8.0.15;
echo "Variables exported.";
echo ""
echo $JAVA_HOME;
echo ""
echo $PATH;
echo ""
echo $CATALINA_HOME;
sleep 2s;
echo ""
echo "Checking Java & Tomcat versions.";
echo ""
echo "Java is installed at `which java` Directory";
echo ""
java -version;
echo ""
java -cp $CATALINA_HOME/lib/catalina.jar org.apache.catalina.util.ServerInfo;
echo ""
echo "Starting Tomcat server"
echo ""
cd $CATALINA_HOME/bin
./startup.sh
echo "'
echo "Removing unpacked tarballs from PWD.";
sudo rm -rf /home/shashank/jdk1.8.0_25;
sudo rm -rf /home/shashank/apache-tomcat-8.0.15;
else
echo "Installer tarballs not found in /home/shashank. Please make sure they exist there. Exiting installation process now."
exit
fi
Advertisements

3 thoughts on “Automate Apache Tomcat Installation on Linux Server Using Shell Script

  1. Johnson says:

    Hi Shashank… this one is a pretty good blog that you have here.. Liked the way you have presented the topics in here.. I’m practically a newbie to linux and I want to setup a lab at home and planning to have it setup on VMWare Workstation. How many VM’s should I be setting up for practice considering that I’m interested to take up Certification in RHEL7 and need a lot of practice.

    I’m currently running the VMWare workstation on Win 7 and have the following sys. setup,
    Win 7 64 bit, Intel core i3 cpu, 8 GB RAM.

    Some advise would be good… Thank you πŸ™‚

    Liked by 1 person

  2. Hi Johnson,

    Thanks for your kind words πŸ™‚ I am glad that you liked my blog.

    My setup at home consists of 2 RHEL 6.2 VMs & 1 Windows 2008 Server VM running on Oracle Virtual Box. I personally find Virtual Box slightly better than VMware Workstation (may be because my laptop is an old one with only 3 GB of RAM)

    Since you have a pretty decent configuration, I would suggest you to have atleast 3-4 Linux VMs & 1 Windows VM. With this, you will be able to perform/simulate almost anything that we do in production environment. You can setup Redhat cluster, Webserver, Mail server, FTP, DNS, Samba & many other things that are there in Certification.You will find these topics on this blog itself. On top-right corner of the page, you will find Menu option (those 3 buttons that say Widgets, Search). There is a separate Page for these How To tutorials. Below is link for your reference.
    https://watilearnd2day.wordpress.com/category/how-tos/

    I hope you will find the blog useful for you πŸ™‚ If you need any help with Linux, feel free to contact me πŸ™‚ I’ll be more than happy to help you πŸ™‚ Good luck for your certification πŸ™‚

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s