YouTube Icon

Code Playground.

How to Install PHP on Ubuntu 18.04

CFG

How to Install PHP on Ubuntu 18.04

In this instructional exercise we will cover the means important to introduce the distro's default PHP 7.2 on Ubuntu 18.04 and incorporate it with Nginx and Apache. We'll additionally tell you the best way to introduce PHP 7.1 and 7.3. 

The majority of the famous PHP structures and applications including WordPress , Laravel , Drupal and Nextcloud are viable with PHP 7.2. 

Prerequisites

Prior to beginning with this instructional exercise, ensure you are signed in as a client with sudo advantages . 

Installing PHP 7.2 with Apache

In the event that you are utilizing Apache as your web worker to introduce PHP and Apache PHP module run the accompanying order: 

sudo apt install php libapache2-mod-php

 When the bundles are introduced restart the Apache administration: 

sudo systemctl restart apache2

Installing PHP 7.2 with Nginx

Not at all like Apache, Nginx doesn't have an inherent help for preparing PHP documents so we need to introduce a different application, for example, PHP FPM ("fastCGI measure director") which will deal with PHP records. 

To introduce the PHP and PHP FPM bundles run the accompanying order: 

sudo apt install php-fpm

When the bundles are introduced you can check the status of the PHP FPM administration with: 

systemctl status php7.2-fpm
* php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-06-30 23:56:14 PDT; 1min 28s ago
     Docs: man:php-fpm7.2(8)
 Main PID: 10080 (php-fpm7.2)
   Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 3 (limit: 2321)
   CGroup: /system.slice/php7.2-fpm.service
           |-10080 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)

You would now be able to alter the Nginx worker square and add the accompanying lines so Nginx can deal with PHP records: 

server {

    # . . . other code

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

Remember to restart the Nginx administration so the new arrangement produce results: 

sudo systemctl restart nginx

Installing PHP extensions

To expand the center usefulness of PHP you can introduce some extra expansions. PHP augmentations are accessible as bundles and can be effortlessly introduced with: 

sudo apt install php-[extname]

For instance in the event that you need to introduce MySQL and GD PHP augmentations you would run the accompanying order: 

sudo apt install php-mysql php-gd

In the wake of introducing another PHP expansion remember to restart the Apache or the PHP FPM administration, contingent upon your arrangement. 

Testing PHP Processing

To test whether your web worker is arranged appropriately for PHP preparing, make another record called info.php inside the/var/www/html registry with the accompanying code: 

/var/www/html/info.php

<?php

phpinfo();

Save the document, open your program of decision and visit http://your_server_ip/info.php 

The phpinfo capacity will print data about your PHP arrangement as appeared on the picture underneath: 

phpinfo Ubuntu 

Installing PHP 7.3 on Ubuntu 18.04

PHP 7.3 is the most recent stable arrival of PHP. Play out the means beneath to introduce PHP 7.3 on Ubuntu 18.04. 

Start by empowering the Ondrej PHP archive: 

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Introduce PHP 7.3 and probably the most widely recognized PHP modules: 

sudo apt install php7.3 php7.3-common php7.3-opcache php7.3-cli php7.3-gd php7.3-curl php7.3-mysql

To check the establishment, run the accompanying order which will print the PHP variant: 

php -v
PHP 7.3.1-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Jan 13 2019 10:19:33) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.1, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.1-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Installing PHP 7.1 on Ubuntu 18.04

Use PHP 7.1 just in case you will introduce applications that isn't viable with PHP 7.2. 

Follow the means beneath to introduce PHP 7.1: 

Empower the Ondrej PHP vault by composing: 

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Introduce PHP 7.1 and few most basic PHP modules: 

sudo apt install php7.1 php7.1-common php7.1-opcache php7.1-mcrypt php7.1-cli php7.1-gd php7.1-curl php7.1-mysql

Check the establishment, by composing: 

php -v
PHP 7.1.20-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Jul 25 2018 10:07:09) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
  with Zend OPcache v7.1.20-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Conclusion

You have effectively introduced PHP on your Ubuntu 18.04 worker. You can likewise find out about how to How to Install and Set up a PHP Project with Composer . 

On the off chance that you have any inquiries or criticism, don't stop for a second to leave a remark.




CFG