How to Install PHP7.2 on Ubuntu 18.04

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:

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

Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3
Ultahost
$2.90 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8

How to Install AMPPS on Ubuntu 18.04

AMPPS is a solution stack for Apache, MYSQL, MongoDB, PHP, Perl and Python that
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How To Install Apache, MySQL & PHP on an Ubuntu 18.04 VPS or Dedicated Server

This article shows users how to setup PHP, Apache web server and MySQL database
4 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install OpenCart on an Ubuntu 18.04 Server or VPS with Apache, MariaDB and PHP 7

In this how-to, you’ll learn how to install OpenCart, an open source shopping ca
3 min read
Angela Olaru
Angela Olaru
Senior Writer & Hosting Expert

How To Install Apache, MariaDB, and PHP (LAMP stack) in Ubuntu 18.04

This tutorial will help install Apache, MariaDB, and PHP (LAMP stack) on Ubuntu
5 min read
Arvind Singh
Arvind Singh
Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top