How to Install PHP on Ubuntu Linux

This guide outlines how to install and use the PHP server-side programming language on Ubuntu Linux with Apache or Nginx web servers. Instructions are provided for the installation process, including PHP integration with Apache and Nginx, PHP modules, and updates. PHP is used to support content management systems like WordPress, Magento, Drupal etc. PHP versions…

This post shows students and new users steps to install and use PHP server-side programming languages on Ubuntu Linux with Apache or Nginx to support content management systems (CMS) written in PHP. Popular CMS frameworks like WordPress, Magento, Drupal, and others are written in PHP.

PHP supports and works with many types of web servers. You can use PHP with Apache, Nginx, and other open-source web servers. However, most PHP implementations are integrated with Apache or Nginx web servers. Because of that, this post will only show you how to use PHP with Apache or Nginx on Ubuntu Linux.

PHP is constantly updated, so don’t be surprised that the versions installed here aren’t the latest. As of this writing, the latest PHP version is PHP 8.0.

If you’re a student or new user learning Linux, Ubuntu Linux is the easiest place to start learning. Ubuntu is the modern, open-source Linux operating system for desktops, servers, and other devices.

To start installing PHP on Ubuntu Linux, follow the steps below.

How to install PHP on Ubuntu with Apache support

As mentioned above, PHP supports many web servers, including Apache, Nginx, and others. If you’re using an Apache web server, the commands below are used to install PHP.

sudo apt update
sudo apt install php libapache2-mod-php

After installing PHP, restart the Apache web server for PHP modules to apply. PHP is tightly integrated with Apache. If you change PHP and want it to apply, simply restart or reload Apache.

To do that, run the comments below.

sudo systemctl restart apache2

How to install PHP on Ubuntu Linux with Nginx support

If you’re running an Nginx web server, the commands below will install PHP with support for Nginx.

Nginx doesn’t have built-in support for processing PHP files and is not tightly integrated as Apache. To add PHP support for Nginx, you must install and use PHP-FPM (FastCGI process manager) to handle the PHP files.

To do that, run the commands below.

sudo apt update
sudo apt install php-fpm

Because PHP isn’t tightly integrated with Nginx, if you make changes to PHP, you must restart or reload PHP and Nginx separately for the changes to apply.

sudo systemctl restart php-fpm
sudo systemctl restart nginx

Also, you must add these lines to the Nginx server block to allow Nginx to read PHP files. Remember to use the version of PHP installed and reference it in the highlighted line below.

server {

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

How to install PHP modules on Ubuntu Linux

You have installed PHP, but many other modules are available to support and enhance PHP performance and functionality.

Below are some common PHP extensions and modules one can install.

php-common php-cli php-gd php-curl php-mysql

How to install the latest version of PHP on Ubuntu Linux

As mentioned above, the current version of PHP is 8.0. However, you will not see that version in Ubuntu repositories. The current latest in Ubuntu repositories is 7.4.

To install the latest of other versions of PHP that are not available in the Ubuntu repository, run the commands below to install a third-party PPA repository that includes multiple versions of PHP.

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

After adding the repository above, you can then install another PHP version.

sudo apt install php8.0 php8.0-common php8.0-cli php8.0-gd php8.0-curl php8.0-mysql

That should do it!

Conclusion:

This post showed you how to install PHP on Ubuntu Linux with Apache or Nginx web server support. Please use the comment form below if you find any errors above or have something to add.

Comments

35 responses to “How to Install PHP on Ubuntu Linux”

  1. Thanks,
    Very simple and very clear!
    Best wishes.

Leave a Reply

Your email address will not be published. Required fields are marked *