DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Frameworks
  4. PHP 7.0 (and 5.6) on Ubuntu

PHP 7.0 (and 5.6) on Ubuntu

PHP 7 is here! Running Ubuntu? Don't want to wait for the official distro package? Check out this PPA for Ubuntu that allows both PHP 5.6 and PHP 7.0 to be installed and run side-by-side!

Lorna Mitchell user avatar by
Lorna Mitchell
·
Feb. 19, 16 · Tutorial
Like (5)
Save
Tweet
Share
12.90K Views

Join the DZone community and get the full member experience.

Join For Free

PHP 7 is released but for those of us who don't usually compile our own PHP, it can be a long wait for our preferred distro to release the packages we want. For Ubuntu, I'm using a PPA which allows both PHP 5.6 and PHP 7.0 to be installed, including things like extensions, at the same time. It was very easy to set up (I'm running Ubuntu 15.10, but this process should also work on older versions back to at least 14.04 which is the previous LTS) so here's a quick walkthrough of what I did.

Add the PPA

The PHP 5.6 and PHP 7.0 packages are from a third party PPA, not provided by the official Ubuntu repositories from Canonical. The PPAs I'm recommending here are from Ondřej Surý who packages PHP for Debian (which is then used by Ubuntu) so while it's not an official repo, he's not exactly random! The PPA itself is here: https://launchpad.net/~ondrej/+archive/ubuntu/php

Complete aside, but an important one: The work of the packagers is voluntary and they enable the rest of us to do whatever it is that we do. If you want us to keep being able to have nice things, consider the donation page for this project, please. https://deb.sury.org/pages/donate.html

To add the PPA to your setup:

sudo add-apt-repository ppa:ondrej/php

Then we'll also want to grab information about what is on offer from this new PPA, so then run:

sudo apt-get update

Install New PHP Versions

I already had some of the php5 packages installed, but I didn't uninstall anything, I just let apt work out what it wanted to do when I asked it to install the new versions:

sudo apt-get install php5.6 php7.0

This resulted in a lot of complaining from apt and lots of conflicts. The first suggested resolution was to remove all the stock php5 packages so that PHP 5.6 could be installed—so I just accepted the first suggestion.

I use Apache, so this setup gave me Apache with both PHP 5.6 and PHP 7.0 modules available, and the PHP 5.6 module actually loaded.

As well as just PHP itself, all the extensions and other tools you'd expect with PHP are there for both versions of PHP, so it's very easy to add in the modules that you need. I was very, very impressed with how nicely this is done.

Configuring and Switching Versions

Now you have two completely separate versions of PHP installed on your system, so let's have a look at where all the pieces went!

The config files are all in /etc/php/5.6 and /etc/php/7.0 respectively—inside here is where you can configure which extensions are loaded, set the ini settings, and everything else for each version in isolation.

I'm an Apache user, and, as I mentioned, both modules are available. So, to switch from one to the other I need to do:

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

For NGINX users, the changes are almost as easy. Digital Ocean have good documentation on this (they do have great docs!) so check out their guide: https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-ubuntu-14-04 as it includes a section on reconfiguring NGINX to use another version of PHP.

From the commandline, I have both php5.6 and php7.0 available as commands. I also still have a php command—look in /etc/alternatives to see that it symlinks to a particular version of PHP cli*. You can also quickly check which yours is using by running php -v.

* more specifically, run which php to see which version of PHP is being used—but, this will probably point to /usr/bin/php, which for me is itself a symlink to the /etc/alternatives/php command.

Working With Extensions

This PPA comes with the usual php-pear package which offers the pecl command for both versions of PHP, so any extensions that are available via PECL can be installed in the usual way. You will also need the relevant headers, so either php5.6-dev or php7.0-dev should be installed.

When the pecl installation completes, you'll get a note to add the *.so file to your php.ini; in fact, the best thing to do here is to look at what's in /etc/php/mods-available. There will be some modules already here, each in its own file named after the extension and ending in .ini. You can copy one to use as a template or create your own and put all the relevant configuration for the extension in it (as a minimum, you need extension=[extensionName].so).

Once the new extension is in mods available, enable and then check it by doing:

sudo phpenmod extension
php -m

This will create the symlinks in the right places for your current version of PHP to load this module, and you should see it in the list of modules output by the php -m. Pro tip: if you don't see it in the output, scroll all the way to the top of the output and see if there are any useful error messages there.

There are some extensions that aren't currently available either by default or via pecl, so I'll write a followup post on compiling an extension from source to use with your PHP.

PHP ubuntu

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Important Data Structures and Algorithms for Data Engineers
  • Java Code Review Solution
  • Java REST API Frameworks
  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: