Posted on Leave a comment

Fix SQL Error: GROUP BY incompatible with sql_mode=only_full_group_by

MySQL 5.7.5+ changed the GROUP BY behavior. So in this and future versions GROUP BY would not work in same way as it used to be before.

So to make GROUP BY feature working like before follow these details:

Note: First of all to follow these steps you must need to have root access to your server.

If you want to disable error sql_mode=only_full_group_by  permanently then do those steps:

  1. sudo nano /etc/mysql/my.cnf
    
  2. Add this to the end of the file
    [mysqld]
    sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    
  3. sudo service mysql restart  to restart MySQL

This will disable ONLY_FULL_GROUP_BY for ALL users

Posted on Leave a comment

PHP: How to fix Image rotation issue in img tag because of EXIF

Photos captured by some smartphones especially iPhones are automatically rotated when we use it in <img> tag which is mostly not acceptable as we don’t need such unwanted rotation. This happens because of EXIF Data stored in that image. But we can easily fix it using PHP by following these steps:

function autorotate($src)
{
// check if extension exists or not
if(extension_loaded('imagick'))
{
    try
    {       
        $image= new Imagick($src);
        switch ($image->getImageOrientation()) {
        case Imagick::ORIENTATION_TOPLEFT:
            break;
        case Imagick::ORIENTATION_TOPRIGHT:
            $image->flopImage();
            break;
        case Imagick::ORIENTATION_BOTTOMRIGHT:
            $image->rotateImage("#000", 180);
            break;
        case Imagick::ORIENTATION_BOTTOMLEFT:
            $image->flopImage();
            $image->rotateImage("#000", 180);
            break;
        case Imagick::ORIENTATION_LEFTTOP:
            $image->flopImage();
            $image->rotateImage("#000", -90);
            break;
        case Imagick::ORIENTATION_RIGHTTOP:
            $image->rotateImage("#000", 90);
            break;
        case Imagick::ORIENTATION_RIGHTBOTTOM:
            $image->flopImage();
            $image->rotateImage("#000", 90);
            break;
        case Imagick::ORIENTATION_LEFTBOTTOM:
            $image->rotateImage("#000", -90);
            break;
        default: // Invalid orientation
            break;
      }
      $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);

      $image->stripImage(); // if you want to get rid of all EXIF data
        $image->writeImage();
        $image->clear();
        $image->destroy();
      return $image;

    }
    catch(Exception $e) {
        return 'Exception caught: ',  $e->getMessage(), "\n";
    }  
  }
  else
  {
      return "Imagick extension is not installed.";
  }
}
  

// now run autorotate() function with source of file as parameter
autorotate('my_img.jpg');

Or you can also fix it with just one line if you have root access to your linux server. Just run following shell command:

convert image.jpg -auto-orient output.jpg

 

Posted on Leave a comment

How to remove index.php from codeigniter in UBUNTU?

This tutorial will show you that how to remove index.php from codeigniter in UBUNTU or how to enable url rewriting.

Step 1 :

Add this in htaccess file

<IfModule mod_rewrite.c>
  RewriteEngine On
  #RewriteBase /

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

Step 2 :

Remove index.php in codeigniter config

$config['index_page'] = '';

Step 3 :

Allow overriding htaccess in Apache Configuration (Command)

sudo nano /etc/apache2/apache2.conf

and edit the file & change to

AllowOverride All

for www folder

Step 4 :

Enabled apache mod rewrite (Command)

sudo a2enmod rewrite

Step 5 :

Restart Apache (Command)

sudo /etc/init.d/apache2 restart

Source

Posted on Leave a comment

SSH: How to stop a script running in the background?

  1. Open/Login SSH into the server
  2. Type in ps x
  3. See the list of processes currently on your server
  4. Get the PID(process id) from that list and type kill xxxxx
  5. That should do it.

Source: StackOverflow
Tested on Linux / CentOS 6.7 x 64

Posted on Leave a comment

PHP: How to run / execute a script in the background forever through SSH ?

You can execute a php script in the background forever by using SSH through PHP but for that you have to make sure first that SSH2 extension is available.

PHP DOCS: http://www.php.net/manual/en/function.ssh2-exec.php

$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

/* if you want to execute script from a different directory then use commands in same line separated 
   by ';', that is required in php.
   In below second command '&' will do the magic to run command forever.
*/ 
$stream = ssh2_exec($connection, 'cd httpdocs/subdir/dir; /path/to/php server.php &'); 
$stream = ssh2_exec($connection, 'ps aux | grep server.php'); 

// printing ssh output on screen
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out); 
// closing ssh connection
$stream = ssh2_exec($connection, 'exit'); 
unset($connection);

If you want to do same thing through SSH Terminal (PuTTY) then follow this tutorial:
SSH: how to run a PHP script in background forever in Linux (CentOS) ?

Posted on Leave a comment

SSH: how to run a PHP script in background forever in Linux (CentOS) ?

If you want to run a PHP script in the background forever through SSH then follow these steps:

If you are using a SSH terminal (PuTTY) then directly run following command:

// Notice '&'; that operator will help to run this script forever.
php server.php &

// make sure that you are in the root directory of server.php

ssh outpout

Now your script is running in background and will keep running indefinitely even after closing terminal.

To check if your script is running or not You can run following command:

ps aux | grep server.php

Screenshot_1

If you want to do same thing through PHP instead of terminal then follow this tutorial:
PHP: How to run / execute a script in the background forever through SSH ?

Posted on Leave a comment

CentOS: how to execute a php file with different php version than the php version of OS

To execute a php file with different php version than the version installed on the main php of your Server follow this:

You should know that where is that php version located so that you can add the path.

In my case I want to use PHP 7 that is installed in the plesk hosting panel folder so I’ll run following command:

 /opt/plesk/php/7.0/bin/php server.php

In above code server.php is the file which we want to execute. Just enter above command in the ssh terminal and it will execute successfully.

If your php is located in some other location then get that path and run in the same manner as I did above.

Testing Environment:

OS: CentOS 6.7 x 64