Blog

03/06/2019

How to install PHP 7 on Debian Linux 8.7/7.x [jessie/wheezy]



I wanted to use PHP 7 on Debian 8.x. How do I install and configure PHP 7 on Debian Linux 8.x server? How do I install PHP 7 on Debian Linux 7.x “wheezy” VPS server hosted in cloud?

PHP 7 is a dynamic scripting langauge for delivering applications. You can get 2x faster performance and 50% better memory consumption than PHP 5.6, allowing you to serve more concurrent users without adding any hardware. However, PHP 7 not included with Debian 8.x stable version. In this tutorial, you will learn about installing PHP 7 on Debian 8.7 server. Please note that Debian 9.0 “Stretch” will include PHP 7 by default.

Related: How to install PHP 7 on Ubuntu Linux 14.04 LTS

Steps to install PHP 7 on Debian 8.7

  1. First, remove old PHP 5.6
  2. Configure dotdeb is an extra repository for PHP 7
  3. Install PHP 7
  4. Reconfigure web server
  5. Restart web server

To list your Debian version, enter:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.7 (jessie)
Release: 8.7
Codename: jessie

Use the following syntax to save existing config files:
$ sudo tar zcvf /root/etc.backup.tar.gz /etc/

Step #1: Delete old php 5.6 (if installed)

First, list all installed old php 5.x packages, enter:
$ dpkg --list | grep php | awk '/^ii/{ print $2}'
Sample outputs:

Fig.01: Debian Linux list installed PHP packages on the system

To delete old php 5.6 packages, enter:
$ x="$(dpkg --list | grep php | awk '/^ii/{ print $2}')"
$ sudo apt-get --purge remove $x

Sample outputs:
Fig.02: Deleting old php 5.6 packages

Step #2: Configure dotdeb

Dotdeb is an extra repository providing up-to-date packages (such as PHP 7 and more) for your Debian servers. It supports both Debian 8.x “Jessie” and Debian 7.x “Wheezy”.

Configuration for Debian 8.x “Jessie”

Append the following two lines to /etc/apt/sources.list as root user::
$ sudo -s
# echo 'deb http://packages.dotdeb.org jessie all' >> /etc/apt/sources.list
# echo 'deb-src http://packages.dotdeb.org jessie all' >> /etc/apt/sources.list

Save and close the file. Here is my updated file:
$ cat /etc/apt/sources.list
Sample outputs:

Fig.03: Updated /etc/apt/sources.list for Debian 8.x “jessie”.

Configuration for Debian 7.x “Wheezy”

Append the following two lines to /etc/apt/sources.list as root user:
$ sudo -s
# echo 'deb http://packages.dotdeb.org wheezy all' >> /etc/apt/sources.list
# echo 'deb-src http://packages.dotdeb.org wheezy all' >> /etc/apt/sources.list

Save and close the file.

As of 15/Feb/2017 the above instructions for Debian 7.x “Wheezy” are no longer working. Please use following instructions to compile PHP 7 from the source code:
$ sudo apt-get install git
$ cd /tmp
$ git clone https://github.com/kasparsd/php-7-debian.git
$ cd php-7-debian/
$ ./build.sh

It will take some time to download and compile everything. At the end you should see something as follows:

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
invertedregexiterator.inc
pharcommand.inc
directorytreeiterator.inc
clicommand.inc
directorygraphiterator.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/
Installing PHP CLI binary:        /usr/local/php7/bin/
Installing PHP CLI man page:      /usr/local/php7/php/man/man1/
Installing PHP FPM binary:        /usr/local/php7/sbin/
Installing PHP FPM config:        /usr/local/php7/etc/
Installing PHP FPM man page:      /usr/local/php7/php/man/man8/
Installing PHP FPM status page:   /usr/local/php7/php/php/fpm/
Installing phpdbg binary:         /usr/local/php7/bin/
Installing phpdbg man page:       /usr/local/php7/php/man/man1/
Installing PHP CGI binary:        /usr/local/php7/bin/
Installing PHP CGI man page:      /usr/local/php7/php/man/man1/
Installing build environment:     /usr/local/php7/lib/php/build/
Installing header files:           /usr/local/php7/include/php/
Installing helper programs:       /usr/local/php7/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php7/php/man/man1/
  page: phpize.1
  page: php-config.1
/tmp/php-7-debian/php-src/build/shtool install -c ext/phar/phar.phar /usr/local/php7/bin
ln -s -f phar.phar /usr/local/php7/bin/phar
Installing PDO headers:           /usr/local/php7/include/php/ext/pdo/

To install PHP 7 on Debian 7.x:
$ sudo ./install.sh
Sample outputs:

update-rc.d: using dependency based boot sequencing

You can verify PHP version:
$ /usr/local/php7/bin/php --version
PHP 7.1.1 (cli) (built: Feb 15 2017 18:49:03) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.1.1, Copyright (c) 1999-2017, by Zend Technologies

All PHP configuration files are stored under /usr/local/php7:

/usr/local/php7/lib/php.ini
/usr/local/php7/etc/php-fpm.conf
/usr/local/php7/etc/php-fpm.d/www.conf
/usr/local/php7/etc/conf.d/modules.ini

While the Debian init script is added to:

/etc/init.d/php7-fpm

See this page for more info about using PHP 7 on Debian 7.x. The rest of the following instructions are for Debian version 8.x only.

Step #3: Fetch and install the GnuPG key (“jessie” only)

Type the following commands
$ cd /tmp
$ wget https://www.dotdeb.org/dotdeb.gpg
$ sudo apt-key add dotdeb.gpg
$ rm dotdeb.gpg

Refresh apt database to include new packages:
$ sudo apt-get update
Sample outputs:

Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB]           
Ign http://ftp.us.debian.org stable InRelease                                 
Get:2 http://packages.dotdeb.org jessie InRelease [9,840 B]                   
Hit http://ftp.debian.org jessie-updates InRelease                            
Hit http://ftp.us.debian.org stable Release.gpg                               
Get:3 http://packages.dotdeb.org jessie/all Sources [30.0 kB]                 
Get:4 http://ftp.debian.org jessie-updates/main amd64 Packages/DiffIndex [5,932 B]
Hit http://ftp.us.debian.org stable Release                                   
Get:5 http://security.debian.org jessie/updates/main amd64 Packages [313 kB]  
Hit http://ftp.debian.org jessie-updates/contrib amd64 Packages               
Hit http://ftp.us.debian.org stable/main amd64 Packages                       
Get:6 http://packages.dotdeb.org jessie/all amd64 Packages [111 kB]           
Get:7 http://ftp.debian.org jessie-updates/non-free amd64 Packages/DiffIndex [736 B]
Hit http://ftp.us.debian.org stable/contrib amd64 Packages                    
Get:8 http://security.debian.org jessie/updates/contrib amd64 Packages [2,506 B]
Hit http://ftp.debian.org jessie-updates/contrib Translation-en               
Hit http://ftp.us.debian.org stable/non-free amd64 Packages                   
Get:9 http://ftp.debian.org jessie-updates/main Translation-en/DiffIndex [2,704 B]
Get:10 http://security.debian.org jessie/updates/non-free amd64 Packages [14 B]
Get:11 http://security.debian.org jessie/updates/contrib Translation-en [1,211 B]
Hit http://ftp.us.debian.org stable/contrib Translation-en                    
Get:12 http://ftp.debian.org jessie-updates/non-free Translation-en/DiffIndex [736 B]
Get:13 http://security.debian.org jessie/updates/main Translation-en [168 kB] 
Hit http://ftp.us.debian.org stable/main Translation-en                       
Get:14 http://security.debian.org jessie/updates/non-free Translation-en [14 B]
Hit http://ftp.us.debian.org stable/non-free Translation-en                   
Ign http://packages.dotdeb.org jessie/all Translation-en_IN                   
Ign http://packages.dotdeb.org jessie/all Translation-en                      
Fetched 708 kB in 10s (69.8 kB/s)                                             
Reading package lists... Done

Step #4: Install PHP 7 (“jessie” only)

You created a shell variable called $x in step #1. To install equivalent of php5 packages, enter:
$ y="$(sed 's/php5/php7.0/g' <<<$x)"
$ echo Old PHP5 packages name: $x
Old PHP5 packages name: libapache2-mod-php5 php-pear php5 php5-cgi php5-cli php5-common php5-fpm php5-gd php5-json php5-mysql php5-readline
$ echo New PHP7 packages name: $y
New PHP7 packages name: libapache2-mod-php7.0 php-pear php7.0 php7.0-cgi php7.0-cli php7.0-common php7.0-fpm php7.0-gd php7.0-json php7.0-mysql php7.0-readline
$ sudo apt-get install $y

Sample outputs:

Fig.04: Installing PHP7 on my Debain 8.7 server

How do I search PHP7 packages (“jessie” only)?

$ apt-cache search php7.0-*
$ apt-cache search php7.0-* | grep -i mysql

Sample outputs from 1st command:

libapache2-mod-php7.0 - server-side, HTML-embedded scripting language (Apache 2 module)
libphp7.0-embed - HTML-embedded scripting language (Embedded SAPI library)
php-all-dev - package depending on all supported PHP development packages
php7.0 - server-side, HTML-embedded scripting language (metapackage)
php7.0-apcu - APC User Cache for PHP
php7.0-apcu-bc - APCu Backwards Compatibility Module
php7.0-bcmath - Bcmath module for PHP
php7.0-bz2 - bzip2 module for PHP
php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary)
php7.0-cli - command-line interpreter for the PHP scripting language
php7.0-common - documentation, examples and common module for PHP
php7.0-curl - CURL module for PHP
php7.0-dba - DBA module for PHP
php7.0-dbg - Debug symbols for PHP7.0
php7.0-dev - Files for PHP7.0 module development
php7.0-enchant - Enchant module for PHP
php7.0-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
php7.0-gd - GD module for PHP
php7.0-geoip - GeoIP module for PHP
php7.0-gmp - GMP module for PHP
php7.0-igbinary - igbinary serializer for PHP
php7.0-imagick - Provides a wrapper to the ImageMagick library
php7.0-imap - IMAP module for PHP
php7.0-interbase - Interbase module for PHP
php7.0-intl - Internationalisation module for PHP
php7.0-json - JSON module for PHP
php7.0-ldap - LDAP module for PHP
php7.0-mbstring - MBSTRING module for PHP
php7.0-mcrypt - libmcrypt module for PHP
php7.0-memcached - memcached extension module for PHP, uses libmemcached
php7.0-mongodb - MongoDB driver for PHP
php7.0-msgpack - MessagePack serializer for PHP
php7.0-mysql - MySQL module for PHP
php7.0-odbc - ODBC module for PHP
php7.0-opcache - Zend OpCache module for PHP
php7.0-pgsql - PostgreSQL module for PHP
php7.0-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary)
php7.0-pspell - pspell module for PHP
php7.0-readline - readline module for PHP
php7.0-recode - recode module for PHP
php7.0-redis - PHP extension for interfacing with Redis
php7.0-snmp - SNMP module for PHP
php7.0-soap - SOAP module for PHP
php7.0-sqlite3 - SQLite3 module for PHP
php7.0-ssh2 - Bindings for the libssh2 library
php7.0-sybase - Sybase module for PHP
php7.0-tidy - tidy module for PHP
php7.0-xdebug - Xdebug Module for PHP
php7.0-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP
php7.0-xmlrpc - XMLRPC-EPI module for PHP
php7.0-xsl - XSL module for PHP (dummy)
php7.0-zip - Zip module for PHP

How do I install PHP7 packages individually?

Alternatively, you can just install needed PHP7 packages. In this example, install php7, php-fpm, php7-mysql and php7-gd:
$ sudo apt-get install php7.0 php7.0-fpm php7.0-gd php7.0-mysql

Step #5: Reconfigure web server and php 7 (“jessie” only)

Your php7 config files are located in /etc/php/7.0/ directory as follows:

  • /etc/php/7.0/apache2/ – PHP 7 with Apache 2
  • /etc/php/7.0/fpm/ – PHP 7 fpm for Nginx/Lighttpd/Apache2 and other server.
  • /etc/php/7.0/mods-available/ – All php ini config file for PHP modules such as gd/mysql/memecached and others.

You need to edit files and configure a web-server as per your setup.

Examples: Configure Lighttpd web-server to use PHP 7

Edit the /etc/lighttpd/conf-enabled/15-fastcgi-php.conf file, enter:
$ sudo vi /etc/lighttpd/conf-enabled/15-fastcgi-php.conf
Append/edit as follows (note updated /run/php/php7.0-fpm.sock path):

fastcgi.server += ( ".php" => 
	((
		"bin-path" => "/usr/bin/php-cgi",
		"socket" => "/run/php/php7.0-fpm.sock",
		"max-procs" => 1,
		"bin-environment" => ( 
			"PHP_FCGI_CHILDREN" => "4",
			"PHP_FCGI_MAX_REQUESTS" => "10000"
		),
		"bin-copy-environment" => (
			"PATH", "SHELL", "USER"
		),
		"broken-scriptfilename" => "enable"
	))
)

Save and close the file. Restart web-server as per step #6.

Examples: Configure Nginx web-server to use PHP 7

Edit the /etc/nginx/sites-enabled/default file, enter:
$ sudo vi /etc/nginx/sites-enabled/default
First add the following at the top of file:

# Upstream to abstract backend connection(s) for PHP 7.
upstream myphpsevenbackend {
  server unix:/run/php/php7.0-fpm.sock;
}

Next, locate the server block and update/edit/append as follows:

# Pass all .php files onto a php-fpm/php-fcgi server. #
    index index.php;
    location ~ [^/].php(/|$) {
        fastcgi_split_path_info ^(.+?.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
                return 404;
    }
# This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in php.ini (default) #
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass myphpsevenbackend;
}

Save and close the file. Restart web-server as per step #6.

Examples: Configure Apache 2 web-server to use PHP 7 (“jessie” only)

To enable PHP 7.0 FPM in Apache2, enter:
$ sudo a2enmod proxy_fcgi setenvif
Considering dependency proxy for proxy_fcgi:
Enabling module proxy.
Enabling module proxy_fcgi.
Module setenvif already enabled
To activate the new configuration, you need to run:
service apache2 restart

$ sudo a2enconf php7.0-fpm
Enabling conf php7.0-fpm.
To activate the new configuration, you need to run:
service apache2 reload

Restart web-server as per step #6.

Step #6: Restart web server

If you are using lighttpd web-server, enter:
$ sudo systemctl restart lighttpd.service
If you are using Nginx web-server, enter:
$ sudo systemctl restart nginx.service
If you are using Apache 2 web-server, enter:
$ sudo systemctl reload apache2.service

Test your setup

Create a file called test.php in your DocumentRoot (e.g. /var/www/html/test.php):

<?php
/* test.php */
  phpinfo();
?>

Save and close the file. Fire a web-browser and type url:
http://your-domain/test.php
OR
http://server.ip.address.here/test.php
Sample outputs:

Fig.05: phpinfo() in action displaying info about PHP 7

How do I stop/start/restart php7.0-fpm (“jessie” only)?

The syntax is as follows
$ sudo systemctl stop php7.0-fpm.service
$ sudo systemctl start php7.0-fpm.service
$ sudo systemctl restart php7.0-fpm.service
$ sudo systemctl reload php7.0-fpm.service

A note about php7.0-fpm configuration file (“jessie” only)

  • /etc/php/7.0/fpm/php.ini – PHP 7 configuration file.
  • /etc/php/7.0/fpm/php-fpm.conf – PHP 7 FPM Configuration file.
  • /etc/php/7.0/fpm/pool.d/www.conf – Default pool for PHP 7 FPM. Here you can define user/group, FastCGI request path, process manager and children values, php error file, memory limit and much more.

Whenever you made changes to any one of the above file(s), reload/restart php7.0-fpm.service using the following syntax:
$ sudo systemctl reload php7.0-fpm.service

So there you have it, a PHP 7 enabled and configured on Debian Linux 7.x or 8.x server successfully.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...