Linux Tutorial · Ubuntu 26.04 LTS · PHP 8.5 · Beginner to Intermediate

PHP is a widely used server-side scripting language for websites, content management systems, APIs, and web applications. Ubuntu 26.04 LTS provides PHP packages through its official repositories, making it possible to install PHP without compiling it from source or adding an external repository.

In this tutorial, you will install PHP, verify the command-line interpreter, add common extensions, and configure PHP to work with either Apache or Nginx. You will also learn how to test PHP execution, check service status, and apply basic security practices.

What you will learn
  • Update Ubuntu and install PHP packages.
  • Check the installed PHP version and configuration.
  • Install common PHP extensions for web applications.
  • Configure PHP with Apache or Nginx and PHP-FPM.
  • Create a test PHP page and verify it in a browser.
  • Remove test files and troubleshoot common issues.
How to install and configure PHP on Ubuntu 26.04 LTS

Prerequisites

Before starting, make sure you have the following:

  • A computer or virtual machine running Ubuntu 26.04 LTS.
  • A user account with sudo privileges.
  • Internet access to download packages.
  • Apache or Nginx if you want to serve PHP through a web server.
  • Basic familiarity with Linux commands and terminal usage.

You can install PHP’s command-line tools without a web server. To execute PHP pages through a browser, however, you need a correctly configured web server and PHP handler.

Important: This tutorial uses Ubuntu’s official package repositories. Package versions can change through security and maintenance updates. Use php -v to check the version actually installed on your system.

Step 1: Update Ubuntu Packages

Refresh the package index

Start by refreshing APT’s package information and applying available updates. If the system has pending updates, review them before proceeding.

sudo apt update
sudo apt upgrade

If Ubuntu installs a new kernel or other system components that require a restart, reboot when it is appropriate for your environment.

Step 2: Install PHP on Ubuntu 26.04

Install the PHP interpreter and common packages

Install PHP using the default package provided by Ubuntu. The generic php package follows the PHP version selected by the distribution’s package maintainers.

sudo apt install php

For a typical Ubuntu 26.04 installation, the repository provides PHP 8.5 packages. Confirm the version on your own system instead of assuming it from the operating-system release alone.

php -v

To find the package candidate available from your configured repositories, run:

apt policy php

Step 3: Install Common PHP Extensions

PHP extensions add features such as database connectivity, image handling, XML processing, multibyte string support, and HTTP requests. Install only the extensions your application needs.

sudo apt install php-cli php-common php-curl php-mbstring php-xml php-zip php-gd php-intl php-mysql
PackageTypical purpose
php-cliRun PHP scripts from the terminal.
php-curlMake HTTP requests to remote services.
php-mbstringWork with multibyte strings and international text.
php-xmlXML parsing and related functionality.
php-zipRead and create ZIP archives.
php-gdCommon image-processing operations.
php-intlInternationalization and locale-aware operations.
php-mysqlMySQL and compatible database connectivity through supported PHP drivers.

Check the extensions currently loaded by the PHP command-line interpreter:

php -m

Applications such as WordPress, Laravel, and other frameworks may require additional extensions or specific PHP versions. Consult the application’s current requirements before deploying it.

Step 4: Choose Your Web Server Setup

Choose one of the following approaches for a basic installation. Apache can run PHP through its PHP module or through PHP-FPM. Nginx normally passes PHP requests to PHP-FPM.

SetupRecommended PHP integrationTypical use
ApacheApache PHP moduleStraightforward single-server website.
ApachePHP-FPMSeparately managed PHP worker processes.
NginxPHP-FPMNginx serving static files and proxying PHP requests to FPM.

Option A: Configure PHP with Apache

Step A1: Install Apache and the PHP module

If Apache is not installed, install it with the PHP Apache module:

sudo apt install apache2 libapache2-mod-php

Step A2: Enable and restart Apache

sudo systemctl enable --now apache2
sudo systemctl restart apache2
sudo systemctl status apache2 --no-pager

Look for an active service status. If Apache fails to start, inspect its configuration and logs before making further changes.

Step A3: Check Apache configuration

sudo apache2ctl configtest

A successful configuration test normally reports Syntax OK.

Option B: Configure PHP with Nginx and PHP-FPM

Step B1: Install Nginx and PHP-FPM

PHP-FPM runs PHP through FastCGI and allows Nginx to pass PHP requests to a dedicated service.

sudo apt install nginx php-fpm

Check the installed FPM version and available service unit:

php -v
systemctl list-unit-files 'php*-fpm.service'

On a standard Ubuntu 26.04 installation using PHP 8.5, the service is normally named php8.5-fpm. If your package version differs, use the actual service name shown by the command above.

Step B2: Enable the services

For the standard PHP 8.5 package, run:

sudo systemctl enable --now nginx
sudo systemctl enable --now php8.5-fpm
sudo systemctl status nginx --no-pager
sudo systemctl status php8.5-fpm --no-pager

If your installed FPM service uses another versioned name, replace php8.5-fpm with the correct unit.

Step B3: Create a test Nginx site

This example uses the default Nginx document root, /var/www/html. If you already host a website, back up its configuration and use the appropriate server block instead of overwriting an existing production configuration.

Open the default site configuration:

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

Inside the existing server block, ensure the root and index directives include PHP and add a PHP location block similar to the following. Preserve other required settings and avoid adding duplicate location blocks.

root /var/www/html;
index index.php index.html index.htm;

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

The socket path must match your installed FPM service. Check the FPM pool configuration if the socket does not exist:

ls -l /run/php/
Security: Do not configure Nginx to pass requests for arbitrary missing PHP files to an interpreter without considering your application’s routing and security requirements. For a production site, use a carefully reviewed server block and verify how nonexistent scripts are handled.

Step B4: Test and reload Nginx

sudo nginx -t
sudo systemctl reload nginx
sudo systemctl status php8.5-fpm --no-pager

Only reload Nginx after its configuration test succeeds. Replace the FPM service name if your installed version differs.

Step 5: Create a PHP Test Page

Create a small PHP file to verify that the web server executes PHP instead of returning the source code as plain text.

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

Add the following content and save the file:

<?php
echo "PHP is working on Ubuntu!";
?>

Open the following address in a browser running on a machine that can reach the server:

http://SERVER-IP/info.php

Replace SERVER-IP with the server’s actual IP address. If you are testing locally on the server, you can use http://127.0.0.1/info.php.

You should see the message PHP is working on Ubuntu!. If the browser displays the PHP source code, stop and correct the PHP handler configuration before serving other PHP applications.

Delete the test file after verification. Do not leave diagnostic scripts such as info.php publicly accessible on a production server.
sudo rm /var/www/html/info.php

Step 6: Check and Configure PHP Settings

PHP uses a php.ini configuration file. The CLI, Apache module, and PHP-FPM may load different configuration files, so changing one file does not necessarily change every PHP execution environment.

Check the active CLI configuration

php --ini
php -i | grep -E 'Loaded Configuration File|memory_limit|upload_max_filesize|post_max_size|date.timezone'

To locate the main configuration files installed on the system, inspect the PHP configuration directory:

ls -la /etc/php/

Common paths include:

ConfigurationTypical location for PHP 8.5
CLI/etc/php/8.5/cli/php.ini
Apache module/etc/php/8.5/apache2/php.ini
PHP-FPM/etc/php/8.5/fpm/php.ini

Edit the configuration appropriate to your web server. For Nginx with FPM, for example:

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

For a web application that accepts uploads, you may need to adjust settings such as upload_max_filesize, post_max_size, and memory_limit to values that match the application’s requirements and available server resources. Avoid increasing limits unnecessarily.

After changing the FPM configuration, restart FPM:

sudo systemctl restart php8.5-fpm

For Apache’s PHP module, restart Apache after editing its PHP configuration:

sudo systemctl restart apache2

Step 7: Verify PHP Extensions and Services

Confirm that PHP can load the extensions required by your application:

php -m
php --ri curl
php --ri mysqli

The last command will only return useful module information if the corresponding MySQLi extension is installed and enabled. For FPM-based sites, remember that the CLI and FPM environments can differ.

Check the service status for the web server you selected:

sudo systemctl status apache2 --no-pager

Or, for Nginx with PHP-FPM:

sudo systemctl status nginx --no-pager
sudo systemctl status php8.5-fpm --no-pager

Step 8: Basic PHP Security Best Practices

  • Keep PHP updated: regularly install Ubuntu security updates and maintenance releases.
  • Remove diagnostic files: delete public test scripts immediately after testing.
  • Keep secrets private: do not store database passwords, API tokens, or private keys in publicly accessible directories.
  • Use HTTPS: protect website traffic with a valid TLS certificate.
  • Install required extensions only: reduce unnecessary components and maintenance overhead.
  • Protect FPM: never expose an FPM FastCGI listener to an untrusted network. Use an appropriately restricted Unix socket or protected network configuration.
  • Review file permissions: give the web server only the access required by the application.
  • Hide production errors: avoid displaying detailed PHP errors to website visitors; log them securely instead.

For production deployments, review the official PHP security documentation and your web application’s security guidance.

Troubleshooting Common PHP Problems

PHP command not found

Check whether PHP is installed and whether APT can find the package:

sudo apt update
sudo apt install php-cli
command -v php
php -v

PHP code appears as text in the browser

The web server may not be configured to execute PHP. Confirm the Apache PHP module or Nginx PHP-FPM configuration, validate the web-server configuration, and restart or reload the relevant service. Do not leave the site publicly accessible until PHP is being handled correctly.

502 Bad Gateway with Nginx

Check that PHP-FPM is running and that the socket path in the Nginx configuration matches the socket created by FPM.

systemctl list-unit-files 'php*-fpm.service'
ls -l /run/php/
sudo nginx -t
sudo journalctl -u php8.5-fpm -n 50 --no-pager

If the FPM unit is not named php8.5-fpm, substitute the actual service name in the journal command.

Apache fails to restart

Test the Apache configuration before restarting it and inspect the service journal for errors.

sudo apache2ctl configtest
sudo journalctl -u apache2 -n 50 --no-pager

A PHP extension is missing

Install the corresponding Ubuntu PHP extension package, then verify it with php -m. If the website uses PHP-FPM or Apache’s module, restart the relevant service after installing or enabling the extension.

Conclusion

You have installed PHP on Ubuntu 26.04, added common extensions, and learned how to configure it with Apache or Nginx. You have also verified PHP execution, inspected its configuration, and reviewed essential security practices.

For a development machine, this is a useful starting point for building PHP applications. For a production server, continue by configuring your domain, HTTPS, application-specific dependencies, database access, backups, logging, and a regular security-update process.

Official References

Explore More VMoreCloud Tutorials

Khurram Shahzad — Hybrid Cloud & Virtualization Engineer

Hybrid Cloud & Virtualization Engineer specializing in VMware, Azure, AWS, Windows Server, Microsoft 365, Linux, networking, backup & disaster recovery. I share practical guides, technical projects and insights into modern IT infrastructure through VMoreCloud.

Leave a Reply

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO