Skip to main content
Image
webdevlopment-nginx-opensenselabs.jpg

HowTo: Change the PHP version on Apache or Nginx

article publisher

Raman

Technology

New versions of PHP bring new features and fixes for security issues and bugs. Migrating from one version to another can often lead to incompatibilities. As of writing this article, PHP 7.2 is the latest stable release and PHP 7.3 is expected to be released in December 2018. Drupal.org recommends PHP 7.1+ for new projects and encourages the community to plan an upgrade for older versions.

Drupal, as we know, is a CMF written in PHP. It uses various PHP libraries and components including the Symfony framework, the Twig template engine, PHPUnit, Guzzle, etc. Hence, it is always recommended to do a compatibility check with all the components, libraries and core/contributed modules of your Drupal site before upgrading to newer versions of PHP.

Now, the process of changing the PHP version of your Drupal site may vary with your hosting provider and the access level that you have on the server. But for this tutorial, Ubuntu 16.04.4 LTS with Apache 2.4.18 and Nginx 1.10.3 was used. Few commands, configuration files, and directory paths may differ with your system and the version of web servers.

Checking the current PHP version

First and foremost, let us see what all versions of PHP are currently installed on our system.

$ update-alternatives --display php
command line for raman@raman ubuntu


You may also check all the installed PHP packages along with its extensions using:

$ apt list --installed | grep "php*"

By default, the one with the highest priority will be the command line version. That is, if we use the interactive shell or parse a PHP file through the terminal, this version will be used.

$ php -v
command line for raman@raman ubuntu


However, this selected command line version may/may not be the configured with your web server. To check the PHP version configured with the web server, you may create a phpinfo() page or echo out the PHP_VERSION_ID constant / phpversion() function.

$ cd /var/www/html/drupal8
$ drush php-eval "echo phpversion();
command line for raman@raman ubuntu

 

List of PHP configurations provided by phpinfo()
List of PHP configurations provided by phpinfo()

If you do not have the access to your server through SSH, you may also view the current PHP version using the Status Reports of your Drupal site available under /admin/reports/status.

Checking the PHP version using Drupal’s Status Report
Checking the PHP version using Drupal’s Status Report


Why Update to PHP 7?

Here are some of the reasons why you need to upgrade from PHP 5 to PHP 7:

  1. PHP 7 is twice as fast as PHP 5. The huge upward shift in performance is one of the reasons why an upgrade is important. This means your website loads in lesser time, giving your web users another reason to stay on your website. 
     
  2. PHP 7 supports simultaneous execution of asynchronous tasks. Tasks such as networks, access to the database, timers and perform events related to I/O operations can be done asynchronously by putting in place a single PHP event-loop. 
     
  3. It is mobile-first. Of course, you need to offer a better value proposition to your users. And mobile users cant be ignored here. PHP 7 offers exactly what businesses need to do to cater to the needs of mobile device users.

    It offers reduced memory usage, execution engine improvements, and native local threat storage which are specifically suited to mobile devices that have limited browsing features. 
     
  4. Uses lesser memory. PHP 7 has demonstrated that technology infrastructure can process higher computing requests without consuming proportionately higher memory. 

How to change the PHP version?

Since Apache and Nginx are the most popular web servers in the market, we will have a quick look on how to change the PHP version on them. Also, it is highly recommended to have a backup of your Drupal site before tinkering around with any of the configurations on your server.

Now, in my case, I have PHP 7.0 configured on the web server and Drupal is giving a nice recommendation to upgrade it. But I do not have the latest PHP 7.2 installed on my system, so, I will first download it along with the required PHP extensions.

These include extensions for URL, JSON, Image, Library, Mbstring, Open SSL, and XML. But, just to ensure that all the extensions get installed, let’s install them manually.

$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install php7.2 php7.2-fpm php7.2-xml php7.2-mysql php7.2-gd

You may view the packages using dpkg -l php7.2* to confirm.

I. Command Line Version

To change the command line version of PHP you may use the following command. Again, changing this won’t affect the version used by the web server.

$ sudo update-alternatives --config php
command line for raman@raman ubuntu


Enter the choice number mentioned besides the desired PHP version

or

$ sudo update-alternatives -set php /usr/bin/php7.2


II.  Apache

For Apache web server, the a2enmod and a2dismod scripts can be used for enabling and disabling the PHP modules.
Disable all the previously enabled PHP modules

$ sudo a2dismod php5.6
$ sudo a2dismod php7.0
$ sudo a2dismod php7.1

Enable the new PHP version module

$ sudo a2enmod php7.2

Restart the web server    

$ sudo service apache2 restart

 

PHP version for a specific website can also be set through to the .htaccess file in the following way.

AddHandler application/x-httpd-php72 .php


III. Nginx

For Nginx, we simply need to update the PHP-FPM socket in its configuration file. But before that make sure that the PHP-FPM is installed for your version and is running as a service.

Take a backup of the default configuration file and then open it up in your favourite text editor.

$ cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak
$ sudo vim /etc/nginx/sites-available/default


Change the FastCGI backend to use the new PHP-FPM socket, save and exit the file

location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

Run the configuration test

$ nginx -t

Restart the web server

$ sudo service nginx restart

We have now successfully changed the PHP version of our Drupal site. Now, navigate to Manage → Reports → Status report to ensure that the version has been changed and there are no errors or warning from any Drupal module or PHP library.

Observe that the PHP version has been updated to 7.2.9-1
Observe that the PHP version has been updated to 7.2.9-1

 
As expected, it did not produce any errors or warnings on a raw Drupal 8 installation. If you face any issues, make sure you installed all the required PHP extensions for your PHP version, check the log messages and refer the issue queues of the core and contrib modules.

For a free website analysis and the best Drupal migration plan for your company's needs, speak to our Drupal experts today.

 

Subscribe

Ready to start your digital transformation journey with us?

Related Blogs

Debunking 6 Common Software Testing Myths

Common%20Misconceptions%20about%20Testing.png

A flawless product delivery requires a perfect combination of both development and testing efforts. Testing plays a vital role in…

With Ant Design, Create React Components Like a Pro

With%20Ant%20Design%2C%20Create%20React%20Components%20Like%20a%20Pro.png

Most enterprise-grade solutions depend on the React stack to create a robust platform for improved delivery and performance. React is one…

Boost developer productivity with Chakra UI

What%20is%20a%20chakra%20%281%29.png

React component libraries are helpful tools in creating stunning interfaces for react-based applications. Though each website component…