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.
- 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.
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.
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 upgradeIf 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 phpFor 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 -vTo find the package candidate available from your configured repositories, run:
apt policy phpStep 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| Package | Typical purpose |
|---|---|
php-cli | Run PHP scripts from the terminal. |
php-curl | Make HTTP requests to remote services. |
php-mbstring | Work with multibyte strings and international text. |
php-xml | XML parsing and related functionality. |
php-zip | Read and create ZIP archives. |
php-gd | Common image-processing operations. |
php-intl | Internationalization and locale-aware operations. |
php-mysql | MySQL and compatible database connectivity through supported PHP drivers. |
Check the extensions currently loaded by the PHP command-line interpreter:
php -mApplications 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.
| Setup | Recommended PHP integration | Typical use |
|---|---|---|
| Apache | Apache PHP module | Straightforward single-server website. |
| Apache | PHP-FPM | Separately managed PHP worker processes. |
| Nginx | PHP-FPM | Nginx 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-phpStep A2: Enable and restart Apache
sudo systemctl enable --now apache2
sudo systemctl restart apache2
sudo systemctl status apache2 --no-pagerLook 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 configtestA 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-fpmCheck 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-pagerIf 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/defaultInside 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/Step B4: Test and reload Nginx
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl status php8.5-fpm --no-pagerOnly 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.phpAdd 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.phpReplace 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.
info.php publicly accessible on a production server.sudo rm /var/www/html/info.phpStep 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:
| Configuration | Typical 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.iniFor 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-fpmFor Apache’s PHP module, restart Apache after editing its PHP configuration:
sudo systemctl restart apache2Step 7: Verify PHP Extensions and Services
Confirm that PHP can load the extensions required by your application:
php -m
php --ri curl
php --ri mysqliThe 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-pagerOr, for Nginx with PHP-FPM:
sudo systemctl status nginx --no-pager
sudo systemctl status php8.5-fpm --no-pagerStep 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 -vPHP 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-pagerIf 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-pagerA 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
- Ubuntu Server documentation: Install and configure PHP
- Ubuntu 26.04 PHP 8.5 package details
- PHP installation and configuration manual
- PHP-FPM official documentation
- PHP security documentation
