How to Install PHP On Ubuntu

By
Sara Zivanov
Published:
February 13, 2025
Topics:

PHP (Hypertext Preprocessor) is a server-side scripting language used to generate dynamic web content, process data, and interact with databases. It plays a crucial role in web development by automating various server tasks.

This tutorial will guide you through installing PHP on Ubuntu, including its integration with Apache and Nginx web servers.

How To Install PHP On Ubuntu

Prerequisites

  • A Linux system running Ubuntu.
  • A user account with sudo or root privileges.
  • Access to the terminal.
  • A running web server (Apache or Nginx).

Installing PHP on Ubuntu

PHP has multiple release versions, with newer versions being introduced over time.

To install the default PHP version available for your Ubuntu release, use the apt package manager:

sudo apt install php
sudo apt install php terminal output

If you need a specific PHP version, use one of the methods listed below.

1. Install PHP with Apache on Ubuntu

If you are running an Apache web server, install PHP with the Apache module. Take the following steps:

1. Update the repositories to ensure you get the latest software version:

sudo apt update

2. Install software-properties-common to help you manage distributions and independent software sources:

sudo apt install software-properties-common
sudo apt install software-properties-common terminal output

3. Add the ondrej/php Personal Package Archive (PPA), which provides multiple PHP versions for Ubuntu, including versions not available in the official Ubuntu repositories. Run the following command:

sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/php terminal output

4. Update the repositories again:

sudo apt update

5. Install the PHP version you want. For example, to install PHP 8.4, run:

sudo apt install php8.4 libapache2-mod-php8.4
sudo apt install php8.4 libapache2-mod-php8.4 terminal output

6. Verify the installation with:

php -v
php -v terminal output

The output displays the PHP version you installed.

2. Install PHP with Nginx on Ubuntu

Nginx does not process PHP files natively, so PHP must be installed and configured separately using PHP-FPM (FastCGI Process Manager).

Follow these steps to install and configure PHP 8.4:

1. Update the repositories to ensure you get the latest software:

sudo apt update

2. Add the ondrej/php PPA to include various PHP versions:

sudo add-apt-repository ppa:ondrej/php
Terminal output for sudo add-apt-repository ppa:ondrej/php

3. To install PHP for Nginx, run the following command:

sudo apt install php8.4-fpm -y
sudo apt install php8.4-fpm -y terminal output

4. Restart the Nginx service to apply the changes with:

sudo systemctl restart nginx

The command has no output.

5. Enable PHP support by editing the server block in a text editor of choice. For example, to edit it in Vim, enter:

sudo vim /etc/nginx/sites-available/default

6. Find the section starting with location ~ \.php$ { and update it to match your PHP version:

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.4-fpm.sock;
  }
Editing the server block in Vim

7. Save the file and exit.

8. Restart Nginx on Ubuntu.

sudo systemctl restart nginx

The command produces no output.

9. Reload PHP with:

sudo systemctl reload php8.4-fpm

The command does not have an output.

Note: If Nginx is not installed on your system, check out our article on how to install Nginx on Ubuntu.

Installing PHP Modules on Ubuntu

To install additional PHP modules, use the following syntax:

sudo apt install php[version]-[package_name]

For example:

sudo apt install php8.4-common
sudo apt install php8.4-common terminal output

The command installs the core PHP 8.4 module and essential extensions required for PHP to function properly.

Alternatively, install multiple modules at once. For example, to install modules mysql, zip, and bcmath on PHP 8.4, run:

sudo apt install php8.4-{mysql,zip,bcmath}
sudo apt install php8.4-{mysql,zip,bcmath} terminal output

To list all loaded PHP modules, run the command:

php -m
php-m-terminal-output

The output lists all compiled PHP modules.

How to Uninstall PHP on Ubuntu

To uninstall PHP from Ubuntu, follow the instructions below:

1. Run the following command:

sudo apt purge php[version]

For example, if you installed the PHP 8.4 FPM version, run:

sudo apt purge php8.4-fpm
terminal output for sudo apt purge php8.4-fpm

2. Remove unnecessary packages with:

sudo apt autoremove
sudo apt autoremove terminal output

3. Check the PHP version to confirm the process worked:

php -v

If the output does not show the version, the process removed everything correctly.

Conclusion

This article explained how to install the default PHP version from the Ubuntu repositories or a specific version using Apache or Nginx. It also elaborated on how to install PHP modules and remove PHP from your system.

Next, learn about PHP error types and error logging.

Was this article helpful?
YesNo