Cantech Knowledge Base

Your Go-To Hosting Resource

How to Install PHP and PHP-FPM on Ubuntu 24.04?

If you’re in the process of configuring your web server and would like to install PHP and PHP-FPM on Ubuntu 24.04, you’ve come to the right place! PHP is required for a website or dynamic app

to run successfully, so whether you’re installing WordPress or just creating a new website, getting PHP installed right is the first step toward getting your stack up and running seamlessly.

This beginner’s tutorial will walk you through every step to install PHP 8.3, PHP-FPM, and your web server (Apache or Nginx) on Ubuntu 24.04.
Let’s get started!

What is PHP and PHP-FPM?

PHP (Hypertext Preprocessor) is one of the most widely used server-side scripting languages. From WordPress to Drupal to Joomla, it’s used in some of the most widely known platforms on the internet.

PHP-FPM (FastCGI Process Manager) is a PHP handler that significantly increases the performance of PHP applications; this is especially true when we’re using Nginx. PHP-FPM can manage pools of PHP workers that take care of multiple requests easily and improve the speed of your sites under load.

Prerequisites

Before installing PHP, make sure you have:

  • A fresh server running Ubuntu 24.04
  • A non-root user with sudo privileges
  • An internet connection

Step 1: Update Your System

We start with a system update every time to ensure we have the latest package information.

sudo apt update && sudo apt upgrade -y

Step 2: Install PHP 8.3 with Common Extensions

Ubuntu 24.04 includes PHP 8.3 in its default repository.

Let’s install PHP and popular extensions for databases, strings, zip files, and m

sudo apt install php php-mysql php-mbstring php-bcmath php-zip php-gd php-curl php-xml -y

Verify your installation:

php -v

You should see something like:

PHP 8.3.x (cli) ...

Step 3: Install PHP-FPM

Now let’s install PHP-FPM, which is especially useful when running Nginx (or if you prefer it over the default Apache mod_php setup):

sudo apt install php-fpm -y

Check the status:

sudo systemctl status php8.3-fpm

You should see active (running) — that means it’s working!

Step 4: Configure PHP-FPM with a Web Server

Now, depending on what you’re using — Apache or Nginx — follow the setup below.

Using Apache with PHP-FPM

1. Install Apache (if not already):

sudo apt install apache2 -y

2. Enable Required Modules:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.3-fpm

3. Restart Apache:

sudo systemctl restart apache2

Apache will now forward PHP requests to PHP-FPM!

Using Nginx with PHP-FPM

1. Install Nginx:

sudo apt install nginx -y

2. Configure Nginx to Use PHP-FPM:

Open your default server block:

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

Look for the location ~ \.php$ block and edit it like this:

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

3. Restart Nginx:

sudo systemctl restart nginx

Step 5: Test Your PHP Installation

Let’s create a quick test file to make sure everything is working:

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

Now visit: http://your_server_ip/info.php

Don’t forget to delete it afterward to avoid exposing sensitive server info:

sudo rm /var/www/html/info.php

Bonus: Want to Install PHP 8.4 or Multiple Versions?

If you need PHP 8.4 (or multiple PHP versions), you can add the Ondřej Surý PPA that is trusted by the community:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.4 php8.4-fpm

You can switch between PHP versions in Apache using:

sudo a2dismod php8.3
sudo a2enmod php8.4
sudo systemctl restart apache2

Security Tip

If you’re using PHP-FPM with public-facing servers, ensure:

  • You’ve disabled display_errors in production.
  • You’re regularly updating PHP with sudo apt upgrade.
  • You monitor for slow or malicious PHP scripts using logs.

Conclusion

And that’s it! You have learned how to install PHP and PHP-FPM in Ubuntu 24.04 and set it all up to work with Apache or Nginx. Whether you are starting a blog, creating APIs, or building Laravel/WordPress, this is a fast, dependable, and production-ready setup.

Frequently Asked Questions (FAQs)

1. How do I install PHP on Ubuntu 24.04?

Run the command sudo apt install php to install the latest PHP version from the default repository.

2. What is PHP-FPM, and why use it?

PHP-FPM (FastCGI Process Manager) optimizes PHP performance and is best suited for Nginx-based setups for efficient request handling.

3. Can I run both Apache and Nginx with PHP-FPM?

Yes, but it’s uncommon. Choose one web server and configure PHP-FPM accordingly.

4. How do I change PHP versions?

Use update-alternatives for CLI and a2dismod / a2enmod for Apache to switch between versions.

5. How do I check the PHP-FPM status?

Run: sudo systemctl status php8.3-fpm

May 26, 2025