How to Install PHP on Ubuntu 24.04

By 

Updated on

6 min read

Install PHP on Ubuntu 24.04

PHP is one of the most used server-side programming languages. Many popular CMS and frameworks such as WordPress, Magento, and Laravel are written in PHP.

This guide covers the steps necessary to install PHP on Ubuntu 24.04 and integrate it with Nginx and Apache. The default Ubuntu 24.04 repositories include PHP 8.3. We will also show you how to install other PHP versions.

Quick Reference

TaskCommand
Install PHP with Apachesudo apt install php libapache2-mod-php
Install PHP with Nginxsudo apt install php-fpm
Install an extensionsudo apt install php-[extname]
Check PHP versionphp -v
List installed modulesphp -m
Restart PHP-FPMsudo systemctl restart php8.3-fpm
Switch PHP version (CLI)sudo update-alternatives --config php
Add Ondřej PPAsudo add-apt-repository ppa:ondrej/php

Prerequisites

To follow this guide, you need to be logged in as a user with sudo privileges .

Installing PHP 8.3 with Apache

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

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

Once the packages are installed, restart Apache for the PHP module to get loaded:

Terminal
sudo systemctl restart apache2

Installing PHP 8.3 with Nginx

Unlike Apache, Nginx does not have built-in support for processing PHP files. We will use PHP-FPM (FastCGI Process Manager) to handle PHP files.

Run the following commands to install PHP and the PHP-FPM package:

Terminal
sudo apt update
sudo apt install php-fpm

Once the installation is completed, the FPM service starts automatically. To check the status of the service, run:

Terminal
sudo systemctl status php8.3-fpm
output
● php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php8.3-fpm.service; enabled; preset: enabled)
     Active: active (running) since Thu 2026-01-30 15:10:05 UTC; 12s ago

You can now edit the Nginx server block and add the following lines so that Nginx can process PHP files:

nginx
server {

    # . . . other code

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

Restart the Nginx service so that the new configuration takes effect:

Terminal
sudo systemctl restart nginx

Installing PHP Extensions

PHP extensions are compiled libraries that extend the core functionality of PHP. Extensions are available as packages and can be easily installed with apt :

Terminal
sudo apt install php-[extname]

For example, to install the MySQL and GD extensions, run:

Terminal
sudo apt install php-mysql php-gd

Here are some of the most commonly used PHP extensions:

ExtensionPackageDescription
MySQLphp-mysqlMySQL and MariaDB database support
cURLphp-curlURL transfer library
GDphp-gdImage processing
Mbstringphp-mbstringMultibyte string support
XMLphp-xmlXML parsing and manipulation
ZIPphp-zipZIP archive support
Intlphp-intlInternationalization functions
OPcachephp-opcacheBytecode caching for performance
BCMathphp-bcmathArbitrary precision math

To install all commonly needed extensions at once:

Terminal
sudo apt install php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl php-opcache php-bcmath

After installing a new PHP extension, restart Apache or PHP-FPM depending on your setup:

Terminal
sudo systemctl restart apache2

Or for Nginx with PHP-FPM:

Terminal
sudo systemctl restart php8.3-fpm

To list all installed PHP modules:

Terminal
php -m

Testing PHP Processing

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

Terminal
echo '<?php phpinfo();' | sudo tee /var/www/html/info.php

Open your browser and visit http://your_server_ip/info.php. You will see information about your PHP configuration as shown on the image below:

phpinfo Ubuntu 24.04
Warning
The phpinfo() page exposes sensitive information about your server and PHP configuration. Remove it after testing: sudo rm /var/www/html/info.php

Installing Other PHP Versions

Ondřej Surý, a Debian developer, maintains a repository that includes multiple PHP versions. This is a third-party source, so use it only if you need a different PHP version than what Ubuntu provides. To enable the repository , run:

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

You can now install any PHP version you need by appending the version number to the package name:

Terminal
sudo apt install php[version]

For example, to install PHP 8.1 and a few common modules:

Terminal
sudo apt install php8.1 php8.1-cli php8.1-common php8.1-opcache php8.1-gd php8.1-curl php8.1-mysql php8.1-mbstring php8.1-xml

Switching Between PHP Versions

If you have multiple PHP versions installed, you can switch the default CLI version using update-alternatives:

Terminal
sudo update-alternatives --set php /usr/bin/php8.1

To interactively choose the default version:

Terminal
sudo update-alternatives --config php

For Apache, disable the current PHP module and enable the one you want:

Terminal
sudo a2dismod php8.3
sudo a2enmod php8.1
sudo systemctl restart apache2

For Nginx, update the PHP-FPM socket path in your server block to point to the correct version, then restart Nginx:

Terminal
sudo systemctl restart nginx

Checking the PHP Version

To check which PHP version is installed on your system, run:

Terminal
php -v
output
PHP 8.3.6 (cli) (built: Jan  5 2026 14:22:47) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies

PHP Configuration Files

PHP configuration files are located at:

  • Main config: /etc/php/8.3/cli/php.ini (CLI) and /etc/php/8.3/apache2/php.ini (Apache) or /etc/php/8.3/fpm/php.ini (FPM)
  • FPM pool config: /etc/php/8.3/fpm/pool.d/www.conf
  • Extension configs: /etc/php/8.3/mods-available/

After changing any configuration, restart the relevant service:

Terminal
sudo systemctl restart php8.3-fpm

Uninstalling PHP

To remove PHP and related packages, you can purge them and clean up dependencies:

Terminal
sudo apt purge 'php*'
sudo apt autoremove

FAQ

What PHP version does Ubuntu 24.04 include?
Ubuntu 24.04 ships with PHP 8.3 in its default repositories.

What is the difference between php and php-fpm?
The php package includes the CLI interpreter and the Apache module. The php-fpm package provides a FastCGI Process Manager used by Nginx and other web servers that do not have built-in PHP support.

Can I run multiple PHP versions at the same time?
Yes. You can install multiple PHP versions from the Ondřej Surý PPA and configure each virtual host or server block to use a different PHP version.

Do I need to restart Apache or Nginx after installing an extension?
Yes. After installing a PHP extension, restart Apache (sudo systemctl restart apache2) or PHP-FPM (sudo systemctl restart php8.3-fpm) for the extension to be loaded.

Where is the php.ini file located?
PHP uses separate php.ini files for each SAPI. The CLI config is at /etc/php/8.3/cli/php.ini, the Apache config at /etc/php/8.3/apache2/php.ini, and the FPM config at /etc/php/8.3/fpm/php.ini.

Conclusion

We have shown you how to install PHP 8.3 on Ubuntu 24.04 with both Apache and Nginx. You can now install extensions, configure PHP, and start building your applications.

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

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page