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.
sudo apt install php php-fpm nginx— install PHP and PHP-FPMsudo 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.inifor 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:
sudo apt update
sudo apt install php This installs the PHP CLI interpreter. Verify the installation:
php -v Expected output:
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
sudo apt install php-fpm nginx PHP-FPM starts automatically as a systemd service. Verify it’s running:
sudo systemctl status php8.5-fpm The output should show the service as active (running). PHP-FPM listens on a Unix socket by default:
ls /run/php/ You should see php8.5-fpm.sock.
Configure Nginx to Process PHP
Create a server block configuration file:
sudo nano /etc/nginx/sites-available/linuxconfig.conf Add this server block:
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:
indexdirective prioritizesindex.phplocation ~ \.php$forwards PHP requests to the PHP-FPM socketlocation ~ /\.htdenies access to hidden files for security
Create the document root 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/
sudo nginx -t
sudo systemctl reload nginx If you don’t have a domain name, test locally by adding an entry to /etc/hosts:
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:
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.
Remove the test file after testing. The phpinfo page exposes sensitive server configuration:
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.
sudo apt install libapache2-mod-php Apache automatically enables the PHP module. Verify it’s active:
apache2ctl -M | grep php Expected output:
php_module (shared) If you see the warning AH00558: apache2: Could not reliably determine the server's fully qualified domain name, suppress it:
echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/servername.conf
sudo a2enconf servername
sudo systemctl reload apache2 Test PHP processing:
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:
sudo rm /var/www/html/info.php 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:
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 requestsphp-mbstring— Multibyte string handling for UTF-8php-xml— XML parsing and manipulationphp-zip— ZIP archive creation and extractionphp-gd— Image creation and manipulationphp-intl— Internationalization functions
Search for all available PHP extensions:
apt search php- | grep "^php8.5" List currently installed modules:
php -m Restart your PHP handler after installing extensions:
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
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:
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:
sudo update-alternatives --set php /usr/bin/php8.1
php -v To switch back:
sudo update-alternatives --set php /usr/bin/php8.5 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:
fastcgi_pass unix:/run/php/php8.1-fpm.sock; Reload Nginx:
sudo systemctl reload nginx Switch PHP Version for Apache
Disable the current module and enable the desired one:
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:
php --ini | grep "Loaded Configuration" Expected output:
Loaded Configuration File: /etc/php/8.5/cli/php.ini For PHP-FPM:
ls /etc/php/8.5/fpm/php.ini For Apache:
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:
sudo nano /etc/php/8.5/fpm/php.ini Commonly adjusted directives:
| Directive | Default | Recommended | Purpose |
|---|---|---|---|
memory_limit | 128M | 256M | Maximum memory a single script can consume |
upload_max_filesize | 2M | 64M | Maximum size of an uploaded file |
post_max_size | 8M | 64M | Maximum size of POST data (must be >= upload_max_filesize) |
max_execution_time | 30 | 60 | Maximum time in seconds a script can run |
max_input_vars | 1000 | 3000 | Maximum number of input variables per request |
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:
sudo systemctl restart php8.5-fpm
# or for Apache:
sudo systemctl restart apache2 Verify changes from the command line:
php -i | grep memory_limit 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.
What to Read Next
- Install and Configure PostgreSQL on Ubuntu 26.04 — set up the database layer for your PHP applications
- Set Up Samba File Sharing on Ubuntu 26.04 — another Ubuntu 26.04 server setup guide
- Check Your PHP Version from the Command Line — verify which PHP version is active across CLI, FPM, and Apache
Related Articles
Deepen your understanding with these curated continuations.
Control Auto-Suspend on Ubuntu 26.04
Complete guide to manage auto-suspend on Ubuntu 26.04. Configure power settings via GUI, gsettings, or systemd to optimize battery life and prevent unwanted sleep.
8 Netlify Alternatives Worth Trying in 2026
The best Netlify alternatives in 2026: Vercel for Next.js, Railway for variable workloads, Coolify for self-hosted cost control, and more. Find the right deployment platform for your stack.
How to Use iCloud Mail SMTP Server to Send Emails
Configure iCloud Mail SMTP settings with app-specific passwords. Includes code examples for PHP, Python, and Node.js to send emails through iCloud in 2026.