Showing posts with label Tomcat. Show all posts
Showing posts with label Tomcat. Show all posts

Friday, July 17, 2015

1. Check if the correct version of Java is installed

JAVA is required for Tomcat to work, CentOS 7 comes with JAVA 7 witch is the needed version. To check if it is installed run:
# java -version
java version "1.7.0_55"
OpenJDK Runtime Environment (rhel-2.4.7.2.el7_0-x86_64 u55-b13)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
In case java is not installed on your system or you have version 1.6.x, you can install it by running:
# yum install java-1.7.0-openjdk.x86_64 java-1.7.0-openjdk-devel

2. Download Tomcat

You can find the latest version of  Tomcat at its download page. You can download it with wget and extract it with tar like this:
# cd /usr/share
# wget http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.9/bin/apache-tomcat-8.0.9.tar.gz
# tar zxvf apache-tomcat-8.0.9.tar.gz

3. Add tomcat user & group

As it is not recommended to run Tomcat as root we will need to create an unprivileged user for it and set the apropiate owner of the tomcat folder:
# groupadd tomcat
# useradd -g tomcat -s /bin/bash -d /usr/share/apache-tomcat-8.0.9 tomcat
# chown -Rf tomcat.tomcat /usr/share/apache-tomcat-8.0.9/

4. Running Tomcat

To start tomcat we will first need to switch to the unprivileged user with:
# su – tomcat
And starting tomcat is as easy as running its startup script like this:
$ cd bin
$ ./startup.sh
The output should look like:
Using CATALINA_BASE: /usr/share/apache-tomcat-8.0.9
Using CATALINA_HOME: /usr/share/apache-tomcat-8.0.9
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-8.0.9/temp
Using JRE_HOME: /
Using CLASSPATH: /usr/share/apache-tomcat-8.0.9/bin/bootstrap.jar:/usr/share/apache-tomcat-8.0.9/bin/tomcat-juli.jar
Tomcat started.
You should now be able to access it with a browser by either accessing http://localhost:8080 if this is a local computer or http://SERVER-IP:8080 if you are running it on a remote host.
Tomcat 8 on CentOS 7
To shutdown Tomcat you can simply run the shutdown script in the same folder like this:
$ ./shutdown.sh

5. Setup user accounts

Finally you have to configure Tomcat users so they can access admin/manager sections. You can do this by adding the users in the conf/tomcat-users.xml file with your favorite text editor. Add this text to the file:
<user username="manager" password="PASSWORD" roles="manager-gui" />
<user username="admin" password="PASSWORD" roles="manager-gui,admin-gui" />

6. Running Multiple Instances of Tomcat (Optional)

Sometimes you need to run more than one instance of Tomcat on the same server. To do this, as root, go back to the /usr/share directory where you first downloaded tomcat and extract it again in a different folder like this:
# cd /usr/share
# mkdir apache-tomcat-2
# tar zxvf apache-tomcat-8.0.9.tar.gz -C apache-tomcat-2 --strip-components 1
# chown -Rf tomcat.tomcat /usr/share/apache-tomcat-2/
Now we need to open the config/server.xml file in the new installation folder and change the port numbers like this:
The shutdown port from:
<Server port="8005" shutdown="SHUTDOWN">
to:
<Server port="8006" shutdown="SHUTDOWN">
The connector port from:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
to:
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
And the AJP port from:
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
to:
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
Now you can just switch back to the tomcat user and start the second instance like this:
# su - tomcat
$ cd /usr/share/apache-tomcat-2/bin/
$ ./startup.sh
Using CATALINA_BASE: /usr/share/apache-tomcat-2
Using CATALINA_HOME: /usr/share/apache-tomcat-2
Using CATALINA_TMPDIR: /usr/share/apache-tomcat-2/temp
Using JRE_HOME: /
Using CLASSPATH: /usr/share/apache-tomcat-2/bin/bootstrap.jar:/usr/share/apache-tomcat-2/bin/tomcat-juli.jar
Tomcat started.
You can now access the new instance of tomcat with your browser at http://localhost:8081/
Congratulation you now have Tomcat installed and ready to use!

Tuesday, October 8, 2013

Tomcat configurations

How to Install Tomcat on Linux ?


# Download and Install JAVA
Download j2sdk-<version> from Sun Download center http://developers.sun.com/downloads/
# chmod +x j2sdk-1_4_2_09-linux-i586.bin
# ./j2sdk-1_4_2_09-linux-i586.bin
Now set java home path
# vi .bash_profile
JAVA_HOME=/usr/local/j2sdk
Now Check if Java is installed on the server using command java -version
[root@map ~]# java -version
java version “1.6.0_07?
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)
# Download Tomcat
Now Download Tomcat from Apache Website and extract it
http://tomcat.apache.org/download-55.cgi
# cd /usr/local/
#wget http://www.poolsaboveground.com/apache/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz
# tar -zxvf apache-tomcat-6.0.18.tar.gz
Create Symlink for the Tomcat Folder
# ln -s /usr/local/apache-tomcat-6.0.18 /usr/local/apache/tomcat
# Install Tomcat
# cd apache-tomcat-6.0.18
# cd bin
# tar xvfz jsvc.tar.gz
# cd jsvc-src
# chmod +x configure
# ./configure
# make
# cp jsvc ..
# cd ..
Now set Cataline home path
# vi .bash_profile
CATALINA_HOME=/usr/loca/tomcat
JAVA=/usr/loca/j2sdk/bin
Save and exit
# Type following commands
# export JAVA_HOME=/usr/local/j2sdk
# export JAVA=/usr/local/j2sdk/bin
# export CATALINA=/usr/local/tomcat
# Start Tomcat
Use Following script to start Tomcat Service on the Server
# /usr/local/apache/tomcat/bin/startup.sh
# Running Tomcat as non root user
Due to security reasons always run tomcat as non-root user i.e. tomcat. To run it as tomcat first you will have to change the ownership of the tomcat folder
# chown -R tomcat.tomcat /usr/local/apache-tomcat-6.0.18
Now Tomcat can be stopped and started under user tomcat using following commands:
# su -l tomcat -c /usr/local/apache/tomcat/bin/startup.sh
# su -l tomcat -c /usr/local/apache/tomcat/bin/shutdown.sh
# Test Tomcat installation
open a browser and browse website http://xx.xx.xx.xx:8080 where xx.xx.xx.xx will be your Server IP and If you get Tomcat page than Tomcat has been installed properly on the Server.
# Creating Script to start, stop and restart Tomcat
The above installation step will not create tomcat service so that user can restart tomcat using command service tomcat restart. Create a new file in /etc/init.d as tomcat and copy following contenents into it.
# vi /etc/init.d/tomcat
#!/bin/bash
#
# Startup script for Tomcat
#
# chkconfig: 345 84 16
# description: Tomcat jakarta JSP server
# Necessary environment variables
export CATALINA_HOME=”/usr/local/tomcat”
if [ ! -f $CATALINA_HOME/bin/catalina.sh ]
then
echo “Tomcat not available…”
exit
fi
start() {
echo -n -e ‘\E[0;0m'"\033[1;32mStarting Tomcat: \033[0m \n"
su -l tomcat -c $CATALINA_HOME/bin/startup.sh
echo
touch /var/lock/subsys/tomcatd
sleep 3
}
stop() {
echo -n -e '\E[0;0m'"\033[1;31mShutting down Tomcat: \033[m \n"
su -l tomcat -c $CATALINA_HOME/bin/shutdown.sh
rm -f /var/lock/subsys/tomcatd
echo
}
status() {
ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap start” | awk ‘{printf $1 ” “}’ | wc | awk ‘{print $2}’ > /tmp/tomcat_process_count.txt
read line < /tmp/tomcat_process_count.txt
if [ $line -gt 0 ]; then
echo -n “tomcatd ( pid ”
ps ax –width=1000 | grep “[o]rg.apache.catalina.startup.Bootstrap start” | awk ‘{printf $1 ” “}’
echo -n “) is running…”
echo
else
echo “Tomcat is stopped”
fi
}
case “$1? in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
status)
status
;;
*)
echo “Usage: tomcatd {start|stop|restart|status}”
exit 1
esac
Now save and exit from the file. Now assign executable permission to this file
# chown 755 /etc/init.d/tomcat
Enable it for all the Run-levels
# chkconfig –add tomcat
# chkconfig tomcat on
Now you can restart tomcat service using following commands.
# service tomcat restart <<< To restart tomcat
# service tomcat stop <<< To stop Tomcat
# service tomcat start <<< To start Tomcat
# service tomcat Status <<< to check the status of Tomcat
Now you have successfully installed Tomcat.


Tomcat with Apache Integration on Linux server


# Installing and configuring mod_jk

To make connection between Tomcat and Apache, you will need to download and install mod_jk connector.
I chose to download the current source from the Apache archives:- http://archive.apache.org/dist/jakarta/tomcat-connectors/jk/source/jk-1.2.15/
[root@map007 /]# cd /usr/local
[root@map007 local]# wget http://archive.apache.org/dist/jakarta/tomcat-connectors/jk/source/jk-1.2.15/jakarta-tomcat-connectors-1.2.15-src.tar.gz
[root@map007 local]# tar -zxvf jakarta-tomcat-connectors-1.2.15-src.tar.gz
Change to the /usr/local/jakarta-tomcat-connectors-1.2.15-src/jk/native directory.
[root@map007 local]# cd jakarta-tomcat-connectors-1.2.15-src/jk/native
Now you are ready to create the custom configure file for your system. Execute the following:
[root@map007 local]# ./buildconf.sh
[root@map007 local]# ./configure –with-apxs=/usr/bin/apxs2
Now build the mod_jk with the following:
[root@map007 local]# make
Finally, if you were successful with the previous commands, copy the newly created mod_jk.soto your Apache2 modules directory. My modules were located at /usr/lib/apache2/modules.
[root@map007 /]# cp /usr/src/jakarta-tomcat-connectors-1.2.15-src/jk/native/apache-2.0/mod_jk.so /usr/lib/apache2/modules

# Configuring Tomcat and Apache

[root@map007 /]# vi /etc/apache2/workers.properties
workers.tomcat_home=/usr/lib/apache-tomcat
workers.java_home=/usr/lib/jdk
ps=/
worker.list=worker1
worker.default.port=8009
worker.default.host=localhost
worker.default.type=ajp13
worker.default.lbfactor=1
# Save and close the file.
# Now you have to configure httpd.conf file and following lines.
[root@map007 /]# vi /etc/apache2/httpd.conf
LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile /etc/apache2/workers.properties
# Where to put jk logs
JkLogFile /var/log/apache2/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the log format
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] “
Save and close the file.
Now a final security point.
[root@map007 /]# chown -R tomcat:tomcat /usr/lib/apache-tomcat-5.5.16
# Now restart the Tomcat.
[root@map007 /]# cd /usr/local/tomcat/bin
[root@map007 bin]# ./shutdown.sh
[root@map007 bin]# ./startup.sh
And restart Apache:
[root@map007 /]# /etc/init.d/apache2 restart
You have Done all the Configurations.
Note :- Start Tomcat first: Always start Tomcat first and then start Apache.

References and paths:

Tomcat conf:
/usr/lib/apache-tomcat/conf/server.xml
Apache modules:
/usr/lib/apache2/modules
Apache conf:
/etc/apache2/workers.properties
/etc/apache2/httpd.conf
Tomcat stop and start:
cd /usr/local/apache-tomcat/bin
./shutdown.sh
./startup.sh
Apache2:
/etc/init.d/apache2 restart
/etc/init.d/apache2 stop
/etc/init.d/apache2 start