Upgrade PHP version for Apache on Ubuntu server

One day, you found that your PHP version is too old and vulnerable. You have to upgrade to latest PHP version to avoid being exploited. You can follow the instructions below. Just a few commands.

Configure apache point to new version of PHP

**Important note**: List all your loaded modules in your old php by checking your phpinfo. The configure the same into new php in order to make sure your application have all needed modules for use.

a2dismod [current_php] ==> dis-mode current php version

root@ip-xxx-xx-x-xxx:/xxx/xxx/www# a2dismod php5 

perl: warning: Setting locale failed.

perl: warning: Please check that your locale settings:

 LANGUAGE = (unset),

 LC_ALL = (unset),

 LC_CTYPE = "UTF-8",

 LANG = "en_US.UTF-8"

    are supported and installed on your system.

perl: warning: Falling back to the standard locale ("C").

Module php5 disabled.

To activate the new configuration, you need to run:

  service apache2 restart

a2enmod [new_php] ==> en-mode to new php version

root@ip-xxx-xx-x-xxx:/xxxx/xxxx/www# a2enmod php5.6 

perl: warning: Setting locale failed.

perl: warning: Please check that your locale settings:

 LANGUAGE = (unset),

 LC_ALL = (unset),

 LC_CTYPE = "UTF-8",

 LANG = "en_US.UTF-8"

    are supported and installed on your system.

perl: warning: Falling back to the standard locale ("C").

Considering dependency mpm_prefork for php5.6:

Considering conflict mpm_event for mpm_prefork:

Considering conflict mpm_worker for mpm_prefork:

Considering conflict mpm_itk for mpm_prefork:

Module mpm_prefork already enabled

Considering conflict php5 for php5.6:

Enabling module php5.6.

To activate the new configuration, you need to run:

  service apache2 restart

You have to restart your apache to take effect.

root@ip-xxx-xx-x-xxx:/xxxx/xxxx/www#  service apache2 restart

 * Restarting web server apache2 

You can now check your phpinfo to see the new php version and loaded modules

Configure console PHP to new version of PHP for your cronjob or cli program

root@ip-xxx-xx-x-xxx:/xxxx/xxxx/www# ln -sf /usr/bin/php[version] /etc/alternatives/php

You can use ‘which’ php command to see what is your current php command point to

That’s all. Hope it help.

Upgrade PHP version for Apache on Ubuntu server