Upgrading PHP Version on Ubuntu 14.04

Once it was necessary to upgrade the version of PHP 5.5.9 to 5.6 on Ubuntu Server 14.04 LTS, the usual update of the system components did not help:

sudo apt-get update
sudo apt-get upgrade

You can try to upgrade the system to 16.04 or higher as I described in the article Updating Ubuntu 14.04 to 16.04. Together with the system will be updated and PHP.

If the system update fails, you can add a third-party source with PHP:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

And install the correct version from it, for example PHP 5.6:

sudo apt-get install php5.6 php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml

Since there are several installed versions, disable the old version and activate the installed one:

sudo a2dismod php5
sudo a2enmod php5.6
sudo service apache2 restart

Similarly, you can install PHP 7.2:

sudo apt-get install php7.2
sudo a2dismod php5.6
sudo a2enmod php7.2
sudo service apache2 restart

Or PHP 7.0:

sudo apt-get install php7.0
sudo a2dismod php7.2
sudo a2enmod php7.0
sudo service apache2 restart

Troubleshooting PHP Warning “Permission denied /var/cpanel/php/sessions/ea-php56/”

Once I updated EasyApache 3 to EasyApache 4 in cPanel and noticed in the PHP logs the following:

[29-Mar-2018 15:54:45 UTC] PHP Warning:  Unknown: open(/var/cpanel/php/sessions/ea-php56/sess_3d96o7nnlnnr473p8619vqkdm1, O_RDWR) failed: Permission denied (13) in Unknown on line 0
[29-Mar-2018 15:54:45 UTC] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/cpanel/php/sessions/ea-php56) in Unknown on line 0

The warning occurs because PHP can not write session files to a directory to which it does not have access rights.
I looked at the rights of this directory, the group and owner was root, and it was also allowed to write and execute for everyone, except reading.

To resolve this warning, you can open full access for everyone:

chmod 777 /var/cpanel/php/sessions/ea-php56

Or open a PHP configuration, for example in the nano editor:

nano /opt/cpanel/ea-php56/root/etc/php.ini

To find:

session.save_path = "/var/cpanel/php/sessions/ea-php56"

And change to:

session.save_path = "/tmp"

That used to be on the old EasyApache3 in the PHP configuration /usr/local/lib/php.ini.

After that, the warning did not appear.

See also:
Migration from EasyApache 3 to EasyApache 4

How to enable PHP short tags?

It was necessary recently to include short PHP tags since the site code that I transferred to another server was written with their use.
By default, they are disabled.
For example, the usual tags look like this:

<?php  code  ?>

And short ones like this:

<? code ?>

To include short ones, you need to find the short_open_tag parameter in the php.ini configuration file and specify its value in On, for example:

short_open_tag=On

php.ini is usually located in the /etc/php5/directory, if cpanel is used, in /usr/local/lib/php.ini.

You need to reboot the web server to apply the change, in Ubuntu apache2 is rebooted with the command:

sudo service apache2 restart

The solution of the error “PDOException “could not find driver””

I noticed somehow the following error:

PDOException “could not find driver”

The reason may be not activated pdo_mysql or not installed php5-mysql.
Let’s look at the PDO:

php -i|grep PDO

In php.ini or connected configuration files there should be a line:

extension=pdo.so
extension=pdo_mysql.so

Let’s see the information about php5-mysql:

dpkg --get-selections | grep php5-mysql

If not installed, then install, in Ubuntu it can be done like this:

sudo apt-get install php5-mysql
sudo apt-get install php-mysql
sudo /etc/init.d/apache2 restart
sudo service apache2 restart

Comments in PHP

In PHP, comments can be of three types.

I will give an example of the first (one-line in the style of C ++):

<?php
echo "TEXT"; // Comment
?>

The second one in Unix style:

<?php
echo "TEXT"; # Comment
?>

The third multiline:

<?php
     /* Comment
        Comment
        Comment */
     echo "TEXT";
?>

PHP. Redirect to another page

I’ll give an example of redirecting to another page:

<?php
  header("Location: http://www.example.com/");
  exit;
?>

Example of redirecting in specified number of seconds:

<?php
  header('Refresh: 5; URL=http://www.example.com/');
  echo 'After 5 seconds you will be automatically redirected to another page.';
  exit;
?>

Example of Redirect in JavaScript (this may not work for everyone):

<script type="text/javascript">
  location.replace("http://www.example.com/");
</script>

Example Redirect in JavaScript after 5 seconds (this may not work for everyone):

<script type="text/javascript">
  setTimeout('location.replace("http://www.example.com/")', 5000);
</script>

An example of Redirect in HTML after 5 seconds (if you specify 0, it will redirect immediately):

<meta http-equiv="refresh" content="5; url=http://www.example.com/">

How to install PHP 5.6 in Ubuntu 16

In Ubuntu 16, installing PHP with apt-get install php installs PHP version 7, so to install PHP 5.6, you can add a third-party repository.

If the system has PHP 7 installed, then you can delete all of its components like this:

sudo apt-get purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`

Now add a third-party source:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

And install from it PHP 5.6:

sudo apt-get update
sudo apt-get install php5.6

Similarly, you can install modules, for example:

sudo apt-get install php5.6-mysql php5.6-mcrypt php5.6-mbstring php5.6-xml

To check the installed version of PHP, perform:

sudo php -v

See also my article:
Upgrading PHP Version on Ubuntu 14.04

SMS sending script via Goip4 gateway

Here is an example of a script written in PHP, for sending SMS messages through the Goip4 gateway.
The script receives data from the SQL database with a query and alternately sends SMS to each number, and also writes an entry about sending it to a special sms table.
Continue reading “SMS sending script via Goip4 gateway”