PHP powers a huge share of the web, including WordPress, Laravel, and Magento. This guide covers installing PHP on Ubuntu and Debian using the Ondřej Surý repository, which provides access to multiple PHP versions and timely security updates.
Prerequisites
- Ubuntu 22.04/24.04 or Debian 11/12 VPS
- Root or sudo access
Step 1 — Update the System
sudo apt update
sudo apt upgrade -y
Step 2 — Add the PHP Repository
On Ubuntu:
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
On Debian:
sudo apt install apt-transport-https lsb-release ca-certificates curl -y
curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
sudo apt update
Step 3 — Install PHP
sudo apt install php8.3 -y
Adjust the version number for your needs (e.g. php8.2, php8.4).
Step 4 — Install Common Extensions
sudo apt install php8.3-cli php8.3-fpm php8.3-common php8.3-mysql \
php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip \
php8.3-bcmath php8.3-intl -y
Step 5 — Verify Installation
php -v
Step 6 — Connect PHP to Your Web Server
For Nginx (via PHP-FPM):
sudo systemctl enable php8.3-fpm
sudo systemctl start php8.3-fpm
Add to your Nginx server block:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
For Apache:
sudo apt install libapache2-mod-php8.3 -y
sudo systemctl restart apache2
Step 7 — Test PHP
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Visit http://YOUR_SERVER_IP/info.php, then remove it once confirmed working:
sudo rm /var/www/html/info.php
Installing Multiple PHP Versions Side by Side
sudo apt install php8.1 php8.1-fpm php8.3 php8.3-fpm -y
Each version installs independently, letting different sites use different PHP-FPM sockets simultaneously.
Switching the CLI Default Version
sudo update-alternatives --config php
Common Errors
PHP files download instead of executing — confirm PHP-FPM is running and the Nginx location block correctly points to the right socket path/version.
"php: command not found" — the CLI package wasn't installed; run sudo apt install php8.3-cli.
Best Practices
- Install only the extensions your application actually requires
- Keep PHP updated with security patches
- Remove the
info.phptest file after verifying installation — it exposes server configuration details
FAQ
Which PHP version should I use?
Use the latest stable version supported by your application's framework — check your framework's documentation for minimum/maximum supported PHP versions before choosing.
Related Articles
- How to Install Composer for PHP Dependency Management
- How to Install Nginx on Ubuntu & Debian
- How to Deploy a Laravel Application on a VPS
