PHP Script to alert if mail queue size is above X on Cpanel Server

I created a php script to alert if mail queue size if above 100.

<?php

$cmd =  '/usr/sbin/exim -bpc';
$num_mails = exec($cmd);

if ($num_mails > 100)
{
$hostname = exec('/bin/hostname');
$subject = 'Mail queue alert on ' . $hostname;
$mail_text = 'Mail queue on server ' . $hostname . ' have ' . $num_mails . ' mails';
$to_email = 'root@' . $hostname;
mail($to_email, $subject, $mail_text);
}

Saved it as /usr/hostonnet/mail_q_monitor.php

Run it with cronjob

*/10 * * * * /usr/local/bin/php /usr/hostonnet/mail_q_monitor.php >/dev/null 2>&1
Posted in Cpanel Server | Tagged , , | Leave a comment

PHP Fatal error: Class ‘DOMDocument’ not found in LocalisationCache.php

While install MediaWiki 1.20.4, i got error

PHP Fatal error:  Class 'DOMDocument' not found in  LocalisationCache.php 

This is because PHP is not compiled with XML support, to fix install php-xml package.

[root@lin wiki]# yum install php-xml
Loaded plugins: fastestmirror, presto
Loading mirror speeds from cached hostfile
 * base: mirrors.hns.net.in
 * epel: mirrors.ispros.com.bd
 * extras: mirrors.hns.net.in
 * updates: mirrors.hns.net.in
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php-xml.x86_64 0:5.3.3-22.el6 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.24)(64bit) for package: php-xml-5.3.3-22.el6.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.22)(64bit) for package: php-xml-5.3.3-22.el6.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: php-xml-5.3.3-22.el6.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.13)(64bit) for package: php-xml-5.3.3-22.el6.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: php-xml-5.3.3-22.el6.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: php-xml-5.3.3-22.el6.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: php-xml-5.3.3-22.el6.x86_64
--> Running transaction check
---> Package libxslt.x86_64 0:1.1.26-2.el6_3.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================
 Package                                                 Arch                                                   Version                                                           Repository                                            Size
============================================================================
Installing:
 php-xml                                                 x86_64                                                 5.3.3-22.el6                                                      base                                                 103 k
Installing for dependencies:
 libxslt                                                 x86_64                                                 1.1.26-2.el6_3.1                                                  base                                                 452 k

Transaction Summary
============================================================================
Install       2 Package(s)

Total download size: 555 k
Installed size: 2.4 M
Is this ok [y/N]: y
Downloading Packages:
Setting up and reading Presto delta metadata
Processing delta metadata
Package(s) data still to download: 555 k
(1/2): libxslt-1.1.26-2.el6_3.1.x86_64.rpm                                                                                                                                                                            | 452 kB     00:02
(2/2): php-xml-5.3.3-22.el6.x86_64.rpm                                                                                                                                                                                | 103 kB     00:00
----------------------------------------------------------------------------

Total                                                                                                                                                                                                        166 kB/s | 555 kB     00:03
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : libxslt-1.1.26-2.el6_3.1.x86_64                                                                                                                                                                                           1/2
  Installing : php-xml-5.3.3-22.el6.x86_64                                                                                                                                                                                               2/2
  Verifying  : libxslt-1.1.26-2.el6_3.1.x86_64                                                                                                                                                                                           1/2
  Verifying  : php-xml-5.3.3-22.el6.x86_64                                                                                                                                                                                               2/2

Installed:
  php-xml.x86_64 0:5.3.3-22.el6

Dependency Installed:
  libxslt.x86_64 0:1.1.26-2.el6_3.1

Complete!
[root@lin wiki]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[root@lin wiki]#
Posted in PHP | Tagged | Leave a comment

Installing php-mcrypt in CentOS 6

After installing phpmyadmin on CentOS 6 64 bit server, phpmyadmin displays warning

The mcrypt extension is missing. Please check your PHP configuration.

php-mcrypt is not availble in CentOS repo. To install it, you need to install EPEL repo.

[root@lin phpmyadmin]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
Retrieving http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.K84Lrw: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
[root@lin phpmyadmin]#

Now install php-mcrypt with yum

yum install php-mcrypt
Posted in CentOS | Tagged , , , | Leave a comment

Ubuntu Routing Problem 192.69.x.x

After moving labs.buyscripts.in to new server, one of the ubuntu PC can’t connect to it.

root@pc15:~# ping labs.buyscripts.in
PING labs.buyscripts.in (192.69.210.196) 56(84) bytes of data.
From pc15 (192.168.0.15) icmp_seq=1 Destination Host Unreachable
From pc15 (192.168.0.15) icmp_seq=2 Destination Host Unreachable
^C
--- labs.buyscripts.in ping statistics ---
10 packets transmitted, 0 received, +9 errors, 100% packet loss, time 9047ms
pipe 3
root@pc15:~# ping 192.69.210.196
PING 192.69.210.196 (192.69.210.196) 56(84) bytes of data.
From 192.168.0.15 icmp_seq=1 Destination Host Unreachable
From 192.168.0.15 icmp_seq=2 Destination Host Unreachable
From 192.168.0.15 icmp_seq=3 Destination Host Unreachable
^C
--- 192.69.210.196 ping statistics ---
5 packets transmitted, 0 received, +3 errors, 100% packet loss, time 4024ms
pipe 3

root@pc15:~# traceroute 192.69.210.196
traceroute to 192.69.210.196 (192.69.210.196), 30 hops max, 60 byte packets
 1  pc15 (192.168.0.15)  3001.838 ms !H  3001.833 ms !H  3001.825 ms !H
root@pc15:~#

Ping and treaceroute fails.

I checked route

root@pc15:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth1
192.0.0.0       0.0.0.0         255.0.0.0       U     1      0        0 eth1
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth1
root@pc15:~# 

Second line

192.0.0.0       0.0.0.0         255.0.0.0       U     1      0        0 eth1

This caused access to 192.69.210.196 fail. It route traffic to LAN. For LAN, only 192.168.x.x needed.

To fix this, i added route to 192.69.0.0

route add -net 192.69.0.0 netmask 255.255.0.0 gw 192.168.0.1 dev eth1

Added the command to /etc/rc.local, so it run on boot.

Posted in ubuntu | Tagged , , | Leave a comment

Hide Firefox 20 Open Links in a New Private window

Firefox 20 added new feature, that allow you to open private browsing window in same browsing session.

firefox 20 hide new private window

I don’t want this new context menu. To hide this, go to your firefox profile. You can find this by going to

FireFox > Help > Troubleshooting Information

You can directly go to Troubleshooting Information page by typing about:support in address bar of firefox.

On next page, click show folder.

In your profile folder go to chrome folder.

Copy userChrome-example.css to userChrome.css

Now add

#context-openlinkprivate { display:none!important; }

To the file and save. Restart firefox. Now the link will be gone.

I had already know how to hide Open In New Window was context-openlink, so to find for Open Links in a New Private window, i downloaded firefox 20 source code and grep for context-openlink.

[root@server12 mozilla-release]# find ./ -name '*.js' -exec grep 'context-openlink' {} \; -print
    this.showItem("context-openlink", shouldShow && !isWindowPrivate);
    this.showItem("context-openlinkprivate", shouldShow);
    this.showItem("context-openlink", shouldShow);
    this.showItem("context-openlinkintab", shouldShow);
    this.showItem("context-openlinkincurrent", this.onPlainTextLink);
./browser/base/content/nsContextMenu.js
  let linkMenuItem = document.getElementById("context-openlinkincurrent");
./browser/base/content/test/browser_plainTextLinks.js

[root@server12 mozilla-release]#

Here is the content of my userChrome.css, i have disabled Open in New Window.

@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
#context-openlink {display: none !important;}
#bookmarksToolbarFolderMenu { display: none !important;}
#organizeBookmarksSeparator { display: none !important;}
#context-openlinkprivate { display:none!important; }
Posted in firefox | Tagged , | 1 Comment

How to reset MySQL password in Cpanel Server

Log into WHM and scroll down and find SQL Services or search mysql root password.

In that section you will find a link to reset your root password for MySQL.

cpanel_mysql_password_1

WHM >> SQL Services >> Change Mysql user password or you can get change it through the cPanel >> MySQL Databases.

cpanel_mysql_password_2

Posted in Cpanel Server | Tagged , , , | Leave a comment

Securing WordPress Site

WordPress is the most popular content management system (CMS) on the web. This article will explain how you can best protect your WordPress site from malware and cracking, without having deep security knowledge.

1. Create a backup of your site.

If you have cPanel you can do this with the backup manager.
If not, you can use “backup buddy”, a WordPress plugin.

2. Update WordPress Version

This is critical because WordPress issues updates that close security vulnerabilities.

3. Change Your Login/Password

The default WP username is “admin” and hackers know this. So you should change it to strong passwords.
(These should incluse UPPER and lowercase letters, numbers, and symbols)
Most hackers try to brute-force your passwords so if it is really strong you should be fine in that regard.

4. Change your WordPress Keys!

Many people overlook this step but it is an important one as these keys work as salts for cookies and ensure better encryption of data.

Use the WordPress Key Generator to generate mentioned keys. Now edit your wp-config.php file and fine the lines that look like:


define(‘AUTH_KEY’, ‘put your unique phrase here’);
define(‘SECURE_AUTH_KEY’, ‘put your unique phrase here’);
define(‘LOGGED_IN_KEY’, ‘put your unique phrase here’);
define(‘NONCE_KEY’, ‘put your unique phrase here’);

Replace them with the ones from the Key Generator and save.

5. Install WP Security Scan

This plugin is great and makes securing your site simple. It scans for security vulnerabilities and informs you of any malicious code.

6. Prevent .htaccess Hacks

.htaccess (hypertext access) is the default name of directory-level configuration file that provides decentralized management of configuration while inside your web tree.

.htaccess files are often used for security restrictions on a particular directory.

So let’s secure your .htaccess!

First we want to protect the .htaccess file itself so add the following (Do this for all .htaccess files you have in root and or create)


# STRONG HTACCESS PROTECTION

order allow,deny
deny from all
satisfy all

Public_html .htaccess below

Now lets secure your config.php by adding:

# protect wp-config.php

Order deny,allow
Deny from all

Now lets prevent the hacker from browsing your directory tree by adding


# disable directory browsing
Options All -Indexes
Lets prevent some script injections now:


# protect from sql injection
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
Go to your /wp-content folder. Lets limit access to the wp-content directory by creating a .htaccess in the wp-content folder and adding:


Order deny,allow
Deny from all

Allow from all

Go to your /wp-admin/ folder. Now if you have a static IP I would recommend creating a .htaccess in your wp-admin folder with the following .


# deny access to wp admin
order deny,allow
allow from xx.xx.xx.xx
deny from all
Replace the X’s with your IP.

Posted in Windows | Leave a comment

Redirect users to Mobile Version or Desktop Version of Websites Based on Device using Apache mod_rewrite

Since the massive rise of smartphones and tablets like the iPhone, iPad, Android phones and tablets, BlackBerries, etc. you might have considered creating a mobile version of your web site. This tutorial explains how to configure Apache to serve the mobile version of your web site if the visitor uses a mobile device, and the normal version if the visitor uses a normal desktop PC. This can be achieved with Apache’s rewrite module.

In this tutorial, “normal” web site is accessible under http://www.example.com and http://example.com, while my mobile site is called http://m.example.com.

Step 1: Enabling mod_rewrite

First you have to make sure that the Apache module mod_rewrite is enabled. On Debian/Ubuntu, you can enable it like this:

a2enmod rewrite

Restart Apache afterwards – for Debian/Ubuntu, the command is:

/etc/init.d/apache2 restart

Step 2: Configuring Apache To Allow Rewrite Rules In .htaccess Files

My “normal” web site www.example.com or example.com has the vhost configuration file /etc/apache2/sites-available/www.example.com.vhost and the document root /var/www/www.example.com/web.

My mobile site m.example.com has the vhost configuration file /etc/apache2/sites-available/m.example.com.vhost and the document root /var/www/www.example.com/mobile.

I want to place the rewrite rules for each site in an .htaccess file (although it is also possible to place the rewrite rules directly in the vhost configuration file). Therefore I must first modify our vhost configurations so that both .htaccess files are allowed to contain rewrite directives. We can do this with the line AllowOverride All (which allows .htaccess to override all settings in the vhost configuration):

vi /etc/apache2/sites-available/www.example.com.vhost
[...]
        
                AllowOverride All
	
[...]
vi /etc/apache2/sites-available/m.example.com.vhost
[...]
        
                AllowOverride All
        
[...]

Restart Apache afterwards:

/etc/init.d/apache2 restart

Step 3: Creating Rewrite Rules

Now let’s create the rewrite rules for the “normal” web site www.example.com/example.com that will redirect all users of mobile devices to the mobile version m.example.com – I focus on the relevant devices/user agents here which are Android, Blackberry, googlebot-mobile (Google’s mobile search bot), IE Mobile, iPad, iPhone, iPod, Opera Mobile, PalmOS, and WebOS.

The /var/www/www.example.com/web/.htaccess file looks as follows:

vi /var/www/www.example.com/web/.htaccess
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
RewriteRule ^$ http://m.example.com/ [L,R=302]

For our mobile web site m.example.com, the rewrite rules that redirect all users that don’t use a mobile device to our “normal” web site www.example.com/example.com look as follows – I’ve simply negated the RewriteCond condition from the previous .htaccess file:

vi /var/www/www.example.com/mobile/.htaccess
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "!(android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^$ http://www.example.com/ [L,R=302]

That’s it! Now you can do some testing, e.g. visit m.example.com with a normal desktop browser:

1

If all goes well, you should be redirected to www.example.com:

2

Now test with a mobile device (I use an Android phone here) and go to www.example.com:

3

You should be redirected to m.example.com:

4

Posted in Apache, Linux | Tagged , , , , | Leave a comment

Install GitWeb to work with Gitosis Installation

First create a virtual host for gitweb in apache


DocumentRoot /home/gitweb.bizhat.com/public_html/
ServerName gitweb.bizhat.com
CustomLog /var/log/httpd/gitweb.bizhat.com.log combined
AddHandler cgi-script .cgi
DirectoryIndex gitweb.cgi


    AllowOverride All
    Options All
    Order allow,deny
    Allow from all
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.* /gitweb.cgi/$0 [L,PT]


NOTE: since wordpress is not converting < tag in virtual host entry, i have uploaded actual virtual host used here

http://blog.hostonnet.com/files_hon/gitweb.conf

GitWeb is part of git installation. If you have installed git from source, you will see gitweb folder inside git source.

Copy gitweb to docroot of your web site.

mkdir -p /home/gitweb.bizhat.com/public_html/
cd /usr/local/src/git*/gitweb
cp -Rf * /home/gitweb.bizhat.com/public_html/

After visiting the web site, i got following error

 Can't locate CGI.pm in @INC (@INC contains:

So i installed perl-CGI

yum install perl-CGI

Now sites start working, but did not show any repository.

Now you need to edit gitweb.cgi and set path to repository.

Edit gitweb.cgi

vi gitweb.cgi

Find

our $projectroot = "/pub/git";

Replace with your gitosis install path

our $projectroot = "/home/git/repositories/";

Now gitweb will show all repos.

To show only selected projects in gitweb, edit gitweb.cgi

Find

our $projects_list = "";

Replace with

our $projects_list = "projects.txt";

Now create a file “projects.txt” with name of repos you want to show in gitweb.

root@server70 [/home/gitweb.bizhat.com/public_html/]# cat projects.txt
website.git
HostOnNet 

billing.git
HostOnNet 

cms.git
HostOnNet 

root@server70 [/home/gitweb.bizhat.com/public_html/]#
Posted in Git | Tagged , , | Leave a comment