Installing PHP 7 Ubuntu

We can make PHP 7 work in Apache2 through PHP-FPM and Apache’s mod_fastcgi module which we install as follows:

apt-get -y install libapache2-mod-fastcgi php7.0-fpm php7.0

PHP-FPM is a daemon process (with the systemd unit php7.0-fpm.service) that runs a FastCGI server on the socket /var/run/php/php7.0-fpm.sock.

Next, enable the following Apache modules…

a2enmod actions fastcgi alias

… and restart Apache:

systemctl restart apache2.service

 

Configuring Apache

To make Apache work with PHP-FPM, we need the following configuration:

        <IfModule mod_fastcgi.c>
                AddHandler php7-fcgi .php
                Action php7-fcgi /php7-fcgi
                Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
                FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization
        </IfModule>

You can put it in the global Apache configuration (so it’s enabled for all vhosts), for example in /etc/apache2/conf.d/php7.0-fpm.conf (this file does not exist, so you must create it), or you can place it in each vhost that should use PHP-FPM. I want to use PHP-FPM with the default vhost so I open its vhost configuration file /etc/apache2/sites-available/000-default.conf.

nano /etc/apache2/sites-available/000-default.conf

… and put the following section somewhere between <VirtualHost></VirtualHost>:

        <Directory /usr/lib/cgi-bin>
                Require all granted
        </Directory>
        <IfModule mod_fastcgi.c>
                SetHandler php7-fcgi .php
                Action php7-fcgi /php7-fcgi virtual
                Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
                FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization
        </IfModule>

Restart Apache afterwards:

systemctl restart apache2.service

Now create the following PHP file in the document root /var/www:

nano /var/www/html/info.php

<?php
phpinfo();

Now we call that file in a browser (e.g. http://192.168.1.100/info.php):

PHP 7 is working fine on Ubuntu 16.04

As you see, PHP 7 is working, and it’s working through FPM/FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP. MySQL is not listed there which means we don’t have MySQL support in PHP yet.

Please note: If you have the PHP 7 apache module installed (mod_php), this can happen e.g. when you start with a LAMP server setup of your ISP and not with an empty server like I did it in this tutorial, then you might have to disable mod_php as shown below.

a2dismod php7.0

and restart apache:

systemctl restart apache2.service

source : https://www.howtoforge.com/tutorial/apache-with-php-fpm-on-ubuntu-16-04/

Posted on: October 10, 2017, by :

Leave a Reply