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.
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.
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.
mod_php
with ApacheThis 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
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
.
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
.
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
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
OutputPHP 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:
<?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
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Thank you for the article. I had to also run sudo
apt-get install libapache2-mod-php7.0
before I got this to work in apache, nginx did work with without any prolemsHi there, and thanks for the tutorial. I have upgraded mine droplet to php 7, and so far so good. After upgrade I bumped into a problem: Error 502.
I figured out that php7.0-fpm.sock in my installation ended up in: /var/run/php/php7.0-fpm.sock (instead of /var/run/php7.0-fpm.sock like suggested in tutorial)
So I fixed my path in nginx.conf
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
After I updated my path in server block, I got error:
*143 connect() to unix:/var/run/php/php7.0-fpm.sock failed (13: Permission denied) ...
So I updated permissions to
sudo chmod 666 /var/run/php/php7.0-fpm.sock
, and now everything worksSo my question here is about the permissions, is this ok to have it 666? Or anyone could suggest some better solution to my problem.
And one more thing, php folder in /var/run/php/php7.0-fpm.sock is ownde by www-data:
drwxr-xr-x 2 www-data www-data 80 Mar 14 13:41 php
Should it be owned by root, like all others in same folder?
Thanks in advanced
Edit:
sudo chown -R nginx:nginx php/
which is mine nginx user, and restored permissions to 0644 for /var/run/php/php7.0-fpm.sockAgain everything works
I installed PHP 7 on Apache prior removing PHP 5.6.
$ php -v
yield php 7 version, on browser’s phpinfo(), I got php 5.6. So I removed PHP 5.6, and then executed:Brennen, could you please change the repositories to ppa:ondrej/php, I will deprecate ppa:ondrej/php-7.0 repository at some point in time as it contains exactly the same PHP 7.0 packages as ppa:ondrej/php.
I followed the instructions on this article to a T but I keep getting the error E: Unable to locate package php7.0-fpm.
Shouldn’t the package be there if I have already add
I follow above steps but after reboot system , mysql can not start : OS: UBuntu 14 PHP 5. 5 upgraded to PHP 7 PLease kindly review to fix it .
Below is the log content :
2019/01/11 00:12:21 [error] 14188#0: *5 FastCGI sent in stderr: “PHP message: PHP Notice: Undefined variable: _SESSION in /usr/local/vesta/web/softaculous/index.php on line 27 PHP message: PHP Notice: Undefined variable: _SESSION in /usr/local/vesta/web/softaculous/index.php on line 32 PHP message: PHP Notice: Undefined variable: _SESSION in /usr/local/vesta/web/softaculous/index.php on line 35 PHP message: PHP Notice: Undefined variable: _SESSION in /usr/local/vesta/web/softaculous/index.php on line 60 PHP message: PHP Notice: Use of undefined constant VESTA_CMD - assumed ‘VESTA_CMD’ in /usr/local/vesta/web/softaculous/index.php on line 65 PHP message: PHP Notice: Use of undefined constant VESTA_CMD - assumed ‘VESTA_CMD’ in /usr/local/vesta/web/softaculous/index.php on line 76 PHP message: PHP Warning: array_keys() expects parameter 1 to be array, null given in /usr/local/vesta/web/softaculous/index.php on line 85” while reading response header from upstream, client: 127.0.0.1, server: _, request: “POST /softaculous/index.php?act=cp_api&api=serialize HTTP/1.1”, upstream: “fastcgi://unix:/var/run/vesta-php.sock:”, host: “127.0.0.1:8083” 2019/01/11 00:12:21 [error] 14188#0: *7 FastCGI sent in stderr: “PHP message: PHP Notice: Undefined variable: _SESSION in /usr/local/vesta/web/softaculous/index.php on line 27 PHP message: PHP Notice: Undefined variable: _SESSION in /usr/local/vesta/web/softaculous/index.php on line 32 PHP message: PHP Notice: Undefined variable: _SESSION in /usr/local/vesta/web/softaculous/index.php on line 35 PHP message: PHP Notice: Undefined variable: _SESSION in /usr/local/vesta/web/softaculous/index.php on line 60 PHP message: PHP Notice: Use of undefined constant VESTA_CMD - assumed ‘VESTA_CMD’ in /usr/local/vesta/web/softaculous/index.php on line 65 PHP message: PHP Notice: Use of undefined constant VESTA_CMD - assumed ‘VESTA_CMD’ in /usr/local/vesta/web/softaculous/index.php on line 76 PHP message: PHP Warning: array_keys() expects parameter 1 to be array, null given in /usr/local/vesta/web/softaculous/index.php on line 85” while reading response header from upstream, client: 127.0.0.1, server: _, request: “POST /softaculous/index.php?act=cp_api&api=serialize HTTP/1.1”, upstream: “fastcgi://unix:/var/run/vesta-php.sock:”, host: “127.0.0.1:8083” 2019/01/11 00:12:21 [error] 14188#0: *9 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined variable: _SESSION in /usr/local/vesta/web/softaculous/index.php on line 27 PHP message: PHP Notice: Undefined variable: _SESSION in /usr/local/vesta/web/softaculous/index.php on line 32
Hi, I am trying to upgrade from PHP 5.6 to PHP 7.1 on my laravel forge (digital ocean) project. I did everything until: sudo nano /etc/nginx/sites-enabled/default But it looks like my /etc/nginx/sites-enabled/default is empty. Is that possible? What should I do? Thank you !!
No that you might also need to tell Apache to start using php7
For everyone visiting this tutorial in 2018:
This didn’t work for me for Apache. There were no errors.
php -v
on the command line showed version 7. Butphpinfo();
still showed 5.6. I figured out that the last steps of the tutorial are missing.I had to run
sudo a2enmod php7.0
to activate the php7 module. Then runsudo service apache2 restart
to restart Apache.