Showing posts with label linux administration. Show all posts
Showing posts with label linux administration. Show all posts

Monday, October 17, 2011

Using mod_jk with Apache 2.2 How to setup a reverse proxy



Advice on mod_jk versus mod_proxy and mod_proxy_ajp
Do not use mod_proxy and mod_proxy_ajp on Apache 2.2 since they are not stable and have caused me some problems. Use mod_jk instead.

Prerequisites & Assumptions Before you continue
I assume you have:
- You already have Apache Webserver 2.2 installed
- You already have Tomcat installed
- You already have a web application on Tomcat with context "jobsapp"
- You are using Redhat Enterprise or CentOS and therefore have yum on your system.

How to get the mod_jk binary for Apache 2.2 on Linux
You wont find it anywhere, you have to compile it, now stop being lazy and start working.

The following steps detail the process:

// Create a temporary source directory, I use /opt/src
mkdir /opt/src
cd /opt/src 


// Get the source package
wget http://mirrors.isu.net.sa/pub/apache//tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.32-src.tar.gz


// Install packages needed to compile the binary
yum install httpd-devel
yum install gcc-c++


// Extract the tar.gz file
tar -zxvf /opt/src/tomcat-connectors-1.2.32-src.tar.gz


// Configure, make, make install
./configure --with-apxs=/usr/sbin/apxs
make
su -c 'make install'

How to configure Apache Webserver with Mod JK

1. Edit the file httpd.conf
vim /etc/httpd/conf/

2. Add this line to load the module:
...
LoadModule jk_module modules/mod_jk.so
...

3. Add also the following lines:

JkWorkersFile   conf/worker.properties
JkLogFile       /var/log/httpd/mod_jk.log
JkLogLevel      debug
JkRequestLogFormat      "%w %U %T"
JkOptions +ForwardURIEscaped +ForwardURICompatUnparsed

After finishing, change JkLogLevel from debug to "error" or "info" so that the log file doesn't grow too much in size.

4. Now create a file names worker.properties inside /etc/httpd/conf/ and add the following lines to it:
vim /etc/httpd/conf/worker.properties

# Worker Properties file for mod_jk
worker.list=tomcat
worker.tomcat.host=localhost
worker.tomcat.port=8009
worker.tomcat.type=ajp13
worker.tomcat.connection_pool_timeout=300




4.5. Make sure to read the mod_jk attributes in this link:
http://tomcat.apache.org/connectors-doc/reference/workers.html

5. Now open the httpd.conf or your virtual host configuration file and add the following lines:

JkMount     /jobsapp tomcat
JkMount     /jobsapp/* tomcat

6. Also open the ssl httpd conf file which has the following line at the top,
and add the same two lines inside them:

<VirtualHost _default_:443>
...
JkMount     /jobsapp tomcat
JkMount     /jobsapp/* tomcat
...
</VirtualHost>


7. Now restart apache

/etc/init.d/httpd restart

Sunday, October 09, 2011

Making a bash script know its current directory location

I use the following variables at the beginning of my Bash script to get such information:

EXECUTED_FROM_DIR=`pwd`
THIS_SCRIPT_DIR=$(cd $(dirname "$0"); pwd)
cd $THIS_SCRIPT_DIR


#Do different tasks here
...


cd $EXECUTED_FROM_DIR

Bash Tips

To check if a file exists (The exclamation ! means "not") :
if [ ! -f ${TOMCAT_DIR}/conf/jobsapp.conf ]
then
  echo "Do something"
fi


To check if a script should be run by a specific user:
if [ "$(id -u -n)" != "root" ]; then
   echo "This script must be run as root user" 1>&2
   exit 1
fi

To prompt a user for an action:

read -p "Deploy on Tomcat (y/n)?" CONT
if [ "$CONT" == "y" ]; then
  echo "Deploying on tomcat...";
  echo "Finished deploying on tomcat.";
else
  echo "Copied files, but didn't deploy on tomcat.";
fi


Tracking an /etc directory using GIT version control while preserving file permissions

.. while preserving permissions on the linux file system.

Here's how to do it:
1. Download the post hook file setgitperms from here or directly from here http://repo.or.cz/w/git.git/blob_plain/HEAD:/contrib/hooks/setgitperms.perl

2. Place the script inside your .git/hooks directory. You have two options here:

(Option A) To apply this for all git repositories newly created
Copy the file setgitperms.perl inside the directory:

mv setgitperms.perl /usr/share/git-core/templates/hooks/.

(Option B) To apply this hook for a specific directory only
Copy the file setgitperms.perl inside the directory:
mv setgitperms.perl <Your Project Directory>/.git/hooks/.

3. Although git new releases handles this, just to be safe, make sure to add execute permissions for the hook:
chmod +x setgitperms.perl

4. Inside the hooks directory, create a file named "pre-commit" and place in it the following lines:

#!/bin/sh
SUBDIRECTORY_OK=1 . git-sh-setup
$GIT_DIR/hooks/setgitperms.perl -r


5. Create file named post-merge and post-checkout and place the following content inside each:

#!/bin/sh
SUBDIRECTORY_OK=1 . git-sh-setup
$GIT_DIR/hooks/setgitperms.perl -w


6. Save the files and make sure they are executable using chmod +x:
chmod +x post-merge
chmod +x post-checkout
chmod +x pre-commit

7. Thats it.

Reference: http://serverfault.com/questions/5410/using-revision-control-for-server-configuration-files

Saturday, October 08, 2011

Finding ports and processes running on linux port 80

The command:

# To find processes running on port 80
lsof -i :80