How to Install PHP on Ubuntu (20.04/22.04) – Step-by-Step Beginner’s Guide

Introduction

Hey there! Want to install PHP on Ubuntu but not sure where to start? Don’t worry – you’re in the right place. Whether you’re setting up your first website, launching a Laravel project, or configuring WordPress, getting PHP up and running on Ubuntu is simpler than you think.

In this complete guide, we’ll cover:

  • Why PHP matters for Ubuntu servers

     

  • The quickest way to install PHP on Ubuntu

     

  • Installing specific PHP versions (7.4, 8.1, 8.2, 8.3, etc.)

     

  • Setting up PHP with Apache or Nginx

     

  • Adding extensions and fixing common errors

     

By the end, you’ll have a fully functional PHP environment on Ubuntu, ready to power your web applications.

Why Install PHP on Ubuntu?

PHP is the engine behind over 75% of websites, including giants like WordPress, Facebook, and Wikipedia. When combined with Ubuntu – one of the most reliable Linux distributions – you get a secure and stable platform for web development.

At CreativeON, we’ve been setting up PHP environments and hosting servers for over 20 years. We’ve worked with major brands like City42, Channel 24, and Chughtai Lab – so we know the best practices that keep PHP secure and lightning-fast on Ubuntu servers.

What You Need Before Installing PHP

Before we dive in, make sure you have:

  • An Ubuntu server (20.04 or newer recommended)

  • Basic terminal knowledge (you don’t need to be a pro!)

  • Admin access (sudo privileges)

  • An SSH connection if working remotely

Got all that? Great – let’s get started!

Method 1: Quick PHP Installation on Ubuntu

This is the easiest way to install PHP on Ubuntu.

Step 1: Update Your System

sudo apt update

 

This makes sure you’re installing the latest available packages.

Step 2: Install PHP with Common Extensions

sudo apt install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath

 

Here’s what some of these do:

  • php: Core PHP engine

  • php-cli: Run PHP commands in terminal

  • php-mysql: Connect PHP to MySQL databases

  • php-curl: Make web requests

  • php-gd: Process images

Step 3: Verify Installation

php -v

 

You should see something like PHP 8.3.3.

Test with a simple command:

php -r ‘echo “PHP is working!\n”;’

 

Method 2: Install Specific PHP Versions on Ubuntu

Need PHP 7.4, 8.1, or 8.2? Ubuntu’s default repo may not include all versions.

Step 1: Add the PHP Repository

sudo apt install software-properties-common

sudo add-apt-repository ppa:ondrej/php

sudo apt update

 

Step 2: Install Your Desired Version

sudo apt install php8.2

 

👉 Replace 8.2 with the version you need.

To manage multiple versions:

sudo update-alternatives –config php

 

Setting Up PHP with a Web Server

Option A: Apache (Classic Choice)

sudo apt install apache2

sudo apt install libapache2-mod-php8.3

sudo a2enmod php8.3

sudo systemctl restart apache2

 

Apache now runs PHP automatically.

Option B: Nginx (Faster Option)

sudo apt install nginx

sudo apt install php8.3-fpm

 

Edit Nginx config:

sudo nano /etc/nginx/sites-available/default

 

Add this inside the server block:

location ~ \.php$ {

    include snippets/fastcgi-php.conf;

    fastcgi_pass unix:/run/php/php8.3-fpm.sock;

}

 

Restart services:

sudo systemctl restart php8.3-fpm

sudo systemctl restart nginx

 

Testing Your PHP Installation

Create a test file:

echo “<?php phpinfo(); ?>” | sudo tee /var/www/html/info.php

 

Open in your browser:

http://your_server_ip/info.php

 

👉 Don’t forget to delete it afterward for security:

sudo rm /var/www/html/info.php

 

Adding PHP Extensions on Ubuntu

Need more features? Add extensions easily:

sudo apt install php8.3-xml php8.3-zip php8.3-curl

 

After installing, restart your server:

# For Apache

sudo systemctl restart apache2  

 

# For Nginx

sudo systemctl restart php8.3-fpm

sudo systemctl restart nginx

 

Troubleshooting PHP Installation on Ubuntu

  • PHP not working? Restart your web server and check configs.

  • Permission errors? Ensure files are owned by www-data.

  • Extensions not loading? Check with php -m and restart services.

  • Logs not clear? Review error logs in /var/log/apache2/ or /var/log/nginx/.

FAQs About PHP on Ubuntu

Always use the latest stable version unless your app requires an older one.

Yes! Use update-alternatives to switch between them.

Yes – if updated regularly, unused functions disabled, and best practices followed.

Why Choose CreativeON for VPS Hosting in Pakistan?

Installing PHP is only half the journey – you also need reliable hosting. At CreativeON, we offer:

  • 20+ years of experience in PHP hosting

  • Affordable VPS hosting in Pakistan with full root access

  • Trusted by top brands like City42, Channel 24, and Chughtai Lab

  • 24/7 expert support

  • Google Workspace Official Partner & PKNIC Gold Partner

👉 Whether you need cheap VPS in Pakistan, dedicated servers, or cloud hosting, CreativeON has you covered.

Wrapping Up

Congratulations! 🎉 You’ve learned how to install PHP on Ubuntu – whether using quick installation or choosing specific versions. You can now set up PHP with Apache or Nginx, add extensions, and troubleshoot issues confidently.

Remember, the foundation of any successful PHP project is reliable hosting. That’s where CreativeON comes in – providing VPS, dedicated servers, and Google Workspace solutions trusted by businesses across Pakistan.

The author
Asher Feroze

I’m Asher Feroze, and I’ve been part of CreativeON for several years, working in various roles including Manager Operations, Business Development Manager, and technical support for our web hosting services. Over time, I’ve gained deep insights into both the business and technical sides of the industry. Now, I use that experience to write informative articles for CreativeON, Gworkspace, and gworkspacepartner.pk, helping readers make smart choices when it comes to web hosting and Google Workspace solutions.

Table of Contents