Install PHP in Ubuntu 18

This article is the third in a mini-series of quick reference guides for installing Apache, PHP, and MySQL in Ubuntu 18. In this article we will cover the installation of PHP 7.2 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.

Previous articles

Check out the previous articles where we installed Apache HTTP Server and MySQL.

Update Ubuntu package list

  1. First, make sure your Ubuntu packages list is up to date.

    sudo apt update
    

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
    

    You will be notified that additional disk space will be used and prompted to continue. Hit y then Enter to continue.

    PHP and the Apache 2 PHP module will be downloaded and installed. This may take a few minutes depending on your internet connection.

  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.2.15-0ubuntu0.18.04.1 (cli) (built: Feb 8 2019 14:54:22) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.15-0ubuntu0.18.04.1, Copyright (c) 1999-2018, 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
    
  5. Restart Apache after the new modules are installed:

    sudo systemctl restart apache2
    

    You can also check the status of Apache

    sudo systemctl status apache2
    

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

    sudo gedit info.php &
    

    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. This confirms that PHP is correctly installed and the Apache PHP module is working.

    PHP info

    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.2/apache2/php.ini

  5. If you also installed the PHP modules curl and mysql you can confirm that on this same page.

    Hit Ctrl + f and search for curl and confirm it shows as enabled:

    PHP info curl module