PHP is the backbone of the web. Frameworks like Laravel, WordPress, and Magento all run on it making it one of the most essential packages to have on any Linux server. If you need to install PHP on Ubuntu quickly and correctly, this guide walks you through every step: setting it up with Apache, configuring it with Nginx using PHP-FPM, installing extensions, and switching to older PHP versions when your app demands it.
Before running any commands, make sure you have:
The default Ubuntu repositories include a stable, tested PHP release. Always verify that your application supports the version available before installing.
Apache has native PHP support through a dedicated module, making the setup straightforward. Run these two commands:
bashsudo apt updatesudo apt install php libapache2-mod-php
The libapache2-mod-php package loads PHP directly into Apache as a module. Once installed, restart Apache to activate it:
bashsudo systemctl restart apache2
That’s all it takes. Apache will now process .php files automatically on every incoming request.
Nginx does not handle PHP natively it delegates that job to PHP-FPM (FastCGI Process Manager), which runs PHP as a separate background service. Install both packages with:
bashsudo apt updatesudo apt install php-fpm
The FPM service starts automatically after installation. Verify it is running:
bashsystemctl status php8.3-fpm
Next, update your Nginx server block configuration to forward PHP requests to the FPM socket:
nginxlocation ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.3-fpm.sock;} Restart Nginx to apply the change:
bashsudo systemctl restart nginx
Pro tip: Always match the PHP version number in the socket path to the version you actually installed. A mismatch here is one of the most common causes of a 502 Bad Gateway error.
The base PHP package is minimal by design. Most real-world applications require additional PHP extensions for database access, image handling, and caching. Install any extension using the apt package manager:
bashsudo apt install php-[extension-name]
For example, to add MySQL and GD support:
bashsudo apt install php-mysql php-gd
After adding extensions, restart either Apache or the PHP-FPM service — whichever your setup uses — so PHP picks up the new modules.
When an older application requires a specific PHP release, add Ondřej Surý’s repository, which maintains multiple PHP versions for Ubuntu:
bashsudo apt install software-properties-commonsudo add-apt-repository ppa:ondrej/php
Then install any version by specifying it in the package name:
bashsudo apt install php8.1 php8.1-cli php8.1-mysql php8.1-gd
Create a test file in your web root to confirm PHP is processing correctly:
php<?php phpinfo(); ?>
Save it as info.php inside /var/www/html/, then open http://your-server-ip/info.php in a browser. You should see a detailed PHP configuration page. Remove this file once you’ve confirmed everything works — leaving it publicly accessible is a security risk.
Getting PHP running on Ubuntu takes under five minutes with the right commands. Whether you’re pairing it with Apache or Nginx, the process is clean and repeatable. Once PHP is live, explore installing Composer for dependency management or set up a full LAMP or LEMP stack to round out your server environment.
Redis is an open-source, in-memory key-value store built for raw speed and versatility. It works equally well as…
Skype doesn't ship with Ubuntu by default it's a proprietary application owned by Microsoft and…
Cybersecurity teams use OSINT to see what attackers can already see from public sources. In…
Legal OSINT is about collecting and analyzing publicly available information without bypassing privacy controls, breaking…
Developers building cross-platform .NET applications often rely on Mono Ubuntu Install to create a flexible…
Spotify is one of the most popular music streaming services worldwide, giving you instant access…