Archive for the ‘PHP / Mysql’ Category

Install Imagemagick / Imagick for PHP on CentOS Cpanel/Plesk

Thursday, April 24th, 2008

Install Imagemagick / Imagick for PHP on CentOS Cpanel/Plesk

Solution : For installing Imagick extension for PHP you have to install ImageMagick from source and Its a Must. Otherwise the extension for PHP wont compile.

*# wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz*
# tar zxf ImageMagick.tar.gz
# cd ImageMagick-x.x.x
# ./configure
# make
# make install

Now download the Imagick extension source from http://pecl.php.net/package/imagick

*# wget http://pecl.php.net/get/imagick-x.x.x.tgz*
# tar zxf imagick-x.x.x.tgz
# cd imagick-x.x.x.tgz
# phpize && ./configure

*** if you get an error which looks like this:

============================
checking for PHP includes… -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext
checking for PHP extension directory… /usr/lib64/php/modules
checking for PHP installed headers prefix… /usr/include/php
checking for re2c… no
configure: WARNING: You will need re2c 0.9.11 or later if you want to regenerate PHP parsers.
checking for gawk… gawk
checking whether to enable the magickwand extension… no
configure: error: not found. Please provide a path to MagickWand-config or Wand-config program.
============================

Solution : Install ImageMagick-devel package using “yum install ImageMagick-devel” and then continue

# make
# make install

This will make compile imagick source and build imagick.so – In my machine, I found imagick.so in the following directory

Installing shared extensions: /path/to/php/modules/

# Now add “extension=imagick.so” in your php.ini (You may need to write the full path of imagick.so based on your configuration)

# Restart your web server and thats it (You should see the Imagick extension section in phpinfo page )

How to Send Email from a PHP Script

Saturday, January 12th, 2008

Using php mail() function to send emails from php script.

Send Email from a PHP Script Example :

The first argument to this function is the recipient, the second specifies the message’s subject and the third one should contain the body. So to send a simple sample message, we could use:

$to = "recipient@example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("

Message successfully sent!

“);
} else {
echo(“

Message delivery failed…

“);
}
?>

Save this file as mail.php and browse from url http://yourdomain/mail.php

Couldn’t open stream {localhost:143}INBOX

Wednesday, November 14th, 2007

Q) All my other IMAP clients can connect to my server, but TWIG says:
Warning: Couldn’t open stream {localhost:143}INBOX in
/var/www/html/twig/lib/mail/php-imap.inc.php3

A) Your server probably has Transport Layer Security turned on, but
PHP does not support TLS. You’ll need to edit config.inc.php3:
$config["imap_port"] = “143/notls”;

Good read…

MySQL Quota Check Tool

Saturday, May 26th, 2007

I came across wonderfull mysql quota check tool — Here is how it works -

The MySQL Quota-Tool helps you to set a size limit on MySQL databases.

It works by checking the size of each database and revoking the INSERT- and REATE-priveleges for the databases, which exceed the given size limit.

When the size of the database falls below the given limit, the INSERT- and CREATE-priveleges are granted again.

This (of course) doesn’t work for users who have global priveleges, because the quota is database and not user based, but in most environments privileges are given in the “db”-table which is modified by the MySQL Quota Tool.

#!/usr/bin/php -q

/*
* MySQL quota script
* written by Sebastian Marsching
*
*/

/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/*
* Create table for quota data with the following statement:
*
* CREATE TABLE `Quota` (`Db` CHAR(64) NOT NULL,
* `Limit` BIGINT NOT NULL,
* `Exceeded` ENUM('Y','N') DEFAULT 'N' NOT NULL,
* PRIMARY KEY (`Db`), UNIQUE (`Db`));
*
* The field 'db' stores the information for which database
* you want to limit the size.
* The field 'limit' is the size limit in bytes.
* The field 'exceeded' is only used internally and must be
* initialized with 'N'.
*/

/*
* Settings
*/

$mysql_host = 'localhost';
$mysql_user = 'root'; // Do NOT change, root-access is required
$mysql_pass = '';
$mysql_db = 'quotadb'; // Not the DB to check, but the db with the quota table
$mysql_table = 'quota';

/*
* Do NOT change anything below
*/

$debug = 0;

// Connect to MySQL Server

if (!mysql_connect($mysql_host, $mysql_user, $mysql_pass))
{
echo "Connection to MySQL-server failed!";
exit;
}

// Select database

if (!mysql_select_db($mysql_db))
{
echo "Selection of database $mysql_db failed!";
exit;
}

// Check quota for each entry in quota table

$sql = "SELECT * FROM $mysql_table;";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))
{
$quota_db = $row['db'];
$quota_limit = $row['limit'];
$quota_exceeded = ($row['exceeded']=='Y') ? 1 : 0;

if ($debug)
echo "Checking quota for '$quota_db'...\n";

$qsql = "SHOW TABLE STATUS FROM $quota_db;";
$qresult = mysql_query($qsql);

if ($debug)
echo "SQL-query is \"$qsql\"\n";

$quota_size = 0;

while ($qrow = mysql_fetch_array($qresult))
{
if ($debug)
{ echo "Result of query:\n"; var_dump($qrow); }
$quota_size += $qrow['Data_length'] + $qrow['Index_length'];
}

if ($debug)
echo "Size is $quota_size bytes, limit is $quota_limit bytes\n";

if ($debug && $quota_exceeded)
echo "Quota is marked as exceeded.\n";
if ($debug && !$quota_exceeded)
echo "Quota is not marked as exceeded.\n";

if (($quota_size > $quota_limit) && !$quota_exceeded)
{
if ($debug)
echo "Locking database...\n";
// Save in quota table
$usql = "UPDATE $mysql_table SET exceeded='Y' WHERE db='$quota_db';";
mysql_query($usql);
if ($debug)
echo "Querying: $usql\n";
// Dismiss CREATE and INSERT privilege for database
mysql_select_db('mysql');
$usql = "UPDATE db SET Insert_priv='N', Create_priv='N' WHERE Db='$quota_db';";
mysql_query($usql);
if ($debug)
echo "Querying: $usql\n";
mysql_select_db($mysql_db);
}

if (($quota_size <= $quota_limit) && $quota_exceeded)
{
if ($debug)
echo "Unlocking database...\n";
// Save in quota table
$usql = "UPDATE $mysql_table SET exceeded='N' WHERE db='$quota_db';";
mysql_query($usql);
if ($debug)
echo "Querying: $usql\n";
// Grant CREATE and INSERT privilege for database
mysql_select_db('mysql');
$usql = "UPDATE db SET Insert_priv='Y', Create_priv='Y' WHERE Db='$quota_db';";
mysql_query($usql);
if ($debug)
echo "Querying: $usql\n";
mysql_select_db($mysql_db);
}
}

?>

—-

More details can be found at – http://projects.marsching.org/mysql_quota/

I am going to install this tool and see how it works for me :)

Thanks,
Preeti S.
ThinkSupport.net

Timeout error occurred trying to start MySQL Daemon.

Friday, May 18th, 2007

I came across mysql error — “Timeout error occurred trying to start MySQL Daemon.”

Mysql will not restart —

[root@vps init.d]# ./mysqld restart
Stopping MySQL: [FAILED]
Timeout error occurred trying to start MySQL Daemon.
Starting MySQL: [FAILED]

Server Details :
Linux VPS — Plesk Installed.
Mysql Version : 4.1

——

Solution : Add this line in /etc/my.cnf

skip-innodb under socket=/var/lib/mysql/mysql.sock

vi /etc/my.cnf
——–

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-innodb
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
———

Thanks,
Sachin
ThinkSupport.net

How to access PHP5 ?

Saturday, April 28th, 2007

How to access PHP5 on your server if you have both PHP4 and PHP5 installed.

You can enabled PHP5 on particular domain by adding following line in .htaccess under public_html folder –

AddType application/x-httpd-php5 .php

Thank you :)

Useful MYSQL tips ..

Friday, April 27th, 2007

Check this —

http://souptonuts.sourceforge.net/readme_mysql.htm

Installing Fileinfo using PECL Modules

Tuesday, April 24th, 2007

The PHP Extension Community Library (PECL) is a collection of modules/extensions that you can install to enhance your version of PHP. The categories range wildly from Authentication to Image Handling, and from Encryption to XML. However, installation of the PECL libraries can sometimes be a bit tricky for the beginner. There are two main ways to install the PECL libraries. Note that if you installed PHP from an RPM or similar package, you will also need the PHP-devel package as well.

You can view all the PECL modules available at the PECL website. Some useful modules are:

We will be installing Fileinfo 1.0.4 for this tutorial.

Installation using the PECL binary

If you are using a more recent version of PHP, chances are you have the pecl binary installed as well. As root from the command line, try running ‘pecl’. If found, it will give you a list of options that can be used to compile your extensions. To install Fileinfo, simply type

pecl install fileinfo

This will download the fileinfo source files, phpize the source, and then compile and install the extension.

You will need to activate the extension by editting your php.ini and add:

extension=fileinfo.so

Restarting your webserver should result in PHP starting with the fileinfo extension active and ready for use.

Installing from Source

Some systems don’t have pecl available due to running an older version of PHP. However, you can still install the extension, albeit with a bit more work. The first step is to download the extension, unpack the file, and phpize the contents. You then need to perform a normal configure and make routine to install the module:

wget http://pecl.php.net/get/Fileinfo-1.0.4.tgz
tar -zxf Fileinfo-1.0.4.tgz
cd Fileinfo-1.0.4
phpize
./configure
make
make install

This will install the fileinfo.so module in your php extensions folder. As per above, add this to your php.ini file by adding

extension=fileinfo.so

in the extensions section. It would be wise to check that your php.ini has the correct extensions directory set as well and this is set via the extension_dir directive.

Installing on Windows

Installation of the PECL modules on windows is alot easier, as all the hard work has been done. Simply locate the extension on the PECL Windows page and simply place the dll in your extensions directory. Add the extension to the php.ini file as described above, restart your web server, and you are good to go!

What is Pear ?

Monday, April 23rd, 2007

PEAR Is short for ‘PHP Extension and Application Repository’

PEAR is having it’s primary goal to become a repository for PHP extensions and library code. The most important goal of the PEAR project is to try to define standard coding packages that can be used be developers directoly and it is re-usable coding.
Each package is a seperate project and is having a diffreent developer team.

Packages are in the gzipped tar files with a description file inside, and installed on your server using the PEAR installer.

You can install any pear package as
———–
pear install pear-package-name
———–

You can check all the PEAR packages that are available with
————
pear list-all
————

You can check installed PEAR packages on your server as
————
pear list
————

How to Optimize MySQL on DirectAdmin Server

Sunday, April 22nd, 2007

vi /etc/my.cnf [ENTER]
Press ‘i’ to enter insert mode, then paste:

[mysqld]
skip-locking
skip-innodb
query_cache_limit=1M
query_cache_size=32M
query_cache_type=1
max_connections=500
interactive_timeout=100
wait_timeout=100
connect_timeout=10
thread_cache_size=128
key_buffer=16M
join_buffer=1M
max_allowed_packet=16M
table_cache=1024
record_buffer=1M
sort_buffer_size=2M
read_buffer_size=2M
max_connect_errors=10
# Try number of CPU’s*2 for thread_concurrency
thread_concurrency=2
myisam_sort_buffer_size=64M
log-bin
server-id=1

[safe_mysqld]
err-log=/var/log/mysqld.log
open_files_limit=8192

[mysqldump]
quick
max_allowed_packet=16M

[mysql]
no-auto-rehash
#safe-updates

[isamchk]
key_buffer=64M
sort_buffer=64M
read_buffer=16M
write_buffer=16M

[myisamchk]
key_buffer=64M
sort_buffer=64M
read_buffer=16M
write_buffer=16M

[mysqlhotcopy]
interactive-timeout
Press ctrl-c to exit insert mode. Then press shift-Z shift-Z to save and quit. Restart mysqld:
Redhat:

/sbin/service mysqld restart

FreeBSD:

/usr/local/etc/rc.d/mysqld restart

Note that this guide is only for MySQL 4. There is a default my.cnf that comes with mysql that will work fine

cp -f /usr/share/mysql/my-huge.cnf /etc/my.cnf