MeshWorld India Logo MeshWorld.
php ubuntu nginx apache linux 10 min read

Install and Configure PHP on Ubuntu 26.04

Jena
By Jena
| Updated: Apr 26, 2026
Install and Configure PHP on Ubuntu 26.04

Ubuntu 26.04 ships with PHP 8.5 in its default repositories. If you’re setting up a web server to run WordPress, Laravel, or custom PHP applications, you need to install PHP and configure it to work with your web server. This guide covers the CLI installation, PHP-FPM for Nginx, the Apache PHP module, common extensions, running multiple PHP versions, and tuning php.ini.

[!TIP] Real-World Scenario: Imagine you’re finally deploying that client’s portfolio at 11 PM, and suddenly the ‘Contact’ form starts throwing 500 errors because a random XML extension is missing. That’s why we don’t just install ‘php’ and call it a day.

TL;DR
  • sudo apt install php php-fpm nginx — install PHP and PHP-FPM
  • sudo apt install php-mysql php-curl php-mbstring php-xml php-zip php-gd — common extensions
  • Configure Nginx with fastcgi_pass unix:/run/php/php8.5-fpm.sock
  • Test with <?php phpinfo(); ?> then remove the file
  • Edit /etc/php/8.5/fpm/php.ini for web server configuration

Prerequisites

Before you start, you need:

  • Ubuntu 26.04 server with sudo access
  • Nginx installed if using PHP-FPM, or Apache if using the Apache module
  • Basic familiarity with the Linux command line

How do I install the PHP CLI?

Ubuntu 26.04 includes PHP 8.5 in its default repositories. Update your package index and install the core PHP package:

bash
sudo apt update
sudo apt install php

This installs the PHP CLI interpreter. Verify the installation:

bash
php -v

Expected output:

text
PHP 8.5.4 (cli) (built: ...) (NTS)

The php package alone only provides the command-line interpreter. To process PHP through a web server, you need either PHP-FPM (for Nginx) or the Apache PHP module.

How do I run PHP with Nginx (PHP-FPM)?

PHP-FPM (FastCGI Process Manager) is the standard way to run PHP with Nginx. Nginx doesn’t have a built-in PHP module, so it relies on PHP-FPM as a separate process.

Install and Start PHP-FPM

bash
sudo apt install php-fpm nginx

PHP-FPM starts automatically as a systemd service. Verify it’s running:

bash
sudo systemctl status php8.5-fpm

The output should show the service as active (running). PHP-FPM listens on a Unix socket by default:

bash
ls /run/php/

You should see php8.5-fpm.sock.

Configure Nginx to Process PHP

Create a server block configuration file:

bash
sudo nano /etc/nginx/sites-available/linuxconfig.conf

Add this server block:

nginx
server {
    listen 80;
    server_name linuxconfig.org www.linuxconfig.org;
    root /var/www/linuxconfig.org/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

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

    location ~ /\.ht {
        deny all;
    }
}

The key sections:

  • index directive prioritizes index.php
  • location ~ \.php$ forwards PHP requests to the PHP-FPM socket
  • location ~ /\.ht denies access to hidden files for security

Create the document root and enable the server block:

bash
sudo mkdir -p /var/www/linuxconfig.org/html
sudo chown -R $USER:$USER /var/www/linuxconfig.org/html
sudo ln -s /etc/nginx/sites-available/linuxconfig.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Information

If you don’t have a domain name, test locally by adding an entry to /etc/hosts:

bash
echo "127.0.1.1 linuxconfig.org" | sudo tee -a /etc/hosts

Remove this line after configuring proper DNS.

Test PHP Processing

Create a test PHP file:

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

Open http://your_server_ip/info.php in your browser. You should see the PHP information page.

Warning

Remove the test file after testing. The phpinfo page exposes sensitive server configuration:

bash
sudo rm /var/www/linuxconfig.org/html/info.php

How do I run PHP with Apache?

If you use Apache, PHP integration is handled through the libapache2-mod-php module. This embeds PHP directly into the Apache process.

bash
sudo apt install libapache2-mod-php

Apache automatically enables the PHP module. Verify it’s active:

bash
apache2ctl -M | grep php

Expected output:

text
php_module (shared)
Information

If you see the warning AH00558: apache2: Could not reliably determine the server's fully qualified domain name, suppress it:

bash
echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/servername.conf
sudo a2enconf servername
sudo systemctl reload apache2

Test PHP processing:

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

Open http://your_server_ip/info.php in your browser. Remove the test file afterward:

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

Don’t install both libapache2-mod-php and php-fpm on the same server unless you have a specific reason. Choose one approach based on your web server.

Which PHP extensions do I need?

A base PHP installation includes only core functionality. Most web applications require additional extensions.

Install the most commonly needed extensions:

bash
sudo apt install php-mysql php-curl php-mbstring php-xml php-zip php-gd php-intl

Extension purposes:

  • php-mysql — MySQL and MariaDB database connectivity (mysqli and PDO drivers)
  • php-curl — HTTP client library for API requests
  • php-mbstring — Multibyte string handling for UTF-8
  • php-xml — XML parsing and manipulation
  • php-zip — ZIP archive creation and extraction
  • php-gd — Image creation and manipulation
  • php-intl — Internationalization functions

Search for all available PHP extensions:

bash
apt search php- | grep "^php8.5"

List currently installed modules:

bash
php -m

Restart your PHP handler after installing extensions:

bash
sudo systemctl restart php8.5-fpm
# or for Apache:
sudo systemctl restart apache2

Can I run multiple PHP versions side-by-side?

Some projects require a specific PHP version. Use the Ondrej Sury PPA to run multiple PHP versions side by side.

Add the Ondrej PPA

bash
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Install an Alternate PHP Version

For example, to install PHP 8.1 alongside the default version:

bash
sudo apt install php8.1 php8.1-fpm php8.1-mysql php8.1-curl php8.1-mbstring php8.1-xml

Both PHP versions now coexist. Each has its own FPM service, configuration directory, and CLI binary.

Switch the Default CLI Version

Use update-alternatives to change which PHP version the php command points to:

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

To switch back:

bash
sudo update-alternatives --set php /usr/bin/php8.5
Warning

Switching the CLI version doesn’t change which PHP-FPM version your web server uses. Update the socket path in your Nginx configuration or disable/enable the appropriate Apache module.

Switch PHP-FPM Version for Nginx

Edit your Nginx server block and change the fastcgi_pass socket path:

nginx
fastcgi_pass unix:/run/php/php8.1-fpm.sock;

Reload Nginx:

bash
sudo systemctl reload nginx

Switch PHP Version for Apache

Disable the current module and enable the desired one:

bash
sudo a2dismod php8.5
sudo a2enmod php8.1
sudo systemctl restart apache2

How do I configure php.ini?

The php.ini file controls PHP’s runtime behavior. Ubuntu 26.04 maintains separate php.ini files for each SAPI (Server API) — CLI, FPM, and Apache each have their own configuration.

Locate Your php.ini Files

Find the active php.ini for the CLI:

bash
php --ini | grep "Loaded Configuration"

Expected output:

text
Loaded Configuration File:         /etc/php/8.5/cli/php.ini

For PHP-FPM:

bash
ls /etc/php/8.5/fpm/php.ini

For Apache:

bash
ls /etc/php/8.5/apache2/php.ini

Key php.ini Directives

Open the appropriate php.ini file for your web server. For PHP-FPM:

bash
sudo nano /etc/php/8.5/fpm/php.ini

Commonly adjusted directives:

DirectiveDefaultRecommendedPurpose
memory_limit128M256MMaximum memory a single script can consume
upload_max_filesize2M64MMaximum size of an uploaded file
post_max_size8M64MMaximum size of POST data (must be >= upload_max_filesize)
max_execution_time3060Maximum time in seconds a script can run
max_input_vars10003000Maximum number of input variables per request
Information

Always set post_max_size to a value equal to or greater than upload_max_filesize. If post_max_size is smaller, file uploads will silently fail.

After editing php.ini, restart the relevant service:

bash
sudo systemctl restart php8.5-fpm
# or for Apache:
sudo systemctl restart apache2

Verify changes from the command line:

bash
php -i | grep memory_limit
Warning

The CLI php -i output reflects the CLI php.ini, not the FPM or Apache one. To verify web server values, use a phpinfo() page or check the specific php.ini file directly.

Summary

  • Install PHP CLI with sudo apt install php
  • Use PHP-FPM with Nginx or the Apache module for web processing
  • Install common extensions for database, HTTP, and image handling
  • Run multiple PHP versions using the Ondrej PPA
  • Edit the correct php.ini file for your SAPI (CLI, FPM, or Apache)
  • Restart your PHP handler after configuration changes

FAQ

What PHP version does Ubuntu 26.04 include by default? Ubuntu 26.04 ships with PHP 8.5 in its official repositories. Run php -v after installation to check the exact point release.

Should I use PHP-FPM or the Apache PHP module? If you’re running Nginx, PHP-FPM is your only option. If you’re running Apache, you can use either, but PHP-FPM generally offers better performance under heavy load because it manages PHP processes independently.

How do I know which php.ini file my web server is using? Create a temporary phpinfo() page and look for the “Loaded Configuration File” row. This shows the exact path to the php.ini that your web server’s PHP handler loads. Delete the file after checking.

Can I run multiple PHP versions simultaneously on Ubuntu 26.04? Yes. After adding the Ondrej PPA, you can install additional PHP versions alongside the default one. Each version runs its own PHP-FPM service, and you can point different Nginx server blocks or Apache virtual hosts to different PHP versions.

Why are my php.ini changes not taking effect? The most common reason is editing the wrong php.ini file. PHP maintains separate configuration files for CLI, FPM, and Apache. Ensure you’re editing the file that matches your web server’s SAPI. You must also restart the PHP-FPM service or Apache after making changes.