Friday, April 17, 2026
Linux

How to Install and Configure PHP on Ubuntu 26.04

How to Install and Configure PHP on Ubuntu 26.04
48views

PHP remains one of the most widely deployed server-side scripting languages on the web, powering everything from simple scripts to large-scale applications like WordPress and Laravel. If you plan to run dynamic websites or web applications on your Ubuntu 26.04 server, learning how to install and configure PHP is an essential step. In this guide, you will walk through the complete process to php install configure ubuntu 26.04, including PHP-FPM for Nginx, the Apache module, common extensions, multiple PHP versions, and php.ini tuning.

Software Requirements

CategoryRequirements, Conventions or Software Version Used
SystemUbuntu 26.04 Resolute Raccoon
SoftwarePHP 8.5, PHP-FPM, Nginx or Apache
OtherPrivileged access to your Linux system as root or via the sudo command.
Conventions# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install PHP and PHP-FPM on Ubuntu 26.04 with a few commands, then configure your web server to process PHP files.

StepCommand/Action
1. Install PHP and PHP-FPMsudo apt install php php-fpm nginx
2. Install common extensionssudo apt install php-mysql php-curl php-mbstring php-xml php-zip php-gd
3. Configure Nginx for PHPAdd fastcgi_pass block to your Nginx server block
4. Test PHP processingCreate a phpinfo() test page and open it in a browser

Installing PHP on Ubuntu 26.04

Ubuntu 26.04 ships with PHP 8.5 in its default repositories, so you can get started without adding any third-party sources. Begin by updating your package index and installing the core PHP package:

This installs the PHP CLI interpreter along with common default modules. Once the installation completes, verify the installed version:

You should see output similar to:

It is important to understand that 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. The following sections cover both approaches.

Installing PHP-FPM for Nginx on Ubuntu 26.04

PHP-FPM (FastCGI Process Manager) is the recommended way to run PHP with Nginx. Unlike Apache, Nginx does not have a built-in PHP module, so it relies on PHP-FPM as a separate process to handle PHP requests. Consequently, you need to install the PHP-FPM package and then configure Nginx to forward PHP requests to it.

Install and Start PHP-FPM

Install the PHP-FPM package:

After installation, PHP-FPM starts automatically as a systemd service. Verify that it is running:

The output should show the service as active (running). Additionally, PHP-FPM listens on a Unix socket by default, which Nginx will use to communicate with it. You can confirm the socket file exists:

You should see a socket file named php8.5-fpm.sock.

Configure Nginx to Process PHP Files

To serve PHP through Nginx, you need a server block that includes a location directive for PHP-FPM. Create a new server block configuration file:

Add the following complete server block. This is a minimal but fully functional configuration that serves both static files and PHP scripts:

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 of this configuration are the index directive, which prioritizes index.php so that Nginx serves PHP index files by default, the location ~ \.php$ block, which matches any request ending in .php and forwards it to the PHP-FPM socket, and the location ~ /\.ht block, which denies access to hidden files such as .htaccess for security.

Now create the document root directory and enable the server block:

$ 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/

Test the Nginx configuration for syntax errors and reload

Test PHP Processing with Nginx

INSTALLATION TIPS
If you do not have a domain name pointing to your server, you can test the server block locally by adding an entry to your /etc/hosts file:

This maps the domain to your local machine, allowing you to access http://linuxconfig.org in your browser for testing purposes. Remove or comment out this line once you configure proper DNS.
Create a test PHP file in your web root to verify that Nginx correctly processes PHP:

Open your browser and navigate to http://your_server_ip/info.php. You should see the PHP information page displaying the full PHP configuration. This confirms that Nginx is correctly forwarding PHP requests to PHP-FPM.

SECURITY ALERT
Remove the info.php file after testing. The phpinfo page exposes sensitive server configuration details that should never be publicly accessible on a production server:

Installing PHP for Apache on Ubuntu 26.04

If you use Apache instead of Nginx, PHP integration is handled through the libapache2-mod-php module. This approach embeds PHP directly into the Apache process, so no separate service is required.

Install the Apache PHP module:

Apache automatically enables the PHP module during installation. You can confirm it is active:

The output should include a line like php_module (shared), confirming that Apache is ready to process PHP files.

INSTALLATION TIPS
If you see the warning AH00558: apache2: Could not reliably determine the server's fully qualified domain name, suppress it by setting a global ServerName directive:

To test PHP processing, create a test file in your Apache document root:

Open http://your_server_ip/info.php in your browser. If you see the PHP information page, Apache is processing PHP correctly. Remove the test file afterward:

INSTALLATION TIPS
Do not install both libapache2-mod-php and php-fpm on the same server unless you have a specific reason. Running both can lead to confusion about which PHP handler is active. Choose one approach based on your web server.

Installing Common PHP Extensions on Ubuntu 26.04

A base PHP installation includes only core functionality. Most web applications require additional extensions for database connectivity, string handling, image processing, and more. Therefore, installing the right extensions is a critical part of setting up PHP on Ubuntu 26.04.

Install the most commonly needed extensions in one command:

Here is what each extension provides:

ExtensionPurpose
php-mysqlMySQL and MariaDB database connectivity (mysqli and PDO drivers)
php-curlHTTP client library for API requests and remote file fetching
php-mbstringMultibyte string handling for UTF-8 and internationalization
php-xmlXML parsing and manipulation (DOM, SimpleXML, XMLReader)
php-zipZIP archive creation and extraction
php-gdImage creation and manipulation (JPEG, PNG, GIF, WebP)
php-intlInternationalization functions (number formatting, date formatting, collation)

To search for all available PHP extensions in the repositories, use:

You can also list all currently installed PHP modules:

After installing new extensions, remember to restart your PHP handler so the changes take effect:

Or for Apache:

Installing Multiple PHP Versions on Ubuntu 26.04

Some projects require a specific PHP version that differs from the default. For instance, a legacy application might need PHP 8.1, while the Ubuntu 26.04 repositories provide PHP 8.5. To run multiple PHP versions side by side, you can use the Ondrej Sury PPA, which is the most widely trusted third-party PHP repository for Ubuntu.

Add the Ondrej PPA

Install an Alternate PHP Version

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

Both PHP versions now coexist on the system. Each version 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:

Verify the switch:

To switch back to the default version:

IMPORTANT
Switching the CLI version does not change which PHP-FPM version your web server uses. To switch the web server’s PHP version, 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:

Then reload Nginx:

Switch PHP Version for Apache

Disable the current module and enable the desired one:

Configuring php.ini on Ubuntu 26.04

The php.ini file controls PHP’s runtime behavior. On Ubuntu 26.04, PHP maintains separate php.ini files for each SAPI (Server API), meaning the CLI, FPM, and Apache each have their own configuration. Consequently, changes you make to one php.ini do not affect the others.

Locate Your php.ini Files

Find the active php.ini for the CLI:

For PHP-FPM, check the phpinfo output or look directly:

For Apache:

Key php.ini Directives

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

The following directives are the most commonly adjusted for web applications:

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

INSTALLATION TIPS
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 even when the file is within the upload limit.

After editing php.ini, restart the relevant service for changes to take effect:

Or for Apache:

You can confirm your changes are active by checking specific values from the command line:

IMPORTANT
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.

Conclusion

You have successfully installed and configured PHP on Ubuntu 26.04. This guide covered the CLI installation, PHP-FPM setup for Nginx, the Apache PHP module, common extensions, running multiple PHP versions with the Ondrej PPA, and tuning the php.ini configuration. With PHP properly configured, your Ubuntu 26.04 server is ready to host dynamic web applications. For more in-depth information on PHP configuration directives, consult the official PHP documentation.

Leave a Response