How to install PHP on Ubuntu - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

How to install PHP on Ubuntu
============================

### [\#PHP](https://rocketeersapp.com/knowledge/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](https://rocketeersapp.com/author/mark-van-eijk) on June 30, 2026 · 2 minute read

1. [Add the PHP repository](#content-add-the-php-repository)
2. [Install PHP-FPM and the extensions you need](#content-install-php-fpm-and-the-extensions-you-need)
3. [Verify the install](#content-verify-the-install)
4. [Connect PHP to Nginx](#content-connect-php-to-nginx)
5. [Tune it for production](#content-tune-it-for-production)
6. [Install Composer](#content-install-composer)
7. [Run more than one version](#content-run-more-than-one-version)
8. [Let Rocketeers handle it](#content-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.

[\#](#content-add-the-php-repository "Permalink")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

```

[\#](#content-install-php-fpm-and-the-extensions-you-need "Permalink")Install PHP-FPM and the extensions you need
-----------------------------------------------------------------------------------------------------------------

For a web server you want PHP-FPM (the FastCGI process manager [Nginx](/how-to-install-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.

[\#](#content-verify-the-install "Permalink")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`.

[\#](#content-connect-php-to-nginx "Permalink")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."

[\#](#content-tune-it-for-production "Permalink")Tune it for production
-----------------------------------------------------------------------

The default `php.ini` is conservative. Most sites need a higher [memory limit](/increase-php-memory-limit), realistic [upload limits](/php-file-upload-exceeds-upload-max-filesize), and [OPcache enabled](/enable-opcache-php) for speed. We walk through the settings that matter in [important PHP config options](/important-php-config-options).

[\#](#content-install-composer "Permalink")Install Composer
-----------------------------------------------------------

Most PHP applications need [Composer](https://getcomposer.org) for dependencies:

 ```
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

```

[\#](#content-run-more-than-one-version "Permalink")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](/how-to-install-multiple-php-versions-on-same-server).

[\#](#content-let-rocketeers-handle-it "Permalink")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 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!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#PHP](https://rocketeersapp.com/knowledge/php)

- [How to run PHP files](https://rocketeersapp.com/knowledge/run-php-files)
- [PHP file upload exceeds upload\_max\_filesize](https://rocketeersapp.com/knowledge/php-file-upload-exceeds-upload-max-filesize)
- [PHP Warning: Undefined array key](https://rocketeersapp.com/knowledge/undefined-array-key-php)
- [PHP Fatal error: Allowed memory size exhausted](https://rocketeersapp.com/knowledge/php-allowed-memory-size-exhausted)
- [PHP: Call to a member function on null](https://rocketeersapp.com/knowledge/call-to-a-member-function-on-null)
- [PHP Fatal error: Class "X" not found](https://rocketeersapp.com/knowledge/php-class-not-found)

 [View all 11 articles →](https://rocketeersapp.com/knowledge/php)
