Introduction
PHP 7, which was released on December 3, 2015, promises substantial speed improvements over previous versions of the language, along with new features like scalar type hinting. This guide explains how to quickly upgrade an Apache or Nginx web server running PHP 5.x (any release) to PHP 7.
Warning: As with most major-version language releases, it's best to wait a little while before switching to PHP 7 in production. In the meanwhile, it's a good time to test your applications for compatibility with the new release, perform benchmarks, and familiarize yourself with new language features.
If you're running any services or applications with active users, it is safest to first test this process in a staging environment.
Prerequisites
This guide assumes that you are running PHP 5.x on an Ubuntu 14.04 machine, using either mod_php
in conjunction with Apache, or PHP-FPM in conjunction with Nginx. It also assumes that you have a non-root user configured with sudo
privileges for administrative tasks.
Adding a PPA for PHP 7.0 Packages
A Personal Package Archive, or PPA, is an Apt repository hosted on Launchpad. PPAs allow third-party developers to build and distribute packages for Ubuntu outside of the official channels. They're often useful sources of beta software, modified builds, and backports to older releases of the operating system.
Ondřej Surý maintains the PHP packages for Debian, and offers a PPA for PHP 7.0 on Ubuntu. Before doing anything else, log in to your system, and add Ondřej's PPA to the system's Apt sources:
- sudo add-apt-repository ppa:ondrej/php
You'll see a description of the PPA, followed by a prompt to continue. Press Enter to proceed.
Note: If your system's locale is set to anything other than UTF-8, adding the PPA may fail due to a bug handling characters in the author's name. As a workaround, you can install language-pack-en-base
to make sure that locales are generated, and override system-wide locale settings while adding the PPA:
- sudo apt-get install -y language-pack-en-base
- sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
Once the PPA is installed, update the local package cache to include its contents:
- sudo apt-get update
Now that we have access to packages for PHP 7.0, we can replace the existing PHP installation.
Upgrading mod_php
with Apache
This section describes the upgrade process for a system using Apache as the web server and mod_php
to execute PHP code. If, instead, you are running Nginx and PHP-FPM, skip ahead to the next section.
First, install the new packages. This will upgrade all of the important PHP packages, with the exception of php5-mysql
, which will be removed.
- sudo apt-get install php7.0
Note: If you have made substantial modifications to any configuration files in /etc/php5/
, those files are still in place, and can be referenced. Configuration files for PHP 7.0 now live in /etc/php/7.0
.
If you are using MySQL, make sure to re-add the updated PHP MySQL bindings:
- sudo apt-get install php7.0-mysql
Upgrading PHP-FPM with Nginx
This section describes the upgrade process for a system using Nginx as the web server and PHP-FPM to execute PHP code.
First, install the new PHP-FPM package and its dependencies:
- sudo apt-get install php7.0-fpm
You'll be prompted to continue. Press Enter to complete the installation.
If you are using MySQL, be sure to re-install the PHP MySQL bindings:
- sudo apt-get install php7.0-mysql
Note: If you have made substantial modifications to any configuration files in /etc/php5/
, those files are still in place, and can be referenced. Configuration files for PHP 7.0 now live in /etc/php/7.0
.
Updating Nginx Site(s) to Use New Socket Path
Nginx communicates with PHP-FPM using a Unix domain socket. Sockets map to a path on the filesystem, and our PHP 7 installation uses a new path by default:
PHP 5 | PHP 7 |
---|---|
/var/run/php5-fpm.sock | /var/run/php/php7.0-fpm.sock |
Open the default
site configuration file with nano
(or your editor of choice):
- sudo nano /etc/nginx/sites-enabled/default
Your configuration may differ somewhat. Look for a block beginning with location ~ \.php$ {
, and a line that looks something like fastcgi_pass unix:/var/run/php5-fpm.sock;
. Change this to use unix:/var/run/php/php7.0-fpm.sock
.
/etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
index index.php index.html index.htm;
server_name server_domain_name_or_IP;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Exit and save the file. In nano
, you can accomplish this by pressing Ctrl-X to exit, y to confirm, and Enter to confirm the filename to overwrite.
You should repeat this process for any other virtual sites defined in /etc/nginx/sites-enabled
which need to support PHP.
Now we can restart nginx
:
- sudo service nginx restart
Testing PHP
With a web server configured and the new packages installed, we should be able to verify that PHP is up and running. Begin by checking the installed version of PHP at the command line:
- php -v
Output
PHP 7.0.0-5+deb.sury.org~trusty+1 (cli) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
You can also create a test file in the web server's document root. Depending on your server and configuration, this may be one of:
/var/www/html
/var/www/
/usr/share/nginx/html
Using nano
, open a new file called info.php
in the document root. By default, on Apache, this would be:
- sudo nano /var/www/html/info.php
On Nginx, you might instead use:
- sudo nano /usr/share/nginx/html/info.php
Paste the following code:
info.php
<?php
phpinfo();
?>
Exit the editor, saving info.php
. Now, load the following address in your browser:
http://server_domain_name_or_IP/info.php
You should see PHP version and configuration info for PHP 7. Once you've double-checked this, it's safest to to delete info.php
:
- sudo rm /var/www/html/info.php
Conclusion
You now have a working PHP 7 installation. From here, you may want to check out Erika Heidi's Getting Ready for PHP 7 blog post, and look over the official migration guide.
Janeth Kent
Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.
Related Posts
Examine the 10 key PHP functions I use frequently
PHP never ceases to surprise me with its built-in capabilities. These are a few of the functions I find most fascinating. 1. Levenshtein This function uses the Levenshtein algorithm to calculate the…
How to Track Flight Status in real-time using the Flight Tracker API
The Flight Tracker API provides developers with the ability to access real-time flight status, which is extremely useful for integrating historical tracking or live queries of air traffic into your…
What is a JWT token and how does it work?
JWT tokens are a standard used to create application access tokens, enabling user authentication in web applications. Specifically, it follows the RFC 7519 standard. What is a JWT token A JWT token…
PHP - The Singleton Pattern
The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a method for limiting the number of instances of an object to just one.…
How to Send Email from an HTML Contact Form
In today’s article we will write about how to make a working form that upon hitting that submit button will be functional and send the email (to you as a…
How To Use Varnish As A Highly Available Load Balancer On Ubuntu 20.04 With SSL
Load balancing with high availability can be tough to set up. Fortunately, Varnish HTTP Cache server provides a dead simple highly available load balancer that will also work as a…
The State of PHP 8: new features and changes
PHP 8.0 has been released last November 26: let's discover together the main innovations that the new version introduces in this language. PHP is one of the most popular programming languages…
HTTP Cookies: how they work and how to use them
Today we are going to write about the way to store data in a browser, why websites use cookies and how they work in detail. Continue reading to find out how…
The most popular Array Sorting Algorithms In PHP
There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations,…
MySQL 8.0 is now fully supported in PHP 7.4
MySQL and PHP is a love story that started long time ago. However the love story with MySQL 8.0 was a bit slower to start… but don’t worry it rules…
Linux For Dummies: Permissions
In the previous articles I made a short introduction to the Unix world and in the following article I have dealt with the basic commands for the file system management. Today we are…
Linux for Dummies: Ubuntu Terminal
I introduced in the previous article, available here, the basic concepts concerning the Linux world. Today we are going to have a look to some basic operations that we can perform…