Read in 3 minutes

last updated 

How to Install PHP on Ubuntu 18.04

Ubuntu 18.04 LTS ships with the latest and greatest PHP version 7.2. Most of the popular PHP frameworks and applications including WordPress, Laravel, Drupal and Symfony are compatible with PHP 7.2.

In this tutorial we will explain how to install PHP 7.2 on Ubuntu 18.04.


Before starting with this tutorial, make sure you are logged in as a user with sudo privileges.

Installing PHP 7.2 with Apache

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

sudo apt install php libapache2-mod-php

Once the packages are installed restart the Apache service :

sudo systemctl restart apache2

Installing PHP 7.2 with Nginx

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

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

sudo apt install php-fpm

Once the packages are installed you can check the status of the PHP FPM service 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 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;

Do not forget to restart the Nginx service so that the new configuration take effect:

sudo systemctl restart nginx

Installing PHP extensions

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

sudo apt install php-[extname]

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

sudo apt install php-mysql php-gd

After installing a new PHP extension do not forget to restart the Apache or the PHP FPM service, depending on your setup.

Testing PHP Processing

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



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

The phpinfo function will print information about your PHP configuration as shown on the image bellow:


You have successfully installed PHP on your Ubuntu 18.04 server.

If you have any question or feedback do not hesitate to leave a comment.