Guide on how to install PHP on Ubuntu 18.04

Avatar photo
How to Install PHP on Ubuntu 18.04 Guide – In this tutorial we will cover the steps required to install the default PHP 7.2 distro on Ubuntu 18.04 and integrate it with Nginx and Apache. I'll also show you how to install PHP 7.1 and 7.3. Most of the popular PHP frameworks and web building applications such as WordPress, […]

How to Install PHP on Ubuntu 18.04 Guide – In this tutorial we will cover the steps required to install the default PHP 7.2 distro on Ubuntu 18.04 and integrate it with Nginx and Apache. I’ll also show you how to install PHP 7.1 and 7.3.

instal PHP di ubuntu

Most of the popular PHP frameworks and web building applications such as WordPress , Laravel, Drupal and Nextcloud are compatible with PHP 7.2. PHP is a general purpose programming language that was originally designed for web development. Originally created by Rasmus Lerdorf in 1994; PHP reference implementations are now produced by The PHP Group.

Requirements

Before starting with this tutorial guide, make sure you are logged in as a user with sudo privileges. → How to Create Sudo User and Sudo Group on Ubuntu .

Install PHP 7.2 with Apache

If you are using Apache as your web server to install PHP and the Apache PHP module run the following commands:

$ sudo apt install php libapache2-mod-php

After the package is installed, restart Apache services:

$ sudo systemctl restart apache2

Install PHP 7.2 with Nginx

Unlike Apache, Nginx doesn’t have built-in support for processing PHP files so we need to install a separate application like PHP FPM (“fastCGI process manager”) which will handle PHP files.

To install PHP and PHP FPM packages, run the following command:

$ sudo apt install php-fpm

After the package has been installed, you can check the status of the PHP FPM services by:

$ systemctl status php7.2-fpm

 Output * 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 can now edit the Nginx server block and add the following lines so that Nginx can process PHP files:

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

Don’t forget to restart the Nginx service so that your new configuration is executed:

$ sudo systemctl restart nginx

Install the PHP extension

To extend the core functionality of PHP you can install a few additional extensions. PHP extensions are available as packages and can be easily installed by:

$ sudo apt install php-[extname]

For example if you wanted to install PHP MySQL and GD extensions you would run the following command:

$ sudo apt install php-mysql php-gd

After installing the new PHP extension don’t forget to restart Apache or the PHP FPM service, depending on your settings.

Testing PHP Processing

To test if your web server is properly configured for PHP processing, create a new file called info.php inside the /var/www/html directory with the following code:

<?php

phpinfo();

Save the file, open a browser of your choice and visit http://your_server_ip/info.php

The phpinfo function will print out information about your PHP configuration as shown in the image below:

phpinfo ubuntu

Install PHP 7.3 on Ubuntu 18.04

PHP 7.3 is the latest stable release of PHP. Perform the steps below to install PHP 7.3 on Ubuntu 18.04.

Start by enabling the Ondrej PHP repository:

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

Install PHP 7.3 and some of the most common 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 verify the installation, run the following command which will print the PHP version:

$ php -v

 Output 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

Install PHP 7.1 on Ubuntu 18.04

Use PHP 7.1 only if you are going to install applications that are not compatible with PHP 7.2.

Follow the steps below to install PHP 7.1:

Enable the Ondrej PHP repository by typing:

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

Install PHP 7.1 and some of the most common 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

Verify the installation, by typing:

$ php -v

 Output 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 successfully installed PHP on your Ubuntu 18.04 server. With this, you will soon be able to create a web server on your ubuntu by following a few steps which I will cover in the next article.

If you have any questions or feedback, feel free to leave a comment.

Total
0
Shares
Previous Post

How to Install a Free SSL Certificate on WordPress using Let's Encrypt

Next Post

10 List of New Smartphones Coming in 2020