Install PHP in Ubuntu 16

This article is the third in a mini-series of quick reference guides for installing Apache, PHP, and MySQL in Ubuntu 16. In this article we will cover the installation of PHP 7 and the Apache PHP module. Lastly we’ll verify PHP works with Apache.

My environment for these steps is Ubuntu 16 running on VirtualBox in Windows 10 x64.

Previously…

Check out the previous article where we installed Apache HTTP Server.

Install PHP

Let’s install PHP now, along with the Apache PHP module

  1. Open a new terminal and type

    sudo apt-get install php libapache2-mod-php
    
  2. When it has finished you can verify the PHP version that was installed by using the -v flag:

    php -v
    

    You should see something like this:

    PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.22-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies

  3. We can also check the installed PHP modules using the -m flag:

    php -m
    

    This will return a list of all the installed modules. Some additional modules we’ll want to install are mysql and curl. We’ll do that next.

  4. Additional PHP modules can be installed at any time. Type the below command to install the mysql and curl modules:

    sudo apt-get -y install php-mysql php-curl
    

    Make sure to restart Apache after a new module is added:

    sudo /etc/init.d/apache2 restart
    

    You can also check the status of Apache

    sudo /etc/init.d/apache2 status
    

Verify PHP and Apache configuration

The standard way to verify a PHP / Apache installation is to use PHP’s inbuilt phpinfo() function which outputs information about PHP’s configuration. Refer to the PHP Manual for more info.

  1. Change into the Apache document root /var/www/html/

    cd /var/www/html/
    
  2. Create a new file named info.php:

    sudo touch info.php
    
  3. Open info.php in a text editor and paste the below PHP script. This will output details of the PHP installation and configuration:

    <?php
        phpinfo();
    ?>
    
  4. Open your web browser and navigate to http://localhost/info.php (or http://127.0.0.1/info.php)

    This page should output PHP configuration details, including any installed modules.

    MySQL install prompt

    Two important settings to note are the Configuration File (php.ini) Path and the Loaded Configuration File. This is where the php.ini file (PHP’s configuration file) is located:

    /etc/php/7.0/apache2/php.ini