Tricks and Tips about Systems/Network

August 26, 2008

How to redirect the page within your php application

Filed under: Apache,Debian,Linux,RedHat EL5,Ubuntu — Liju Mathew @ 10:40 pm

PHP Redirect Script

You can easily redirect using following code:

 
<?php
/* Redirect browser */
header("Location: http://theos.in/");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>

Another sample hack

Sample function – sitefunctions.php (note I’m not the author of the following I found it somewhere else on the Internet):

function movePage($num,$url){
   static $http = array (
       100 => "HTTP/1.1 100 Continue",
       101 => "HTTP/1.1 101 Switching Protocols",
       200 => "HTTP/1.1 200 OK",
       201 => "HTTP/1.1 201 Created",
       202 => "HTTP/1.1 202 Accepted",
       203 => "HTTP/1.1 203 Non-Authoritative Information",
       204 => "HTTP/1.1 204 No Content",
       205 => "HTTP/1.1 205 Reset Content",
       206 => "HTTP/1.1 206 Partial Content",
       300 => "HTTP/1.1 300 Multiple Choices",
       301 => "HTTP/1.1 301 Moved Permanently",
       302 => "HTTP/1.1 302 Found",
       303 => "HTTP/1.1 303 See Other",
       304 => "HTTP/1.1 304 Not Modified",
       305 => "HTTP/1.1 305 Use Proxy",
       307 => "HTTP/1.1 307 Temporary Redirect",
       400 => "HTTP/1.1 400 Bad Request",
       401 => "HTTP/1.1 401 Unauthorized",
       402 => "HTTP/1.1 402 Payment Required",
       403 => "HTTP/1.1 403 Forbidden",
       404 => "HTTP/1.1 404 Not Found",
       405 => "HTTP/1.1 405 Method Not Allowed",
       406 => "HTTP/1.1 406 Not Acceptable",
       407 => "HTTP/1.1 407 Proxy Authentication Required",
       408 => "HTTP/1.1 408 Request Time-out",
       409 => "HTTP/1.1 409 Conflict",
       410 => "HTTP/1.1 410 Gone",
       411 => "HTTP/1.1 411 Length Required",
       412 => "HTTP/1.1 412 Precondition Failed",
       413 => "HTTP/1.1 413 Request Entity Too Large",
       414 => "HTTP/1.1 414 Request-URI Too Large",
       415 => "HTTP/1.1 415 Unsupported Media Type",
       416 => "HTTP/1.1 416 Requested range not satisfiable",
       417 => "HTTP/1.1 417 Expectation Failed",
       500 => "HTTP/1.1 500 Internal Server Error",
       501 => "HTTP/1.1 501 Not Implemented",
       502 => "HTTP/1.1 502 Bad Gateway",
       503 => "HTTP/1.1 503 Service Unavailable",
       504 => "HTTP/1.1 504 Gateway Time-out"
   );
   header($http[$num]);
   header ("Location: $url");
}

Now call it as follows:

<?php
@include("/path/to/sitefunctions.php");
movePage(403,"http://www.cyberciti.biz/");
?>

July 30, 2008

How to run mulitple Tomcat instance on the same server

Filed under: Apache,Debian,Linux,RedHat EL5,Ubuntu — Liju Mathew @ 10:41 pm
Tags:

I am listing the steps to  archive the goal

1, Step 1: Install the Tomcat files

2, Make directories for each instance

3. Editing the server.xml

Tomcat  using  2 major port one for listening http request and another for listening shutdown port

in here i put tomcat installation on /usr/local/tomcat5.5.9 folder

now I am going to edit server.xml inside the conf folder

vi /usr/local/tomcat5.5.9 folder/conf/server.xml

Find the entry

a, <Server port=”8001″ shutdown=”SHUTDOWN”> and changed the server port to any unused port

b, <!– Define a non-SSL HTTP/1.1 Connector on port 8080 –>
<Connector port=”9000″ maxHttpHeaderSize=”8192″  Change the Connector port to any unused port which tomcat listen

Again  restart the tomcat and check the that it is listening in the connector port by

!, telnet localhost   <connector port >  if it is opend a blank window it work

2, exicute $ netstat -nlp| grep  :”<connector port> .If it shows any result it’s working

Now you can start to depoly  the website.

I recommended that always to use tomcat as a backend server for Apache. You can use proxy pass module for  this functionality in apache. It also included in my previous  blog in Apache category.

################ Tomcat start up scripts ###########

# This is the init script for starting up the
#  Jakarta Tomcat server
#
# chkconfig: 345 91 10
# description: Starts and stops the Tomcat daemon.
#

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

# Get config.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

tomcat=/usr/local/jakarta-tomcat-5.5.9
startup=$tomcat/bin/startup.sh
shutdown=$tomcat/bin/shutdown.sh
export JAVA_HOME=/usr/java/jdk1.5.0_06

start(){
echo -n $”Starting Tomcat service: “
#daemon -c
$startup
RETVAL=$?
echo
}

stop(){
action $”Stopping Tomcat service: ” $shutdown
RETVAL=$?
echo
}

restart(){
stop
start
}

# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
status)
# This doesn’t work ;)
status tomcat
;;
restart)
restart
;;
*)
echo $”Usage: $0 {start|stop|status|restart}”
exit 1
esac

exit 0
####### end of scripts ########
How to   use this scripts

1, open in any editor and  change the path of java and  Tomcat install location

2, Move the scripts to the /etc/init.d

3, Give execute permission to the scripts, #chmod  755 /etc/init.d/tomcatd  and then execute the scripts for the testing. Eg: # ./etc/init.d/tomcat start

4, Setting runlevel and added it to system service list by # chkconfig –add /etc/init.d/tomcatd

5. Setting the tomcat  should be run at start up by #chkconfig tomcatd   on

That’s it

Liju

July 28, 2008

How to monitor SSH Login activities and FTP logins over the web

Filed under: Debian,Linux,RedHat EL5,Ubuntu — Liju Mathew @ 8:48 pm

Basic Idea behind scripts is,

We can able to monitor the activities of ssh logins,any hacking attempt and ftp logins. The steps are involved in this scripts is

1, first check the log log files for whether there is any entry is present

2, If any lines present in the logs regarding the login activities, write all those lines in our custom web access file.

3, Place the report file in a web accessable location

4, Create a virtual host on Apache for this

5, set a cron to automate the logs creation and removal

########################### Log monitoring scripts ##############

#!/bin/bash
#fingers: Liju mathew ~! lijumathewliju@gmail.com
#created: 27.07.2008
#purpose: to check the status of ssh/logins and notify the system personnel
#      if they are not listening!

today=`date +%d-%m-%y`
day_ftr=`date +%b” “%d`
web_location=/var/www/logs

# In the below example I guess the ssh log files are created in /var/log/secure (on debian /var/lig/auth.log) #and vsftp/proftp logs are created on /var/log/vsftp.log
## SSH Allow script for checking empty data from the file

/bin/cat /var/log/secure| grep “$day_ftr”| grep sshd | grep “Accepted password” &> /dev/null
if [ "$?" -eq "0"  ];then
/bin/cat /var/log/secure | grep sshd | grep “Accepted password” > $web_location/ssh-allow-$today.txt
fi

#SSH -Deny scripts for removing empty file creation
/bin/cat /var/log/secure |grep “$day_ftr” | grep “sshd” | grep “Failed password”$> /dev/null
if [ "$?" -eq "0"  ];then
/bin/cat /var/log/secure |grep “$day_ftr” | grep “sshd” | grep “Failed password” > $web_location/ssh-deny-$today.txt
fi
## FTP Scripts for avioding empty file creation
/bin/cat /var/log/xferlog | grep “$day_ftr” &> /dev/null
if [ "$?" -eq "0"  ];then
/bin/cat /var/log/xferlog | grep “$day_ftr” > $web_location/ftp-log-$today.txt
fi
### ############End of scripts ############

Create a virtual host entry for this

<VirtualHost *:80>

ServerName 182.163.98.2

Alias /logs  “/var/www/html/logs”

<Directory “/var/www/html/logs” >
Options +Indexes
AllowOverride All
Allow from all
Order allow,deny
</Directory>

<Location /logs>
AuthType Basic
AuthName “Admin access details “
AuthUserFile /var//pwd/prod/passwd
Require valid-user
Allow from all
</Location>

</VirtualHost>

3, Set up a cron for o this

copy this scripts to /var/opt/scripts/ssh_monitor.sh

30 12  * * * /bin/sh /var/opt/scripts/ssh-monitor.sh

That’s it

Note : pls make sure that log file is properly formatted with my  grep comand. If not. pls update it with your required strings.

July 24, 2008

Memory inspection

Filed under: Debian,Linux,PostgresSQL,RedHat EL5,Ubuntu — Liju Mathew @ 8:53 pm

A, Cheking  memory size and avialbale ususge

$free -m

B, What is the maximum RAM supported by the system?

$ dmidecode -t 16

C, How many memory slots are available for expansion?
$ dmidecode -t 17 | grep Size

D, Detailed memory information

$ cat /proc/meminfo

July 6, 2008

Throttle The Disk I/O Rate: Limit disk I/O for rsync Tool

Filed under: Debian,Linux,RedHat EL5 — Liju Mathew @ 8:00 pm

I run a backup script called /root/backup.sh which runs rsync command. However, rsync makes a lots of disk I/O and network I/O. I’d like to reduce both disk I/O and network I/O. I’ve 10Mbps server connection and 160GiB SATA hard disk. How do reduce disk I/O so that the entire system doesn’t die or become unresponsive?

Here is the answer,

Method # 1: Limit I/O bandwidth

The –bwlimit option limit I/O bandwidth. You need to set bandwidth using KBytes per second. For example, limit I/O banwidth to 10000KB/s (9.7MB/s), enter:
# rsync --delete --numeric-ids --relative --delete-excluded --bwlimit=10000 /path/to/source /path/to/dest/

March 13, 2008

Debian-Package Installations

Filed under: Debian — Liju Mathew @ 9:05 pm
Tags:

######### How to install java on debian######################
Add the follwing repository into /etc/apt/sources.list
#Local Mirror
deb ftp://gulus.usherbrooke.ca/debian/ etch main contrib non-free
#deb-src ftp://gulus.usherbrooke.ca/debian/ etch main contrib non-free
#Security
deb http://security.debian.org/ etch/updates main contrib non-free
#deb-src http://security.debian.org/ etch/updates main contrib non-free

#Multimedia, from local mirror
deb ftp://gulus.usherbrooke.ca/debian-multimedia/ etch main
#deb-src ftp://gulus.usherbrooke.ca/debian-multimedia/ etch main

#Beryl
deb http://debian.beryl-project.org etch main
#deb-src http://debian.beryl-project.org etch main

then exicute the follwing commands

sudo apt-get install sun-java5-jre
sudo apt-get install sun-java5-plugin

################ How to install Subversion on Debian ###################

#apt-get install libapache2-svn subversion subversion-tools

##################### How to install VNCsever on Debian ##############
#apt-get install vnc4server

configaration file /etc/vnc.conf
Installing VNC Client is very simple:

#apt-get install xvnc4viewer

Once it’s installed you can connect to a running server by using:

Xvnc4viewer hostname

If invoked with no arguments you’ll be prompted for the host you wish to connect to, and if necessary a password.

################ apache_mod_python installation ##############
download http://mirrors.24-7-solutions.net/pub/apache/httpd/modpython/mod_python-3.3.1.tgz
then install apache extension tool
#apt-get install apache2-threaded-dev
untar and configure it with
# ./configure –with-apxs=/usr/bin/apxs2
######################### Installing Postgressql on debain #######
#apt-get install postgresql postgresql-dev postgresql-client postgresql-doc
enabling php-pgsql

######## Enabling Pgsql module on apache-php module ##########
#apt get install php5-pgsql
################ Debain installation of apache 1.3.34 ################

apt-get install apache
apt-get install apache-common
apt-get install apache-dbg
apt-get install apache-dev
apt-get install apache-doc
apt-get install apache-perl
apt-get install apache-ssl
apt-get install libapache-mod-perl
apt-get install libapache-mod-ssl
apt-get install libapache-mod-php5

######## Ruby on Rails installation on Debian server ######################
apt-get install ruby libzlib-ruby rdoc irb rubygems rails eruby
apt-get install libapache2-mod-fcgid libfcgi-ruby1.8 ruby1.8-dev

Afterwards, we enable a few Apache modules:
a2enmod ssl
a2enmod rewrite
a2enmod suexec

a2enmod include
#/etc/init.d/apache2 force-reload this may help u reload the entire processes
apt-get install libmysql-ruby
apt-get install libpgsql-ruby

######## Optional Libraries ##########
apt-get install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8
# ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby
# ln -s /usr/bin/ri1.8 /usr/local/bin/ri
# ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc
# ln -s /usr/bin/irb1.8 /usr/local/bin/irb
# apt-get install gcc-3.4-doc gcc-3.4 g++-3.4
#apt-get install libfcgi-dev
Now lets install the ruby-fcgi bindings from source so we no we have the good ones:
wget http://sugi.nemui.org/pub/ruby/fcgi/ruby-fcgi-0.8.6.tar.gz
tar xzvf ruby-fcgi*
cd ruby-fcgi-0.8.6
ruby install.rb config
ruby install.rb setup
sudo ruby install.rb install
Now lets make sure it works:
root@example:~/ruby-fcgi-0.8.6# irb
irb(main):001:0> require ‘fcgi’
=> true
irb(main):002:0>

http://brainspl.at/pages/perfect_vps/

apt-get install esvn
apt-get install libmysql-ruby
apt-get install libmysql-ruby1.8
apt-get install libmysqlclient14-dev
gem install mysql
################## Reconfigure X server in Debain ###################
#dpkg-reconfigure xserver-xorg
################# installing MySQL on Debian ################
Install MySQL
#apt-get install mysql-server mysql-client libmysqlclient12-dev
Set your mysql root password
# mysqladmin -u root password yourrootsqlpassword
####### MySQL trouble shooing #######
Symptoms: After installing mysql on Debian using Debain binaries. It won’t allow anyone connect it from remotely even though we have given privileges to the user from MySQL console.
Solution is listed below
a, When you run $netstat -tap you should now see a line like this:

tcp 0 0 localhost.localdo:mysql *:*
which means that MySQL is accessible on 127.0.0.1 on port 3306.
b, If you do not see this line, edit /etc/mysql/my.cnf and comment out skip-networking or remove the file from there
# skip-networking
If you want MySQL to listen on all available IP addresses, edit /etc/mysql/my.cnf and comment out bind-address = 127.0.0.1:
# bind-address = 127.0.0.1:
If you had to edit /etc/mysql/my.cnf you have to restart MySQL:
#/etc/init.d/mysql restart
############### Debian Default fonts location ######################
/usr/share/fonts/truetype
############### Debian SQLite Installation ################3
* Use apt-get install python-sqlite to install pysqlite 1.0.
* Use apt-get install python-pysqlite1.1 to install pysqlite 1.1.
* Use apt-get install python-pysqlite2 to install pysqlite 2.x.

Check the status of a Crontab entry

Filed under: Debian,Linux,RedHat EL5,Ubuntu — Liju Mathew @ 8:44 pm
Tags:

How to check a cronjob is working properly

1.From the terminal exicute
$ crontab -e
* * * * * date >>/home/cronstatus.txt

then save and open the /home/cronstatus.txt using any editor
check the time intervel between the date printed on the file.

Note: the above cron job will execute every second .So you need to be remove from it ASAP after the testing …
Will update the later part shortly…….
That’s it !

March 12, 2008

Installing Apache 1.3 with SSL

Filed under: Debian,Linux — Liju Mathew @ 8:57 pm
Tags:

Installing Apache with SSL support

1. Download openssl source from http://www.openssl.org.
2. untar the source into /usr/local/src
3. cd /usr/local/src/openssl-0.9.7b
4. ./config –prefix=/usr/local/openssl
5. make
6. make install
7. Download apache 1.3.x from http://httpd.apache.org/dist/
8. untar in /usr/local/src/
9. Download modssl from http://www.modssl.org
10. untar the source into /usr/local/src
11. cd /usr/local/src/mod_ssl-2.8.14-1.3.27
12. ./configure –with-apache=../apache_1.3.27/ –prefix=/usr/local/apache –enable-module=so –enable-module=expires –enable-module=rewrite –enable-suexec –suexec-caller=nobody –suexec-docroot=/usr/local/apache/htdocs/ –suexec-logfile=/var/log/suexec_log –with-ssl=/usr/local/openssl
13. cd ../apache_1.3.27
14. make
15. make certificate TYPE=custom
14. make install
16. cd /usr/local/apache/conf/
17. cd ssl.key
18. cp server.key server.key.old
19. /usr/local/openssl/bin/openssl rsa -in server.key.old -out server.key
20. /usr/local/apache/bin/apachectl startssl

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.