How to Install PHP 7 on CentOS 6

Previously i posted about upgrading PHP 5.3 to PHP 5.6 on CentOS 6.
If you want to install / upgrade it to PHP 7, here’s how:

1. Make sure to remove older version of PHP
yum remove php-common

2. Add EPEL and Remi repository
yum install
yum install

3. Install yum-utils
yum install yum-utils

4. Enable Remi repository
yum-config-manager --enable remi-php72

5. Now install PHP 7
yum install php php-xml php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo

6. When it’s done, restart your web server (apache or nginx)

7. Check your PHP version
php -v

Hope it helps.




How to reset MySQL 5.7 root password on Linux

I was having problem with MySQL root password after a fresh installation on CentOS 6. I cannot login to mysql as i didn’t know what was the default password for root. The installation didn’t prompt me to enter the password for root user.

So if you have a problem like me, or you have forgotten your root password, here’s how to reset it.

1. Shutdown the MySQL
service mysqld stop

2. Start mysql in safe mode
mysqld_safe --skip-grant-tables

Keep this process running, and don’t close this terminal

3. Open new terminal

4. Login to MySQL as root, and without password
mysql -u root

5. Change to database mysql
use mysql;

6. Now update the password for root user
update user set authentication_string=PASSWORD('YOUR-NEW-PASSWORD-HERE'), password_expired = 'N' where user='root';

notes: In MySQL 5.7, the `password` field was removed, now the field name is `authentication_string`.

7. Don’t forget to flush it

8. Exit

9. Back to terminal 1, and stop the process (or close it). Then start the MySQL
service mysqld start

10. Now you can login to MySQL with your new password
mysql -u root -pYOUR-NEW-PASSWORD-HERE

Hope it helps.




How to Upgrade PHP 5.3 to PHP 5.6 on CentOS 6

Follow this post to install PHP 7 to your CentOS 6

Nowadays, to keep up up-to-date with the latest PHP technology, you need at least PHP version 5.6 installed on your server.
But for CentOS 6 users, PHP 5.3.3 is the latest version of PHP available through the official CentOS package repository.

To update your PHP version to version 5.6, follow these steps:

  1. Install repository
    rpm -Uvh
  2. Remove older PHP version
    yum remove php-common
  3. Install PHP 5.6
    yum install php56w php56w-mysql php56w-common php56w-pdo php56w-opcache
  4. Restart Apache
    service httpd restart
  5. Check PHP version
    php --version
    You will get something like:

    PHP 5.6.32 (cli) (built: Oct 29 2017 19:00:01)
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

Congratulation, your PHP is now upgraded to version 5.6!




Convert PHP object to an array recursively

In PHP, you can easily convert an object to an array by typecasting it.

$array = (array) $object;

But if you have multi-dimensional / nested objects, this will only converts the top level of the object. So instead of looping through the object and typecasting it, just use this single line code:

$array = json_decode(json_encode($object), true);

And to convert an array to an object recursively:

$object = json_decode(json_encode($array, JSON_FORCE_OBJECT), false);

note: This only works if the properties of the object you are trying to convert are declared as public


PHP-GD: Create Image from Text

Some people want to hide their text into image for any reason. As example, to avoid spambot steal their email address, or to generate dynamic images content.

Its simply done with PHP and GD Library. See this simple function:

function imgfromstring($string){
	$font  = 5;
	//dynamic size of image. to fit your text.
	$width  = ImageFontWidth($font) * strlen($string);
	$height = ImageFontHeight($font);

	//start creating the image
	$gbr = imagecreate ($width,$height);

	//set background to white
	$bgcolor = imagecolorallocate($gbr, 255, 255, 255);
	//set color for text
	$txtcolor = imagecolorallocate ($gbr, 0, 0, 0);

	//generate the image
	imagestring($gbr, $font, 0, 0, $string, $txtcolor);
	header("Content-type: image/jpeg");
	return imagejpeg($gbr);

$txt = "[email protected]";
echo imgfromstring($txt);

example output:

Another sample is using image as the background. Its useful when you need to create waterwark to protect your images.
Sample code:

function toImgWithBG($str){
	$bg = "background.jpg";
	$font = 5; //Font size

	$gbr =imagecreatefromjpeg($bg);

	//Calculate string width and height
	$pos_x  = imagesx($gbr) - (ImageFontWidth($font) * strlen($str));
	$pos_y = imagesy($gbr) - ImageFontHeight($font);

	//set text color
	$teks = imagecolorallocate($gbr, 255, 0, 0);

	//generate the image, and put on the right bottom
	imagestring($gbr, 5, $pos_x, $pos_y, $str, $teks);

	header("Content-type: image/jpeg");
	return imagejpeg($gbr);

echo toImgWithBG("copyright:");

example output:

You can download source code of this example here:
download here download this file


Restore Ubuntu Boot Loader

To repair your Ubuntu GRUB, you need Ubuntu LiveCD, boot from this CD, and open a terminal. Be a root, by typing “sudo su” and enter your password.

Find the partition which your Ubuntu system is installed.

# fdisk -l

On my machine, my Ubuntu partition is on /dev/sda3, so i’ll use this for example.

Create a mountpoint for this:

# mkdir /media/rootgrub
# mount /dev/sda3 /media/rootgrub

Check if your partition is the correct partition

# ls /media/rootgrub

if the output is not like this, then you have the wrong partition

bin dev home lib mnt root srv usr boot etc initrd lib64 opt sbin sys
var cdrom initrd.img media proc selinux tmp vmlinuz

unmount the partition if have the wrong one, then mount the others. To make sure you have the correct partition, run ls /media/root/boot, which should output something like this :

config-2.6.18-3-686 ¬ ¬ initrd.img-2.6.18-3-686.bak ¬ ¬
grub lost+found ¬ ¬ ¬ ¬ ¬ vmlinuz-2.6.18-3-686 ¬ ¬ ¬ ¬ ¬ ¬ initrd.img-2.6.18-3-686

Now when everything is ok, install the Grub:

# sudo grub-install –root-directory=/media/rootgrub¬ /dev/sda

If you have some warning, try this:

# sudo grub-install –root-directory=/media/rootgrub¬ /dev/sda –recheck

Change /dev/sda to other partition you want to install Grub on. If all goes fine, you will see the output like this:

Installation finished. No error reported. This is the contents of the device map /boot/grub/ Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script `grub-install’.

(hd0) /dev/sda

Restart to your harddrive (not to LiveCD) and see, your GRUB should be AUTOMAGICALLY appear!


Reset Mysql Root Password

Terrible, just setup a new server, then i forgot root password for mysql database. So i need to reset the root password.

This is the way i do:
1. Login to our server machine as root (system administrator). Yes! we must have root access to our machine.

2. Shutdown mysql server (if already running). Make sure no other mysql daemon running.

$ /etc/init.d/mysql stop

3. Create a text file and place the following statements in it. Replace the password with the password that you want to use.

UPDATE mysql.user SET Password=PASSWORD(‘YourNewPasswordHere’) WHERE User=’root’;

The UPDATE and FLUSH statements each must be written on a single line. And make sure we have statement “where user=’root’“, to make sure, only our root password is updated.

4. Save the text file into our home directory, or somewhere else. Example, i saved this as: “/home/vandai/mysql-reset

5. Start the MySQL server with the special –init-file option:

mysqld_safe –init-file=/home/vandai/mysql-reset &

This will execute the contents of the file named by the –init-file option at startup, changing each root account password.

6. Don’t forget to delete mysql-reset file immediately!!

Now we will be able to login to MySql database as root with our new password.

Never forget the password again!!