
If you want to Install PHP on Ubuntu 26.04, the main goal is simple: get a clean, secure, and working PHP setup without breaking your web stack. This guide shows you how to install the latest PHP, configure it for real-world use, and avoid the common mistakes that slow down new servers.
I wrote this from the point of view of a sysadmin who has done this kind of work many times in production. You will get a practical Install PHP on Ubuntu 26.04 setup that works for beginners, developers, and admins who need a reliable Linux server tutorial they can follow step by step.
Ubuntu 26.04 is expected to ship with a modern PHP release, and if you need a newer build or multiple versions, the Ondřej Surý repository is the standard path many Linux admins use. The exact version and package names can vary by repository, so this article focuses on the safest install flow and explains why each step matters.
Prerequisites
- Ubuntu 26.04 LTS installed and updated.
- A user with sudo access.
- Internet access for package downloads.
- A terminal or SSH session.
- A web server such as Nginx or Apache if you plan to serve PHP pages.
- A backup or snapshot if this is a production machine.
These requirements matter because PHP installation touches system packages, service files, and sometimes your web server config. A small mistake can break a live site, so a backup gives you a fast rollback path.
Step 1: Update Your System
Refresh package lists
Start by updating the package index.
sudo apt update
This command asks Ubuntu to refresh its list of available packages. It matters because your server should install the latest package metadata before you add PHP or any related modules.
Upgrade installed packages
Next, upgrade what is already installed.
sudo apt upgrade -y
This reduces version conflicts and helps keep your base system current before PHP goes in. On a busy server, this also lowers the chance that older libraries will cause dependency problems later.
Install helper packages
Now install the tools needed to add external repositories safely.
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https -y
These packages matter because they help Ubuntu manage PPAs, verify TLS certificates, and handle repository access cleanly. Without them, adding a third-party PHP repository can fail or behave inconsistently.
Step 2: Install PHP from Ubuntu Repos
Install the base PHP package
If Ubuntu 26.04 already includes the PHP version you want, start with the native packages.
sudo apt install php php-cli php-fpm php-common -y
This installs the core PHP runtime, the command-line binary, the FastCGI process manager, and shared files. It is a good first choice because it keeps your install close to Ubuntu’s default support model.
Add common extensions
Most real apps need more than the base runtime.
sudo apt install php-mysql php-xml php-curl php-mbstring php-zip php-gd php-intl php-bcmath -y
These modules support database access, XML handling, HTTP requests, string processing, archives, image work, locale features, and math functions. That is why most production PHP sites need them right away.
Confirm the install
Check the version and loaded modules.
php -v
php -m
You want to see the installed PHP version and a module list with the extensions you expect. This is a fast way to confirm the install worked before you connect PHP to your web server.
Expected output example:
PHP 8.4.x (cli) (built: ...)
The exact version may differ depending on the repository you use, but the command should clearly show a working PHP binary.
Step 3: Use the Latest PHP via PPA
Add the Ondřej Surý repository
If Ubuntu’s default repo is not new enough, use the widely used PHP PPA.
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
This repository is popular because it provides co-installable PHP versions and newer releases than the main Ubuntu archive in many cases. That is useful when your app needs newer PHP features or when you manage multiple sites with different PHP requirements.
Install a specific PHP version
If you want a version such as 8.4, install it directly.
sudo apt install php8.4 php8.4-cli php8.4-fpm php8.4-common -y
This approach gives you more control than the generic php package. It also makes your setup easier to manage when you need to pin a specific runtime for a project.
Install versioned extensions
Use version-matched modules for the same PHP release.
sudo apt install php8.4-mysql php8.4-xml php8.4-curl php8.4-mbstring php8.4-zip php8.4-gd php8.4-intl php8.4-bcmath -y
This keeps your extension stack aligned with the PHP version you installed. Matching versions matters because it reduces compatibility issues and makes upgrades easier later.
Set the default CLI version
If you have more than one PHP version, choose the default CLI binary.
sudo update-alternatives --config php
This is important because Composer, cron jobs, and shell scripts use the CLI version, not the web server version. If the wrong binary is active, your tools may behave differently from your site.
Step 4: Configure PHP for Real Use
Edit php.ini for CLI or FPM
Open the file for the environment you need.
sudo nano /etc/php/8.4/cli/php.ini
Or, for web traffic:
sudo nano /etc/php/8.4/fpm/php.ini
This matters because CLI and FPM can use different values. For example, a developer may want longer execution time in the shell, while production web requests should stay tighter.
Adjust common settings
A basic set of safe values looks like this:
memory_limit = 256M
max_execution_time = 120
upload_max_filesize = 64M
post_max_size = 64M
expose_php = Off
These settings help prevent runaway scripts, improve upload support, and reduce information exposure. They also give you a more controlled baseline for a live system.
Restart PHP-FPM
Apply the changes after editing.
sudo systemctl restart php8.4-fpm
sudo systemctl status php8.4-fpm
Restarting is necessary because PHP-FPM reads config files at startup. If you skip this step, your new values will not take effect.
Step 5: Configure PHP-FPM with Nginx
Check the socket
After install, PHP-FPM usually creates a socket file.
ls -la /run/php/
This tells you which socket your web server should use. That matters because Nginx needs the exact socket path to pass PHP requests correctly.
Add the Nginx PHP block
A typical server block looks like this:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.4-fpm.sock;
}
This sends PHP requests to PHP-FPM instead of trying to run PHP inside Nginx. That design is faster and cleaner for most Linux server setups.
Test and reload Nginx
sudo nginx -t
sudo systemctl reload nginx
The test command checks for config errors before you apply the change. That is why every sysadmin should run it before reloading a live server.
Step 6: Configure PHP with Apache
Choose the Apache mode
Apache can run PHP in different ways.
sudo apt install libapache2-mod-php -y
This method is simple, but many admins now prefer PHP-FPM because it separates web serving from PHP execution. That separation often improves flexibility and makes multi-version setups easier.
Enable Apache PHP-FPM support
If you use PHP-FPM with Apache:
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.4-fpm
sudo systemctl restart apache2
This tells Apache to hand PHP files to FPM instead of processing them directly. It is the cleaner choice when you want better control over versions and pools.
Verify with a test file
Create a simple PHP info page.
echo "" | sudo tee /var/www/html/info.php
Open it in a browser, confirm PHP works, then delete it.
sudo rm /var/www/html/info.php
That file proves the web server and PHP runtime are connected correctly, but you should remove it because it exposes system details.
Step 7: Install Useful Extensions
Common production modules
Most apps need more than the base set.
sudo apt install php8.4-mysql php8.4-xml php8.4-curl php8.4-mbstring php8.4-zip php8.4-gd php8.4-intl php8.4-bcmath -y
These modules cover the needs of CMS platforms, APIs, e-commerce systems, and custom web apps. Installing them early saves time because you avoid debugging missing-extension errors later.
Check what is active
php -m | sort
This command shows the loaded modules in a readable order. It helps you confirm that the install matches the needs of your application stack.
Step 8: Keep It Secure
Limit exposure
Turn off features you do not need.
expose_php = Off
This hides version details from HTTP headers. That matters because you should not advertise more system information than needed.
Use tight permissions
Make sure your app files do not belong to everyone.
- Use a dedicated web user.
- Keep writable folders limited.
- Avoid broad 777 permissions.
- Store uploads and cache files in controlled paths.
These basics reduce the damage from a bad upload, a plugin bug, or a compromised account. Strong permissions are one of the cheapest security wins you can make.
Update regularly
Keep PHP and the OS patched.
sudo apt update
sudo apt upgrade -y
Regular updates matter because PHP and extension packages receive bug fixes and security patches over time. On a server, slow patching creates risk fast.
Troubleshooting
PHP command shows the wrong version
Check the active CLI version.
php -v
sudo update-alternatives --config php
If the wrong version appears, switch it with update-alternatives. This happens often when you install more than one PHP release.
PHP-FPM will not start
Inspect the service and config.
sudo systemctl status php8.4-fpm
sudo journalctl -u php8.4-fpm --no-pager -n 50
Most failures come from a bad config line, wrong socket permission, or a missing package. Checking the journal usually shows the real cause quickly.
Nginx gives a 502 Bad Gateway
Check the socket path and service status.
ls -la /run/php/
sudo systemctl status php8.4-fpm
A 502 often means Nginx points to the wrong socket or PHP-FPM is not running. This is a connection problem, not usually a PHP code problem.
Missing extension error
Install the package the app needs.
sudo apt install php8.4-xml php8.4-mbstring php8.4-curl -y
Many apps fail because one required module is missing. The error message usually names the extension, so read it carefully before changing anything else.
Changes do not appear after editing config
Restart the service.
sudo systemctl restart php8.4-fpm
sudo systemctl reload nginx
PHP-FPM and Nginx cache runtime state, so a restart or reload is often needed before new settings take effect. This is a normal part of server administration.