Friday, June 15, 2012

How to enable mod_rewrite in centos via SSH

This is a very simple question, but for beginners it is little bit difficult,
Now assuming that everything is installed, open the httpd conf file and start editing it,

You can find this file in
/etc/httpd/conf/httpd.conf
or directly edit it
vi /etc/httpd/conf/httpd.conf
in this file search for Directory “/var/www/html” this can be on line 290-320

You will find a line of code, just like below
AllowOveride None
Just change it to
AllowOveride All
Save it, and exit, mod_rewrite is enabled on your server.

yum install phpmyadmin

I am using CentOS 5 in one of my Virtual Servers and I wanted it to have phpMyAdmin. I tried installing it by doing yum install phpmyadmin but it said No package phpmyadmin available. So I add to add a repo. I discovered that rpmforge repo had this package. To add it, I executed the following commands:
 
wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.i386.rpm
rpm -Uhv rpmforge-release-0.5.1-1.el5.rf.i386.rpm
 
If you have 64-bit version installed, you should try this one out:

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.x86_64.rpm
rpm -Uhv rpmforge-release-0.5.1-1.el5.rf.x86_64.rpm
 
If you get a 404 Not Found, then you will need to figure out the latest updated version of repo url from rpmforge usage page.
After that, you may like to update your packages by doing this:

yum update
 
and then continue installing phpmyadmin

yum install phpmyadmin
 
It should ask you for confirmation and you can continue installing phpMyAdmin with dependencies.
When installation is done, you can edit /etc/httpd/conf.d/phpmyadmin.conf and allow it to be opened from anywhere and not just the same computer. You are going to need this if you have installed it in a remote virtual server.
To do so, open up /etc/httpd/conf.d/phpmyadmin.conf using:

nano /etc/httpd/conf.d/phpmyadmin.conf
You will see:


  Order Deny,Allow
  Deny from all
  Allow from 127.0.0.1
 
Just change the line “Allow from 127.0.0.1″ to “Allow from [yourip]“, where [yourip] is IP address of your computer if you have public static IP. Otherwise, you can also set it to “Allow from all”.
Press Ctrl+O followed by Enter to save and Ctrl+X to exit nano.
Now, you must restart apache. To do so, run

service httpd restart
 
Now, you can access phpMyAdmin by visiting http://vps_server_IP_or_domain/phpmyadmin. But you will get the following error:

Error :
 Forbidden
You don't have permission to access /phpmyadmin/ on this server.


To fix:
nano /etc/httpd/conf.d/phpmyadmin.conf
Remove or comment the first two lines in bold.


#Order Allow,Deny
#Deny from all
Allow from 112.213.90.120
Restart the apache server.
service httpd restart

Error
The configuration file now needs a secret passphrase (blowfish_secret).
To make it work, you will need to edit config.inc.php. To do so, type in:

nano /usr/share/phpmyadmin/config.inc.php
 
Find a line saying:

$cfg['blowfish_secret'] = ''; /* YOU MUST FILL IN THIS
FOR
COOKIE AUTH! */
Fill in any secret keyword there, eg mysecretpassphrase, so that it looks like:
$cfg['blowfish_secret'] = 'mysecretpassphrase'; /* YOU MUST FILL IN THIS
FOR
COOKIE AUTH! */
 
and then save it.
If you don’t have mysql server installed, you will get the following error:
Error
#2002 – The server is not responding (or the local MySQL server’s socket is not correctly configured)
To install mysql-server, just run:

 yum install mysql-server
 
and then start it:

 service mysqld start
 
Then change the root password:

mysqladmin -u root password PASSWORD_HERE
 
Now, you will have to remove phpmyadmin:

yum remove phpmyadmin
 
and reinstall it again:

yum install phpmyadmin
 
Then you will need to edit the config.inc.php again to enter blowfish secret (see above). Then, you can login.
If however, you want phpMyAdmin to connect to a remote server, you can change the line by replacing localhost with your server IP:

$cfg['Servers'][$i]['host'] = 'localhost';
 
Hope this helps.

Monday, June 11, 2012

Thực hành setup MCSE

Setup Trên máy MAY1

Đặt IP và DNS
10.10.10.10
255.255.255.0

DNS: 10.10.10.10
Vì DNS server cài tại MAY1


Đổi tên computer (=> more => DNS suffix) => gõ tên domain vd: store.com (không cần khởi động)

SETUP DNS:
Vào "add remove program" => "Networking services" => cài DNS

Vào DNS:
Tạo "Forward Lookup zone" =>  New Zone =>  (Primary zone)  => điền vào "zone name" tên domain đã setup bên trên vd: store.com

Tạo  "Revert loopkup zone" => để phân giải tên thành domain
Nhập vào dãy IP: 10.10.10

reboot
Vào cmd gõ nslookup để check DNS

C:\Documents and Settings\Administrator>nslookup
Default Server:  dc1.store.com
Address:  10.10.10.10
> dc1
Server:  dc1.store.com
Address:  10.10.10.10
Name:    dc1.store.com
Address:  10.10.10.10
>


Chú ý: Nếu nslookup không thấy được domain thì vào DNS -> Reverse Lookup Zones => xxxxxx Subnet => New Pointer PTR
Dynamic Update => Chọn Nonsecure và secure


nâng AD:
cmd => dcpromo

Chọn option thứ 1 vì mới có 1 DC
Chọn option 1
FULL DND => gõ domain vào vd: store.com

Tự tạo thư mục sysvol để replicate (để mặc định)

Permissions - Chọn option thứ 2
nếu Chọn option 1 khi có winnt 

Đặt pass để restore vd: 1234567


Bắt ngưởi dùng zone vào domain trước khi trứng thực
DNS -> Forward lookup -> click phải chuột domain -> chọn Properties -> Chọn Change Zone Type ->  Check vào "Store the zone in Active Directory"

DNS -> Reverse lookup -> click phải chuột xxxx subnet -> chọn Properties -> Chọn Change Type ->  Check vào "Store the zone in Active Directory"

========================
Setup trên Máy 2:

Đổi IP: 
10.10.10.100
255.255.255.0

DNS: Trỏ về DNS máy 1
Preferred  lấy DNS chính IP máy 1
 10.10.10.10

Alter  lấy DNS chính IP máy 2
vd: 10.10.10.100

Cài DNS:

Join domain
- Đổi tên computer : dc2
- Chọn domain : store.com
  Nhập user/passs account máy1

Khởi động
=> chọn domain store.com

dcpromo

chọn option 2
nhập user/pass của domain máy 1
nhập pass restore => có thể nhập pass khác máy 1
vd: Abc1234567

reboot

Test lại bằng cách mở AD tạo thử một OU  => Kiểm tra lại 2 máy có đồng bộ không


 =================

Cấu hình máy 3
- Đặt IP
10.10.10.200
255.255.255.0

DNS1: 10.10.10.10
DNS2: 10.10.10.100

Join vào domain
Đổi tên máy

Cài đặt dịch vụ DNS
reboot, đăng nhập và

Ủy quyền cho zone vn trên máy 1
Vào DNS -> click phải chuột Store.com -> New Delegation
Nhập : vn
Chọn Browser đến máy 3 -> nhập IP máy 3: 10.10.10.200

Cấu hình lại DNS cho máy 3
- Vào DNS -> New zone -> Primary -> vn.store.vn
- Vào DNS -> Reverse -> 10.10.10
=> Click phải chuột vn.store.com -> New Host
nhập: may3
Nhập  ip máy 3:  10.10.10.200
Check chọn update poiter PTR  (nếu không check thì cấu hình tay chọn vào mục reverse Lookup -> click phải 10.10.10.x ->New Pointer PTR)

Vào cmd gõ DCPROMO
- New domain (option 1)
- Child domain (option 2)
- User/passs
- Child domain: vn
Pass restore: Abc1234567


Saturday, June 9, 2012

Fixing that really irritating perl: warning: Setting locale failed. on OS X leopard

Anytime I’ve been running a perl based script on my leopard box I got this really irritating output with whatever else I was expecting:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
     LC_ALL = "En_US",
     LANG = (unset)
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
With a quick Google search I found an answer in this Rubify post. Basically the solution / fix is to make sure the following is set in either your ~/.profile or ~/.bashrc or ~/.cshrc
 
# This setting is for the new UTF-8 terminal support
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

Wednesday, June 6, 2012

Create Auto kill mysql slow

Auto kill mysql slow

use DBI;
use strict;

use POSIX qw(setsid);

# daemonize the program
&daemonize;

$| = 1;

#------------------------------------------------------------------------------
# !!! Configure check time and timeout. These are both in seconds.

my $check =      5;    # check processes every $check seconds
my $slow_time =  30;    # stop processes that run for >= $slow_time seconds

# !!! Configure log file - All slow queries also get logged to this file

my $logfile =    "/var/log/check_mysql_query.log"; # log slow queries to this file

# !!! Configure the database connection parameters

my $db_string = "dbi:mysql:mysql";  # DBI resource to connect to
my $db_user =   "root";     # DBI username to connect as
my $db_pass =   "root";     # DBI password to connect with

# !!! Configure path to sendmail program

my $sendmail_bin = "/usr/sbin/sendmail";

#
#------------------------------------------------------------------------------


my ($dbh,$sth,$sth2,$thread,$state,$time,$query,$explain);

print "connecting\n";
my $opt = {
    'RaiseError'=>0,
    'PrintError'=>0
};
$dbh = DBI->connect($db_string,$db_user,$db_pass,$opt);
unless ($dbh) {
    print "Error: Unable to connect to database: $DBI::errstr\n";
    exit 1;
}

$SIG{'TERM'} = sub {
    print "caught sig TERM!\nexiting!\n";
    $dbh->disconnect;
    exit 1;
};

print "preparing\n";
unless ($sth = $dbh->prepare("show full processlist")) {
    print "error preparing query: $DBI::errstr\nexiting!\n";
    $dbh->disconnect;
    exit 1;
}

print "initialized.. starting loop\n";
while(1) {
    unless ($sth->execute) {
        print "statement execute failed: ".$sth->errstr."\nexiting!\n";
        last;
    }
    while(my @tmp = $sth->fetchrow) {
        $thread = $tmp[0];
        $state = $tmp[4];
        $time = $tmp[5];
        $query = $tmp[7];
        if ($state eq "Query" && $query =~ /^SELECT/ && $time >= $slow_time) {
            print "killing slow query thread=$thread state=$state time=$time\n";
            $dbh->do("kill $thread");
            unless (log_query($logfile,$query)) {
                print "log_query failed! exiting!\n";
                last;
            }


        }
    }
    sleep($check);
}

$sth->finish;
$dbh->disconnect;

exit 1;

sub log_query {
    my ($file,$query) = @_;
    unless (open(O,">>".$file)) {
        print "error opening log file '$file': $!\n";
        return undef;
    }
    print O $query."\n-----\n";
    close(O);
    return 1;
}

sub daemonize {
    chdir '/'or die "Can't chdir to /: $!";
    open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
    open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
    open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
    defined(my $pid = fork) or die "Can't fork: $!";
    exit if $pid;
    setsid or die "Can't start a new session: $!";
    umask 0;
}

Create Simple Daemon for linux

Simple Daemon: Listing 1

use POSIX qw(setsid);
chdir '/'                 or die "Can't chdir to /: $!";
umask 0;
open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
#open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!";
defined(my $pid = fork)   or die "Can't fork: $!";
exit if $pid;
setsid                    or die "Can't start a new session: $!";
while(1) {
   sleep(5);
   print "Hello...\n";
}
 
The example above is a simple daemon that will print Hello... to the console every five seconds. Also, notice that one line of the program has been commented out. Removing the comment will suppress the hello message by sending all standard output to /dev/null. Also notice that we included the POSIX library and explicitly imported the setsid function since this function is part of the POSIX library, not a built-in Perl function. One other little critical piece is the while(1) { } loop with sleep(5) inside. The loop ensures that the script will run indefinitely. The sleep function sets the number of seconds between each iteration in the loop. The main body of your code will site inside this while loop.

Web Mirror: Listing 2

For example, let's say we need a daemon that mirrors a Web page on a pre-production server. When the file gets updated, it is synced with the production server. We can easily whip up a script that does this by using the mirror method of the LWP::Simple module. Below is a script that implements this functionality.

# load required modules
use strict;
use POSIX qw(setsid);
use LWP::Simple;
# set costants
my $URL     = 'http://www.webreference.com/perl/';
my $FILE    = '/tmp/motherofperl.html';
# flush the buffer
$| = 1;
# daemonize the program
&daemonize;
# our infinite loop
while(1) {
    # mirror the file
    mirror($URL,$FILE);
   
    # wait for 60 seconds
    sleep(60);
}
sub daemonize {
    chdir '/'                 or die "Can't chdir to /: $!";
    open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
    open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
    open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
    defined(my $pid = fork)   or die "Can't fork: $!";
    exit if $pid;
    setsid                    or die "Can't start a new session: $!";
    umask 0;
}


 Auto start 

Crontab loop for 1 minute

*/1 * * * * /root/auto_start.sh >/dev/null 2>&1


#!/bin/bash
RESTART="/root/auto_kill_slow_mysql.pl"

#path to pgrep command
PGREP="/usr/bin/pgrep"

# Httpd daemon name,

HTTPD="auto_kill_slow"

# find httpd pid
$PGREP ${HTTPD}

if [ $? -ne 0 ] # if auto_kill not running
then
 # restart apache
 $RESTART
fi