2015 PHP 7 released on December 3, 2012 promises a significant speed improvement compared with previous versions of the language, as well as new features such as scalar type hints. This guide describes how to quickly upgrade an Apache or Nginx web server running PHP 5.x (any version) to PHP 7.
**Warning: **Like most major language versions, it is best to wait a while before switching to PHP 7 in production. At the same time, now is a good time to test the compatibility of the application with the new version, perform benchmark tests, and become familiar with the new language features.
If you are running any service or application with active users, the safest thing to do is to test this process in a staging environment first.
An Ubuntu server with a non-root account that can use the sudo
command has been set up, and the firewall has been turned on. Students who don’t have a server can buy it from here, but I personally recommend you to use the free Tencent Cloud Developer Lab for experimentation, and then buy server.
This guide assumes that you are running PHP 5.x on an Ubuntu 14.04 computer. You can use mod_php
with Apache or PHP-FPM with Nginx. It also assumes that you have configured sudo
permissions for administrative tasks for non-root users.
A Personal Package Archive, or PPA, is hosted in an appropriate repository [Quick Start] (https://launchpad.net/). PPA allows third-party developers to build and distribute packages for Ubuntu outside of official channels. They are usually a useful source of beta software, modified builds, and backports to older operating systems.
Ondřej Surý maintains the Debian PHP package and provides PPA PHP 7.0 on Ubuntu. Before doing anything else, please log in to your system and add Ondřej's PPA to the system's Apt source:
sudo add-apt-repository ppa:ondrej/php
You will see the description of the PPA and then prompt to continue. Press Enter to continue.
**Note: **If the system's locale is set to any language other than UTF-8, adding PPA may fail due to incorrect handling of characters in the author's name. As a workaround, you can install language-pack-en-base
to ensure that the locale is generated, and to override the system-wide locale setting when adding PPA:
sudo apt-get install -y language-pack-en-base
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
After installing PPA, update the local package cache to include its contents:
sudo apt-get update
Now that we can access the PHP 7.0 package, we can replace the existing PHP installation.
mod_php
This section introduces the upgrade process of a system that uses Apache as a web server and mod_php
to execute PHP code. Conversely, if you are running Nginx and PHP-FPM, skip to the next section.
First, install the new package. This will upgrade all important PHP packages, except php5-mysql
which will be deleted.
sudo apt-get install php7.0
**Note: **If you make extensive changes to any configuration files in /etc/php5/
, those files still exist and can be referenced. The configuration file for PHP 7.0 now exists in /etc/php/7.0
.
If you are using MySQL, please make sure to re-add the updated PHP MySQL binding:
sudo apt-get install php7.0-mysql
This section introduces the upgrade process of a system that uses Nginx as a 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 will be prompted to continue. Press Enter to complete the installation.
If you are using MySQL, be sure to reinstall the PHP MySQL binding:
sudo apt-get install php7.0-mysql
**Note: **If you make extensive changes to any configuration files in /etc/php5/
, those files still exist and can be referenced. The configuration file for PHP 7.0 now exists in /etc/php/7.0
.
Nginx uses [Unix domain socket] (https://en.wikipedia.org/wiki/Unix_domain_socket) to communicate with PHP-FPM. The socket is mapped to a path on the file system. Our PHP 7 installation uses the 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 an editor of your choice):
sudo nano /etc/nginx/sites-enabled/default
Your configuration may be different. Look for a block beginning with location ~ \.php$ {
and a line similar to fastcgi_pass unix:/var/run/php5-fpm.sock;
. Change 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 500502503504/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 do this by pressing Ctrl-X to exit, pressing y to confirm, and then pressing Enter to confirm the file name to be overwritten.
You should repeat this process for any other virtual sites in /etc/nginx/sites-enabled
that need to support PHP.
Now we can restart nginx
:
sudo service nginx restart
After configuring the web server and installing the new package, we should be able to verify that PHP is up and running. First check the installed PHP version on the command line:
php -v
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 test files in the document root directory of the web server. Depending on your server and configuration, this may be one of the following:
/var/www/html
/var/www/
/usr/share/nginx/html
Use nano
to open the new file called in the document root directory of info.php
. By default, on Apache, this will be:
sudo nano /var/www/html/info.php
On Nginx, you can use:
sudo nano /usr/share/nginx/html/info.php
Paste the following code:
<? php
phpinfo();?>
Exit the editor and save info.php
. Now, load the following address in the browser:
http://server_domain_name_or_IP/info.php
You should see the PHP version and configuration information for PHP 7. Once you have double checked this, the safest thing is to delete info.php
:
sudo rm /var/www/html/info.php
You have now installed PHP 7. Check the Official Migration Guide here.
For more Ubuntu tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How To Upgrade to PHP 7 on Ubuntu 14.04"
Recommended Posts