wordpress delete old post revisions improve site performance

wordpress tutorial, wordpress delete old post revisions improve site performance. We given sql commands and suggested some plugin for delete post revisions.

WordPress has auto save functionality. After some time wordpress creates the revision in database tables automatically. It is really useful. If something happens like powercut or browser crashes then you do not loose the your post data.

wordpress delete old post revisions improve site performance

SQL for delete post rivisions.



DELETE wp,wt,wpm FROM wp_posts wp LEFT JOIN wp_term_relationships wt ON (wp.ID = wt.object_id)
LEFT JOIN wp_postmeta wpm ON (wp.ID = wpm.post_id) WHERE wp.post_type = 'revision'

 
If you want to disable post revision system permanently, you can add following line in wp_config.php file.

define('WP_POST_REVISIONS', false);

 

But it has some bad issues also. If you have 5 to 10 revision per post and 200 posts then you will have at least 1500 to 2000 entries in table. Most important part is all revisions are unuseful. Each additional revision increase the size of your database thus making the processing time slower and slower.

But do not disable to auto save functionality and revision. you need to clean your db after certain time.

For cleaning the database you can use the following plugins.

  • 1. Optimize Database after Deleting Revisions –

Optimize Database after Deleting Revisions


This plugin is a ‘One Click’ WordPress Database Cleaner / Optimizer.

Main Features
Deletes redundant revisions of posts and pages (you optionally can keep an ‘x’-amount of the most recent revisions)
Deletes trashed posts, pages and comments (optional)
Deletes spammed comments (optional)
Deletes ‘orphan postmeta items’
Optimizes the database tables (optionally you can exclude certain tables from optimization)
Creates a log file of the optimizations (optional)
Optimization can be scheduled to automatically run once hourly, twice daily, once daily or once weekly at a specific time (optional)
‘Optimize DB (1 click)’ link in the admin bar (optional)
Settings
You can find the settings page in the WP Admin Panel » Settings » Optimize DB Options.

Starting the Optimization
You can start the Optimization in the WP Admin Panel » Tools » Optimize Database. Note: if you use the Scheduler the Optimization will run automatically!

  1. 2. Revision Removal – http://wordpress.org/plugins/revision-removal/

Revision Post is the new features of WordPress ver 2.6 and above. Every time you made a change to your post or page, it will automatic save add a revision, just like a draft. As many as you made change, the more revision you will have in your database. It will not only increase your database size, but also increase your load time as the server need more time to access the database.

Revision Manager is your choice to remove all the revisions quickly to help you to increase the speed of implementation of the SQL statement, and increase the speed of your server load.

wordpress delete old post revisions improve site performance
wordpress delete old post revisions improve site performance

How can we save Ram usages using some wordpress theme tricks

While running wordpress site, save Ram usage is always great idea and you can easily improve the site performance by simple wordpress theme tricks.

We mostly use the get_permalink(), get_the_title() methods in our wordpress theme. Do not pass the post ID as parameter.

if you’re doing a get_permalink or get_title() call with Post id, 8 out of 10 times you’ll need to more of that post than just the permalink, so this isn’t really a problem.

save Ram usage

Post object is actually already slightly faster than calling get_permalink with $post->ID (in get_post it then only sanitizes and adds to cache, it doesn’t fetch new data), but the real benefit comes when you add a variable called filter in the $post object, setting it to “sample”. Now you decide whether that post object is going to be cached or not and which variables it contains.

Pass the $Post object instead of Post ID.

Do not use the Custom fields. Your server need to fire extra custom quries on Mysql server.

If your are using $wpdb->get_results or new WP_Query( $args ) then add the order by.

How can we save Ram usages using some wordpress theme tricks
How can we save Ram usages using some wordpress theme tricks

If wordpress site is hacked then how to fix issue

Recently one of my wordpress site is hacked which is on wordpress. There is something wrong happening on server. We fixed issue with some steps, we given full steps for fixing issue. Due to disk I/O notification and CPU usages notification email I got to know.

If wordpress site is hacked then how to fix issue

There is something wrong happening on server.

First thing I did which is checking the apache access logs and error logs. I was getting per second 100 request from some IP addresses.

I stoped apache server and I took my database and filesystem backup. Deleted my admin username and added new administrator with new username.

You should use the Better WP Security wordpress plugin. This is very useful plugin.

iThemes Security (formerly Better WP Security)

I added following code in my .htaccess file

 # BLOCK BAD IPS
 <limit GET POST PUT>
 Order Allow,Deny
 Allow from all
 # uncomment/edit/repeat next line to block IPs
 # Deny from 123.456.789
 Deny from 192.111.152.122
 Deny from 192.111.144.233
 Deny from 110.85.90.123
 </limit>

If wordpress site is hacked then how to fix issue
If wordpress site is hacked then how to fix issue

But above code was still not helpful to me because disk I/O and apache process was taking time to sending the request to 403.

Then I blocked the IP Address on My Linux server using following commands.

 iptables -A INPUT -s 192.111.144.789 -j DROP
 iptables -A INPUT -s 192.111.152.122 -j DROP
 iptables -A INPUT -s 192.119.144.123 -j DROP

This solved my issue.

deleting all one day old files from folder through php

If you want to find the old files from your system or server then you can use the following code. I used the following code for deleting the old files from system.

deleting all one day old files from folder through php


/*****************Get the path to Extension ****************/
$array_path = explode("/",$_SERVER['SCRIPT_FILENAME']);
<div id=":2e">$dynamic_path = "";
for ($i=0;$i
 if($array_path[$i]!="")
$dynamic_path =$dynamic_path."/".$array_path[$i];

// This linux command will delete the one day older from specific folder.
exec("find ".$dynamic_path."* -mtime +1 -exec rm {} \;");</pre>
<div>
For deleting the old files from system I used the Linux command. You can execute the command using the exec function.
If you want to use the Linux command then use the following command
# find /home/user/test/ -m

deleting all one day old files from folder through php
deleting all one day old files from folder through php

time +1 exec rm {}

Above code will only work in Linux server.

convert avi to flv using ffmpeg with Thumbnail

Video upload functionality is needed in every networking site. I will give best option the create the thumbnail from video file and convert video file to flv format.

convert avi to flv using ffmpeg with Thumbnail

For conversion of video files and creating the thumbnail we need the ffmpeg software need to be installed on server. Here I used the linux server. For installing the FFmpeg I used following article.

http://mysql-apache-php.com/ffmpeg-install.htm

After installing the ffmpeg software in linux you can use the command prompt for converting the video file.

Use the following command for covert the avi or video file to flv format.

#ffmpeg -i VIDEOFILE.avi  -ab 56 -ar 44100 -b 200 -r 15 -s 320×240 -f flv final.flv

If you are using the PHP then you can use following code in your PHP script for converting the video file.


exec("ffmpeg -i VIDEOFILE.avi  -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv final.flv");

//For creating the thumbnail use the following code

exec("ffmpeg -y -i moviefile.avi -vframes 1 -ss 00:01:60 -an -vcodec png -f rawvideo -s 110x90 new.png");

converting the video files to flv format that takes some time. So I recommend not to convert the video file to flv while uploading.

Just create the thumbnail and show to use.

For converting the video file to flv format use the cronjob. Create two or One cronjob setting and daily two or more time through script convert the video files to flv.

mysql sleep processes issue solved

I faced the sleep query issue sometimes.  Many times that kills the server. When ever you are using dedicated virtual hosting server that time you need to me very careful how you are setting up the apache and mysql. we solved mysql sleep processes issue

mysql sleep processes issue solved

mysql sleep processes issue solved
mysql sleep processes issue solved

I found some major reasons for mysql sleep processes. Here I am giving some reasons:

[viral-lock message=”Reasons and Solution is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]

1. apache requests increase
2. mysql queries increase
3. mysql slow queries increase
4. apache connections start to wait on slow mysql queries
5. apache connections take longer to close
6. more apache connections are opened
7. with each apache request a new sleeping mysql connection is made
8. mysql & apache reach max_connections
9. server slows to a crawl

[/viral-lock]

Using linux top command you can check the load average and which process is taking so much memory of your server. If you saw mysqld is taking much memory of server then you should check using following mysql commands:

> show full processlist;

using this command you can check the mysql processes. Which sql queries being fired on mysql server. If you found so much sleep processes then you should check your mysql variables and settings. If so much mysql queries being fired on specific table then you should change the table storage engine type.

Using following command you can change the storage engine type.
> ALTER TABLE tablename ENGINE = MYISAM;

MYISAM storage engine type is faster then inodb. you can create the mediator tables also for big size table to handing a load of table.

Then major change for solving the issue of sleep processes is, you should change the wait_timeout variable. Default value is 2800 seconds.

mysql> show variables;

you can change the setting using following  mysql command. Best value is 60 second need to be set for wait timeout.
mysql> show variables like ‘wait_timeout’;

mysql> set global wait_timeout=60;

mysql>show variables like ‘wait_timeout’;

After doing above changes you can check the mysql processlist. If you are still facing issues with mysql then please write to me.

how to find out cpu and disk usage in linux

When you are running any script on server or running any application you want to know how much memory or disk uses is happened by application.

how to find out cpu and disk usage in linux

how to find out cpu and disk usage in linux
how to find out cpu and disk usage in linux

When you are doing load testing this time this kind of applications are very useful to check the server status. Create file name CPU-Disk.sh and put following content in that.

#!/bin/sh

# Change this as needed

OUTPUT_FILE=file.txt

# Initially delete old output file if needed.

/bin/rm -f $OUTPUT_FILE

while [ 1 -eq 1 ]; do

date >> $OUTPUT_FILE

echo "------------------------" >> $OUTPUT_FILE

cat /proc/meminfo >> $OUTPUT_FILE

echo "------------------------" >> $OUTPUT_FILE

df -k >> $OUTPUT_FILE

echo "------------------------" >> $OUTPUT_FILE

top -b -i -n 1 >> $OUTPUT_FILE

# Record terminator. Add more === if needed :)

echo "========================" >> $OUTPUT_FILE

sleep 60

done

This command is useful to create the sh file executable in linux

#chmod +x /YOURPATH/CPU-Disk.sh

when you need to check the server status you need to run the this file using this command

#CPU-Disk

How to install Mongodb on linux

Mongodb is used for many projects now, it is quite fast. I given full detailed information about How to install Mongodb on linux box. Before installing the Mongodb on linux box you need to install following packages on box.

How to install Mongodb on linux

[root@sonyk-pc Download]# sudo yum -y install git tcsh scons gcc-c++ glibc-devel
[root@sonyk-pc Download]# sudo yum -y install boost-devel pcre-devel js-devel readline-devel
[root@sonyk-pc Download]# sudo yum -y install boost-devel-static readline-static ncurses-static

For 32bit user use following command

[root@sonyk-pc Download]# wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.2.tgz

For 64bit use following command

[root@sonyk-pc Download]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.6.2.tgz
[root@sonyk-pc Download]# tar xzf mongodb-linux-i686-1.6.2.tgz
[root@sonyk-pc Download]#  cd mongodb-linux-i686-1.6.2

Mongo stores database in data/db folder. so we need to create those folder using following command.

[root@sonyk-pc mongodb-linux-i686-1.6.2]# sudo mkdir -p /data/db/
[root@sonyk-pc mongodb-linux-i686-1.6.2]# sudo chown `id -u` /data/db
[root@sonyk-pc mongodb-linux-i686-1.6.2]# ./bin/mongod
./bin/mongod --help for help and startup options
Thu Sep  9 13:10:55 MongoDB starting : pid=22159 port=27017 dbpath=/data/db/ 32-bit
** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
**       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Sep  9 13:10:55 db version v1.6.2, pdfile version 4.5

If you want to create mongodb service in linux then use following steps:

[root@sonyk-pc Download]# wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.2.tgz
[root@sonyk-pc Download]# tar xzf http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.2.tgz
[root@sonyk-pc Download]# mv mongodb-linux-i686-1.6.2 /opt/mongodb
[root@sonyk-pc Download]# mkdir -p /srv/db/mongodb
[root@sonyk-pc Download]# touch /srv/db/mongodb.log
[root@sonyk-pc Download]# mkdir /opt/bin/
[root@sonyk-pc Download]# mkdir /opt/config/

create the File: /opt/bin/mongodb-stop
Put following code in that file:

#!/bin/bash
pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`;
if [ "${pid}" != "" ]; then
kill -2 ${pid};
fi

create the File: /opt/bin/mongodb-start
Put following code in that file:

#!/bin/sh
/opt/mongodb/bin/mongod --config /opt/config/mongodb \
## --upgrade \ ##runs a database upgrade option if needed \

File: /opt/config/mongodb
Put following code in that file:

# Configuration Options for MongoDB
#
# For More Information, Consider:
# - Configuration Parameters: http://www.mongodb.org/display/DOCS/Command+Line+Parameters
# - File Based Configuration: http://www.mongodb.org/display/DOCS/File+Based+Configuration
dbpath = /srv/db/mongodb
logpath = /srv/db/mongodb.log
logappend = true
bind_ip = 127.0.0.1
port = 27017
fork = true
auth = true
# noauth = true

Do that file as linux executable
chmod +x /opt/bin/mongodb-start
chmod +x /opt/bin/mongodb-stop
We’ve also created a very basic “init script” as a wrapper around the mongodb-start and mongo-stop scripts described above. You will still need to modify and manage the configuration of your MongoDB server in the files above. This script only provides a means for ensuring that MongoDB will start at boot. Issue the following commands:
wget http://library.linode.com/databases/mongodb/reference/init-rpm.sh
mv init-rpm.sh /etc/rc.d/init.d/mongodb
chmod +x /etc/rc.d/init.d/mongodb /etc/init.d/mongodb
chkconfig –add mongodb
chkconfig –level 35 mongodb on
You will also need to create a user and group for mongodb; issue the following command:

useradd -M -r --home-dir /opt/mongodb mongodb

Now issue the following command to ensure that the MongoDB user you just created will have access to all required files in the /srv/db/ hierarchy:
chown mongodb:mongodb -R /srv/db/
To start and stop MongoDB using the init script, issue the appropriate command from the following:
/etc/init.d/mongodb start
/etc/init.d/mongodb stop
For checking the web admin interface of Mongodb – listening on port 28017
Check this URL : http://localhost:28017/
Before installing the Mongodb on linux box you need to install following packages on box.[root@sonyk-pc Download]# sudo yum -y install git tcsh scons gcc-c++ glibc-devel[root@sonyk-pc Download]# sudo yum -y install boost-devel pcre-devel js-devel readline-devel[root@sonyk-pc Download]# sudo yum -y install boost-devel-static readline-static ncurses-static

For 32bit user use following command[root@sonyk-pc Download]# wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.2.tgz
For 64bit use following command[root@sonyk-pc Download]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.6.2.tgz

[root@sonyk-pc Download]# tar xzf mongodb-linux-i686-1.6.2.tgz [root@sonyk-pc Download]# cd mongodb-linux-i686-1.6.2
Mongo stores database in data/db folder. so we need to create those folder using following command.
[root@sonyk-pc mongodb-linux-i686-1.6.2]# sudo mkdir -p /data/db/[root@sonyk-pc mongodb-linux-i686-1.6.2]# sudo chown `id -u` /data/db
Using following command you can start the mongo database.
[root@sonyk-pc mongodb-linux-i686-1.6.2]# ./bin/mongod./bin/mongod –help for help and startup optionsThu Sep 9 13:10:55 MongoDB starting : pid=22159 port=27017 dbpath=/data/db/ 32-bit
** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data** see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Sep 9 13:10:55 db version v1.6.2, pdfile version 4.5

If you want to create mongodb service in linux then use following steps:
[root@sonyk-pc Download]# wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.2.tgz [root@sonyk-pc Download]# tar xzf http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.2.tgz[root@sonyk-pc Download]# mv mongodb-linux-i686-1.6.2 /opt/mongodb[root@sonyk-pc Download]# mkdir -p /srv/db/mongodb[root@sonyk-pc Download]# touch /srv/db/mongodb.log
[root@sonyk-pc Download]# mkdir /opt/bin/[root@sonyk-pc Download]# mkdir /opt/config/
create the File: /opt/bin/mongodb-stopPut following code in that file;

#!/bin/bash
pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`;if [ "${pid}" != "" ]; then    kill -2 ${pid};fi

create the File: /opt/bin/mongodb-startPut following code in that file:

#!/bin/sh
/opt/mongodb/bin/mongod --config /opt/config/mongodb \## --upgrade \ ##runs a database upgrade option if needed \

File: /opt/config/mongodbPut following code in that file:

# Configuration Options for MongoDB## For More Information, Consider:# - Configuration Parameters: http://www.mongodb.org/display/DOCS/Command+Line+Parameters# - File Based Configuration: http://www.mongodb.org/display/DOCS/File+Based+Configuration
dbpath = /srv/db/mongodblogpath = /srv/db/mongodb.loglogappend = true
bind_ip = 127.0.0.1port = 27017fork = true
auth = true# noauth = true

Do that file as linux executatblechmod +x /opt/bin/mongodb-startchmod +x /opt/bin/mongodb-stop

We’ve also created a very basic “init script” as a wrapper around the mongodb-start and mongo-stop scripts described above. You will still need to modify and manage the configuration of your MongoDB server in the files above. This script only provides a means for ensuring that MongoDB will start at boot. Issue the following commands:
wget http://library.linode.com/databases/mongodb/reference/init-rpm.shmv init-rpm.sh /etc/rc.d/init.d/mongodbchmod +x /etc/rc.d/init.d/mongodb /etc/init.d/mongodbchkconfig –add mongodbchkconfig –level 35 mongodb on

You will also need to create a user and group for mongodb; issue the following command:

useradd -M -r --home-dir /opt/mongodb mongodb

Now issue the following command to ensure that the MongoDB user you just created will have access to all required files in the /srv/db/ hierarchy:
chown mongodb:mongodb -R /srv/db/
To start and stop MongoDB using the init script, issue the appropriate command from the following:
/etc/init.d/mongodb start/etc/init.d/mongodb stop

For checking the web admin interface of Mongodb – listening on port 28017Check this URL : http://localhost:28017/

How to install Mongodb on linux
How to install Mongodb on linux

Post message to another server through fopen and fsockopen in php

PHP tutorial for, Post message to another server through fopen and fsockopen in php. When this comes to sending data to server to server we need to post the data using web services.
Many people want to use the curl php method but there are serious issues with that.

Post message to another server through fopen and fsockopen in php

 

Post message to another server through fopen and fsockopen in php
Post message to another server through fopen and fsockopen in php

So I recommended to use fopen function to post the message to server to server.

You can use the following code for post the message to another server using fopen and fsockopen

    function post($host, $post_url,$port,$data) {
        $errno = 0;
        $errstr = '';

        $path = str_replace($host,'',$post_url); // IP of minor instance

        $header_variables = "POST / HTTP/1.1\r\n";
        $header_variables .= "Host: $host\r\n";
        $header_variables .= "Connection: Close\r\n";
        $header_variables .= "Content-Type: application/xml\r\n";
        $header_variables .= "Content-Length: " . strlen($data) . "\r\n\r\n";

        // establish the connection and send the request
        $fp = fsockopen($host, $port, &$errno, &$errstr, 15);
        if ($fp) {
            stream_set_timeout($fp, 15);
            fputs($fp, $header_variables);
            fputs ($fp, $data); //data written
            fputs ($fp, "\r\n"); //request posted

            $response = stream_get_contents($fp);
            $info = stream_get_meta_data($fp);
            if (!$info['timed_out'] && $fp) {
                //get response and everything is ok
            } else {
                trigger_error('Timed out for '.$post_url);
                exit();
            }

            fclose($fp); //close the file

        } else {
            trigger_error('Failed to open socket connection. ');
            exit();
        }
    }



using CURL function post message also possible for that you can use following code

<!--?php

/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/

$ch = curl_init();

$data = array('name' =--> 'Foo', 'file' => '@/home/user/test.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);
?>

how to find largest tables on MySQL Server

Using following article You can find the largest tables from Mysql database. Finding largest tables on MySQL instance is no brainier in MySQL 5.0+ thanks to Information Schema but I still wanted to post little query I use for the purpose so I can easily find it later, plus it is quite handy in a way it presents information:

how to find largest tables on MySQL Server

PLAIN TEXT

SQL:

mysql> SELECT concat(table_schema,’.’,table_name),concat(round(table_rows/1000000,2),’M’) rows,concat(round(data_length/(1024*1024*1024),2),’G’) DATA,concat(round(index_length/(1024*1024*1024),2),’G’) idx,concat(round((data_length+index_length)/(1024*1024*1024),2),’G’) total_size,round(index_length/data_length,2) idxfrac FROM information_schema.TABLES ORDER BY data_length+index_length DESC LIMIT 10;
+————————————-+——–+——–+——–+————+———+
| concat(table_schema,’.’,table_name) | rows | DATA | idx | total_size | idxfrac |
+————————————-+——–+——–+——–+————+———+
| art87.link_out87 | 37.25M | 14.83G | 14.17G | 29.00G | 0.96 |
| art87.article87 | 12.67M | 15.83G | 4.79G | 20.62G | 0.30 |
| art116.article116 | 10.49M | 12.52G | 3.65G | 16.18G | 0.29 |
| art84.article84 | 10.10M | 10.11G | 3.59G | 13.70G | 0.35 |
| art104.link_out104 | 23.66M | 6.63G | 6.55G | 13.18G | 0.99 |
| art118.article118 | 7.06M | 10.49G | 2.68G | 13.17G | 0.26 |
| art106.article106 | 9.86M | 10.19G | 2.76G | 12.95G | 0.27 |
| art85.article85 | 6.20M | 9.82G | 2.51G | 12.33G | 0.26 |
| art91.article91 | 8.66M | 9.17G | 2.66G | 11.83G | 0.29 |
| art94.article94 | 5.21M | 10.10G | 1.69G | 11.79G | 0.17 |
+————————————-+——–+——–+——–+————+———+
10 rows IN SET (2 min 29.19 sec)

I do some converting and rounding to see number of rows in millions and data and index size in GB so I can save on counting zeros.
The last column shows how much does the index take compared to the data which is mainly for informational purposes but for MyISAM can also help you to size your key buffer compared to operating system cache.

I also use it to see which tables may be worth to review in terms of indexes. Large index size compared to data size often indicates there is a lot of indexes (so it is well possible there are some duplicates, redundant or simply unused indexes among them) or may be there is long primary key with Innodb tables. Of course it also could be perfectly fine tables but it is worth to look.

Changing the query a bit to look for different sorting order or extra data – such as average row length you can learn quite a lot about your schema this way.

It is also worth to note queries on information_schema can be rather slow if you have a lot of large tables. On this instance it took 2.5 minutes to run for 450 tables.

UPDATE: To make things easier I’ve added INFORMATION_SCHEMA to the query so it works whatever database you have active. It does not work with MySQL before 5.0 still of course