How to Install PHP7.2 on Ubuntu 18.04

Introduction

The latest version of Ubuntu (Ubuntu 18.04) comes with the most recent version of PHP (7.2). The Hypertext Preprocessor commonly known as PHP, is a powerful, server-side scripting language for web application development which also utilized as a robust general-purpose programming language.

This tutorial will help you install PHP on your Ubuntu 18.04 server.

Ready? Let’s go!

Prerequisites

To properly install PHP on Ubuntu 18.04, you must be logged into your Ubuntu 18.04 server as a user with sudo permissions.

Step 1 Installing PHP 7.2

In this tutorial, we’ll install PHP with Apache and Nginx web server.

Installing PHP With Apache Web Server

If Apache is installed and configured as your web server, then you can follow the steps below to install PHP 7.2.

Run the command below to install PHP and the required Apache PHP modules:

$ sudo apt install php libapache2-mod-php

The packages should be installed automatically. Once, the process is complete, execute the following command to restart your Apache service.

$ sudo systemctl restart apache2

Installing PHP With Nginx Web Server

Nginx web server does not have built-in capabilities to process PHP files. For this reason, we ought to install and configure a disparate application like the PHP FPM to handle the processing of PHP files.

Run the command below to install the latest version of PHP and the required PHP FPM packages:

$ sudo apt install php-fpm

Once the modules are installed, you can run the command below to check the status of your PHP FPM service:

$ systemctl status php7.2-fpm

This will give you an output like the one below:

php7.2-fpm.service-ThePHP7.2FastCGIProcessManager
   Loaded: loaded(/lib/systemd/system/php7.2-fpm.service;enabled;vendor preset: enabled)
   Active: active(running)sinceSat2018-08-2503:08:51UTC;1weeks3daysago
     Docs: man:php-fpm7.2(8)
 Main PID: 8377(php-fpm7.2)
   Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 3(limit:1152)
   CGroup: /system.slice/php7.2-fpm.service
           |-8377 php-fpm: masterprocess(/etc/php/7.2/fpm/php-fpm.conf)
           ├─8391 php-fpm:pool www
           └─8392 php-fpm: poolwww

Next, edit your Nginx server block and include the lines below to enable Nginx web server to process PHP files;

server {

    # . . . other code

    location~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }
}

Once this is done, restart your Nginx web server for the configuration changes to take effect.

$ sudo systemctl restart nginx

Step 2 Installing PHP Extensions

By now you have successfully, installed PHP, but it's good to install additional extensions to enhance the core functionality of your installation.

First, execute the command below to see all the available PHP modules;

$ sudo apt-cachesearch php7.2

This will give you an output with the available extensions/module for PHP7.2 and a short description for each. To install one PHP7.2 extension run the command:

$ sudo apt install php-[extname]

Where [extname] is the extension name. For instance, if you want to install IMAP module for PHP7.2, execute the command as follows:

$ sudo apt install php-imap

On the other hand, if you want to install multiple PHP modules all at once, you can run the command:

$ sudo apt install php-[extname1] php-[extname2] php-[extname3] ....

For example to install, mbstring, phpdbg, sybase, and soap, execute the command as follows:

$ sudo apt install php7.2-mbstring php7.2-phpdbg php7.2-sybase php7.2-soap

Once you install all the extensions you need, run the command below to restart Nginx or Apache service, based on your setup.

For Nginx execute the command:

$ sudo systemctl restart nginx

On the other hand, if you are using Apache run the command below:

$ sudo systemctl restart apache2

Step 3 Testing PHP Processing

By now, you have installed PHP7.2 in you Ubuntu 18.04 server. The next step is scrutinizing your web server to see if it’s properly configured to process PHP files. We’ll create a basic PHP script; info.php and save it in the web root directory.

First, execute the commands below to create a blank file in the location  /var/www/html/.

$ sudo nano /var/www/html/info.php

Add the text below to the blank file:

<?php
phpinfo();
?>

Save and exit the file.

Next, open the link below using any web browser:

http://your_server_ip/info.php

This will prompt the phpinfo feature to print the details of your PHP configuration. The following page will appear, indicating the installation was successful, and your web server is properly configured:

How to Install PHP7.2 on Ubuntu 18.04

Conclusion

Congratulations! You have successfully installed and configured PHP7.2 on your Ubuntu 18.04 server.

Check out these top 3 Best web hosting services

Was this article helpful?