How to install PHP on Ubuntu 24
To install PHP on Ubuntu you only need packages from Ubuntu’s own repository: PHP 8.3 on Ubuntu 24.04 and PHP 8.1 on 22.04. This guide sets PHP up the way most Nginx servers run it, through PHP-FPM. It also covers extensions, where php.ini lives and how to get another PHP version when the one in Ubuntu is too old.
Install PHP on Ubuntu with apt
For a site behind Nginx you need two packages: php-fpm, the service that runs PHP for the web server, and php-cli, the php command for scripts and Composer. Both pull in the version your Ubuntu ships.
sudo apt update
sudo apt install -y php-fpm php-cli
php -v
systemctl status php8.3-fpm --no-pagerThe version check prints, for example PHP 8.3.6 (cli). The service status should show Active: active (running). On Ubuntu 22.04 the service is called php8.1-fpm, and every 8.3 in this guide becomes 8.1.
Don’t start with the bare php package. When no PHP-FPM is installed yet, that package brings in the Apache module, and Apache with it, which then fights Nginx for port 80.
Connect PHP-FPM to Nginx
Nginx doesn’t run PHP itself. It hands .php requests to PHP-FPM over a socket file. If Nginx isn’t on the server yet, start with how to install Nginx on Ubuntu. Then open your site’s file, for example /etc/nginx/sites-available/example.com, and make it look like this:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}snippets/fastcgi-php.conf comes with Ubuntu’s Nginx. The socket name carries the PHP version. To see the one on your server:
ls /run/php/Now add a test page, check the configuration and reload Nginx:
echo '<?php phpinfo();' | sudo tee /var/www/example.com/info.php
sudo nginx -t
sudo systemctl reload nginxOpen http://example.com/info.php. The page shows the PHP version, and the Server API line says FPM/FastCGI.

The phpinfo page shows your server’s settings to anyone who opens it. Delete it as soon as you’ve looked. This removes only the test file: sudo rm /var/www/example.com/info.php
Install PHP extensions
Extensions come as separate packages named php-<name>. These names follow Ubuntu’s default PHP version. For one exact version, use php8.3-<name>. Here are the ones most frameworks and CMSs ask for:
sudo apt install -y php-mysql php-pgsql php-curl php-mbstring php-xml php-zip php-gd php-intl php-bcmath
sudo systemctl restart php8.3-fpm
php -mPHP-FPM loads extensions only when it starts, so restart it after every install. The last command lists the extensions the command line sees. To turn an installed extension off, for example intl:
sudo phpdismod intlAnd to turn it back on:
sudo phpenmod intlWhere is php.ini on Ubuntu
Ubuntu keeps a separate php.ini for each way PHP runs, so a setting changed in one file doesn’t reach the other:
/etc/php/8.3/fpm/php.ini: for sites served through PHP-FPM./etc/php/8.3/cli/php.ini: for thephpcommand, cron jobs and Composer./etc/php/8.3/mods-available/: one small ini file per extension./etc/php/8.3/fpm/pool.d/www.conf: the FPM pool, with its user, socket and number of processes.
To see which file the command line loads:
php --iniFor the web side, look at Loaded Configuration File on the phpinfo page. After you edit the FPM file, apply it:
sudo systemctl restart php8.3-fpmCommon changes are covered in how to raise the PHP memory limit.
Check and switch PHP versions
To see the command-line version and every version installed:
php -v
ls /etc/php/Ubuntu keeps one PHP version for the life of each release. For a newer one, the usual source is the ppa:ondrej/php PPA. It is a third-party repository. Ondřej Surý, who also maintains PHP packages in Debian, runs it, and it is not part of Ubuntu or php.net. Add it only if you need that version.
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install -y php8.4-fpm php8.4-cli php8.4-mysqlVersions live side by side, each with its own service and socket. Point a site at the new one by changing fastcgi_pass to unix:/run/php/php8.4-fpm.sock and reloading Nginx. The php command follows on its own: in automatic mode it takes the newest version, here 8.4. To pick another one:
sudo update-alternatives --config php
php -vThe PPA’s page says it is being merged into packages.sury.org. Read the notes on its Launchpad page before you add it, and add extensions with the same version prefix (php8.4-curl).
If PHP doesn’t work
- The browser downloads the
.phpfile or shows its code: the site has nolocation ~ \.php$block, or Nginx wasn’t reloaded. Check the configuration:sudo nginx -tThen reload Nginx:sudo systemctl reload nginx 502 Bad Gateway, and the Nginx log saysconnect() to unix:/run/php/php8.1-fpm.sock failed (2: No such file or directory): the socket infastcgi_passhas the wrong version, or PHP-FPM is stopped. See which sockets exist:ls /run/php/Then check the service:systemctl status php8.3-fpmPHP Fatal error: Uncaught Error: Call to undefined function mysqli_connect(): the MySQL extension is missing. Installphp-mysqland restart PHP-FPM.- A change in php.ini does nothing: you edited the
clifile instead offpm, or didn’t restart PHP-FPM. - For anything else, read the Nginx log:
sudo tail -n 50 /var/log/nginx/error.logThen the PHP-FPM log:sudo journalctl -u php8.3-fpm -n 50
If you also work on a Mac

System Junk in MacWashing finds the caches of every app it knows, including those hidden inside app data, plus logs, Xcode junk and leftovers of deleted apps. It shows how much each takes and clears them in one go. Anything debatable stays unchecked until you decide.
Free trial · macOS 14 or later
Questions and answers
How do I install PHP on Ubuntu?
Update the package list: sudo apt update Then install PHP: sudo apt install php-fpm php-cli You get PHP 8.3 on Ubuntu 24.04 and PHP 8.1 on 22.04, with the FPM service already running.
Do I need Apache to run PHP?
No. With PHP-FPM, Nginx passes PHP requests to it over a socket. Apache comes in only if you install the php package before any FPM, or ask for libapache2-mod-php.
Can I have two PHP versions on one server?
Yes. Each version has its own php8.x-fpm service and socket, so every site can use its own. The php command uses the one chosen with update-alternatives.