Changing Apache’s PHP version in Ubuntu 16.04 (Xenial Xerus)

Maybe you’ve also encountered that php versions’ are not the same on Apache web server & Command line. Because of installing softwares/packages from command line in Linux, usually command line PHP gets updated, without affecting PHP module used by Apache.

Say, My machine in Linux mint which is built on Ubuntu 16.04 (Xenial Xerus) & we are about to change Apache’s php to version PHP7.0.

In such circumstances, you need to make sure of missing PHP module for Apache. To do so, jump into terminal & run:

ls -l /usr/lib/apache2/modules/ | grep libphp

If you couldn’t find sth like ‘libphp7.0.so’, it means you don’t have the required module. So, first install it using:

sudo apt-get install libapache2-mod-php7.0 --fix-missing

after installing the module, if you ls Apache modules directory, this time you’ll see that ‘libphp7.0.so’ is present.

Now that you have the required module, you should disable the existing php module version (e.g. php5.6) used by Apache:

sudo a2dismod php5.6

and enabling desired version:

sudo a2enmod php7.0

So far so good. What we’ve done so far, would suffice for websites & web apps without database(e.g. mysql) dependency. But what if your website uses a mysql database as its data storage? you have to install mysql connection library for PHP7.0. To do so, run following command & you’ll have databases connected to the new PHP interpreter again:

sudo apt-get install php7.0-mysql

Alright, everything is OK. Just one step to embrace php7.0! restart Apache:

sudo service apache2 restart

Well done! you can use phpinfo() to check for php version.

As final note, lets browse directory of PHP7.0 modules. Do you know Where it is? if you browse into /usr/lib/php, you’ll find a some folders named using a date. What are these weird folders? Each of them are representing a particular version of PHP & are called Internal API version:

PHP Version PHP Extension (Internal API version)
PHP 5.6 20131226
PHP 7.0 20151012

If you look into 20151012 folder, you’ll see some files with .so extensions, these are php extensions. Then for installing a php extension that you are not sure about its name, you may type `sudo apt-get install php7.0` & press tab key to see available modules to install. Just, don’t forget to restart Apache server or things won’t take effect until next system restart!

Alright, we’ve done with upgrading php version in Linux Mint(Ubuntu 16.4 – Xenial Xerus) &I hope you’re comfortable with your upgraded machine. Happy coding php7.0!