Knowledge
How to install PHP on Ubuntu
#PHP
A modern PHP install means more than one apt package — you need PHP-FPM and the right set of extensions for your application. Here is how to install PHP on Ubuntu and wire it into Nginx.
Published by Mark van Eijk on June 30, 2026 · 2 minute read
- Add the PHP repository
- Install PHP-FPM and the extensions you need
- Verify the install
- Connect PHP to Nginx
- Tune it for production
- Install Composer
- Run more than one version
- Let Rocketeers handle it
Ubuntu's default repositories are usually a PHP version or two behind, and they don't always carry every extension you'll need. The standard fix is Ondřej Surý's PPA, which packages every current PHP release for Ubuntu and keeps them up to date. Here's how to install PHP properly for a web server.
Add the PHP repository
Add the PPA that almost every production PHP server uses:
sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update
Install PHP-FPM and the extensions you need
For a web server you want PHP-FPM (the FastCGI process manager Nginx talks to), the CLI, and the common extensions. This installs PHP 8.4 with the set a typical Laravel or modern PHP app expects:
DEBIAN_FRONTEND=noninteractive sudo apt-get install -y \
php8.4-fpm php8.4-cli php8.4-curl php8.4-mbstring php8.4-xml php8.4-zip \
php8.4-mysql php8.4-pgsql php8.4-sqlite3 php8.4-gd php8.4-intl php8.4-bcmath \
php8.4-redis php8.4-opcache php8.4-readline
Swap 8.4 for whichever version you need — 8.3, 8.2, and so on are all available from the same PPA.
Verify the install
Check the CLI version and that the FastCGI service is running:
php -v
sudo systemctl status php8.4-fpm
PHP-FPM listens on a Unix socket at /var/run/php/php8.4-fpm.sock. That's the path your Nginx server block passes requests to with fastcgi_pass.
Connect PHP to Nginx
In your site's server block, hand .php files to the FPM socket:
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Test and reload Nginx, then drop a phpinfo() file in your web root to confirm PHP is executing:
sudo nginx -t && sudo service nginx reload
If you see the raw PHP source instead of a rendered page, the location ~ \.php$ block isn't matching — that's the usual cause of "PHP code showing in the browser."
Tune it for production
The default php.ini is conservative. Most sites need a higher memory limit, realistic upload limits, and OPcache enabled for speed. We walk through the settings that matter in important PHP config options.
Install Composer
Most PHP applications need Composer for dependencies:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Run more than one version
Different sites often need different PHP versions on the same box. That's entirely possible — see how to install multiple PHP versions on the same server. And once it's installed, here's how to run PHP files.
Let Rocketeers handle it
Picking the right extension set, keeping FPM tuned, enabling OPcache, installing Composer, and doing it again for every PHP version your sites need is fiddly, repetitive work. Rocketeers provisions PHP with the full production extension set, tunes php.ini and FPM sensibly, and lets each site pick its own PHP version — no PPAs or socket paths to remember.
Subscribe to our newsletter
Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!