Tutorials

Install PHP on Ubuntu: Complete Setup Guide for Apache & Nginx

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.

What You Need Before Starting

Before running any commands, make sure you have:

  • A server or local machine running Ubuntu (20.04 or later)
  • Sudo privileges on the system
  • A web server already installed either Apache or Nginx

The default Ubuntu repositories include a stable, tested PHP release. Always verify that your application supports the version available before installing.

How to Install PHP on Ubuntu with Apache

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.

How to Install PHP on Ubuntu with Nginx

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.

Installing PHP Extensions

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.

Installing a Specific PHP Version

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

Verifying Your PHP Installation

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.

Cyber Defence

Recent Posts

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 days ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

2 days ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

3 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

6 days ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

6 days ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

6 days ago