Install and Uninstall PHP on Ubuntu [Step by Step Guide]

Learn how to install and uninstall PHP on Ubuntu with this easy step-by-step guide. Perfect for developers and beginners. Follow our simple instructions now!

Updated: 01 Jun, 24 by Lisa P 13 Min

List of content you will read in this article:

Have you ever wanted to build websites and web applications but weren't sure where to start? Well, if you're an Ubuntu user, then PHP is a powerful tool you'll need in your arsenal. If you don’t know how to install PHP Ubuntu or uninstall it on your Ubuntu system then you`re in the right place. In this blog post, we'll guide you how to install and uninstall PHP Ubuntu system. So, no different you're a seasoned developer or just starting your web development journey, this guide will help you. 

The accessible PHP versions in Ubuntu depend on the Ubuntu version and the sources that are enabled. Here's a brief review of the PHP versions often available in different Ubuntu versions:

  • Ubuntu 20.04 (Focal Fossa): The default version is PHP 7.4.
  • Ubuntu 22.04 (Jammy Jellyfish): The default version is PHP 8.1.
  • Ubuntu 23.10 (Kinetic Kudu): The default version is PHP 8.2.
  • Ubuntu 24.04 (LTS, due for release in April 2024): The expected default version is PHP 8.3.

Note: These are the default versions available by the official Ubuntu repository. You can install additional PHP versions using Personal Package Archives (PPAs) if necessary.  For example, if you want an older version, such as PHP 5.6, you can add a PPA that includes it for compatibility reasons.

Some additional points to consider

  • Security upgrades: Newer PHP versions usually involve higher security features and more frequent safety updates. For additional safety, we recommend using the latest stable version whenever possible for better security.
  • Application Compatibility: Ensure that the PHP version you select is compatible with your web applications. If you're unsure, consult your application's documentation.
  • Multiple Versions: Ubuntu allows you to have multiple PHP versions installed simultaneously. This is handy for development, but make sure your web server is configured to use the correct version for each application.

Understanding how to install PHP Ubuntu gives you the ability to create your own web development environment. Then, you`re allowed to test and create dynamic apps and websites locally. This provides more control, flexibility, and affordability than relying entirely on external hosting services. Now, let`s see how to install PHP Ubuntu with different methods. 

Prerequisites

For installing PHP on Ubuntu, you need 3 main prerequisites:

  1. Ubuntu system: You'll need a computer running a recent version of Ubuntu.
  2. Sudo privileges: You'll need access to an Ubuntu user account with sudo privileges to install software packages.
  3. Terminal access: You'll need to be comfortable using the terminal window to execute commands during the installation process.

Method #1: Install PHP with Apache on Ubuntu

Installing PHP with Apache on Ubuntu is a great way to create a local development environment. These tutorials will show you how to install PHP with Apache on your Ubuntu server. You'll be able to choose between PHP 7.4 and 8.1.

Linux  VPS
Debian VPS Server

Starting From $5.99/Monthly

Update and upgrade: This command updates the package lists and upgrades any existing packages on your system.

sudo apt update && sudo apt upgrade

Install PPA prerequisites: This installs the necessary tools to work with Personal Package Archives (PPAs).

sudo apt install software-properties-common

Add PHP PPA:  This step provides various PHP versions.

sudo add-apt-repository ppa:ondrej/php

Update package lists (Do this again): Run this command again to ensure the package lists include the new PPA repository.

sudo apt update

Install Apache and PHP 7.4, 8.1

Install Apache and PHP: Choose the version you want to install by replacing `7.4` with `8.1` in the following command:

sudo apt install apache2 php<version> libapache2-mod-php<version>

For example, to install Apache and PHP 7.4:

sudo apt install apache2 php7.4 libapache2-mod-php7.4

Restart Apache: This command restarts the Apache service to activate the changes.

sudo systemctl restart apache2

Verify PHP Installation

Verify PHP Installation: There are two ways to verify the installation:

Firs way: Check version, which displays the installed PHP version.

php -v

Second way: Creating a test page. This way you should create a file named `info.php` in your web server's document root (usually `/var/www/html`). You can use a text editor like `nano`:

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

Paste the following code into the file:

<?php

phpinfo();

?>

If the version 7.4 is installed successfully, you’ll see something like the following image:

Install PHP with Apache on Ubuntu

On the other hand, if you have installed version 8.1, the version will show:

Install PHP with Apache on Ubuntu

Method #2: Install PHP with NGINX on Ubuntu

NGINX requires additional settings to handle PHP, unlike Apache which supports it natively. The first steps are the same:

  • Update system packages:

sudo apt update && sudo apt upgrade

  • Install PPA prerequisites: We'll be using a Personal Package Archive (PPA) for PHP installation. This step installs the necessary tools to work with PPAs.

sudo apt install software-properties-common

  • Add PHP PPA: This command adds the PPA repository containing the desired PHP version to your system's package list. 

sudo add-apt-repository ppa:ondrej/php

  • Update package list: After adding the new repository, refresh the package list to include the newly available PHP packages.

sudo apt update

Install NGINX and PHP 7.4

Now that we've prepared the system for PHP with the PPA repository, it's time to install the essential components:

To Install NGINX, run the following command:

sudo apt install nginx

This command retrieves and installs the latest stable version of NGINX from the official Ubuntu repositories. NGINX will act as the web server, meaning it handles incoming requests and delivering web content to your users.

To Install PHP 7.4, use the following command:

sudo apt install php7.4-fpm

In this command, we installed `php7.4-fpm`. This specific package provides PHP 7.4 functionality in a FastCGI Process Manager (FPM) configuration. FPM helps manage multiple PHP worker processes efficiently and as a result, performance and scalability for your web applications will be improved. 

Install NGINX and PHP 8.1

Now, you only need to run the following command for installing NGINX and PHP 8.1:

sudo apt install nginx php8.1-fpm

Configure NGINX to use PHP-FPM

Now that NGINX and PHP are installed, we need to configure NGINX to understand how to handle PHP requests and work together with PHP-FPM.

  • Edit the default NGINX configuration: Open the default Nginx configuration file using your preferred text editor. We'll use `nano` here, but you can choose another editor you're comfortable with.

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

 

Master Nano quickly and easily! Our step-by-step guide on How to Use Nano Text Editor will help you become a pro in no time.

  • Enable PHP processing: Inside the configuration file, locate the block that looks something like `location ~ \.php$`. This block defines how NGINX should handle files with the `.php` extension (PHP scripts). Uncomment the lines within this block (you can do it by removing the leading `;` character) to activate PHP processing with NGINX.
  • Save and restart NGINX: After enabling the PHP processing block, save the changes made to the configuration file. Then, restart NGINX to apply the new configuration.

sudo systemctl restart nginx

  • Restart PHP-FPM service (depending on your PHP version): Finally, restart the PHP-FPM service to ensure it's ready to handle PHP requests from NGINX. The specific command will depend on the PHP version you installed:

For PHP 7.4:

sudo systemctl restart php7.4-fpm

For PHP 8.1:

sudo systemctl restart php8.1-fpm

Now, your Ubuntu server can handle dynamic web content powered by PHP!

Verify PHP Installation

Once you've configured NGINX and PHP-FPM, let's confirm everything is working as expected. First of all, you need to create a simple test file. The following command will create a file named `phpinfo.php` in your web server's document root. This script uses the `phpinfo()` function to display detailed information about your PHP installation.

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

Explanation of this command:

  • echo "<?php phpinfo(); ?>": This part generates the content for the PHP script, which simply calls the `phpinfo()` function.
  • | sudo tee /var/www/html/phpinfo.php: The pipe (`|`) redirects the output of the `echo` command to the `tee` command. `tee` creates a new file while simultaneously displaying the content on the terminal. We use `sudo` for write permissions to the document root.

Now, you need to access the test script in your browser: Open a web browser and go to the following URL. Replace `<your_server_ip>` with the actual IP address of your server:

http://<your_server_ip>/phpinfo.php

If everything is set up correctly, you should see a detailed page displaying information about your PHP configuration, including the version, loaded modules, and environment variables.

PHP modules, also known as extensions, act like building blocks that add functionalities to the core PHP-FPM package. These modules provide features for various tasks, such as database interaction, image manipulation, or working with email.

To install a specific PHP module on your Ubuntu system, you can use the following command format:

sudo apt install php<version>-<module_name>

Explanation of this command:

  • <version>: Replace this with the PHP version you're using (e.g., `7.4`, `8.1`).
  • <module_name>: This represents the specific module you want to install. Common module names include `mysql`, `gd`, `curl`, etc.

For example, if you want to install the `mysql` module for PHP 7.4, you would run:

sudo apt install php7.4-mysql

If you want to view all active PHP modules on the system, run the following command: 

# php -m

Before we tell you how to uninstall PHP Ubuntu, you should know why you might need to uninstall it. There are a few reasons to uninstall PHP Ubuntu:

  • No longer needed: If you are not using PHP for any web applications on your server and it is no longer serving a purpose, you should uninstall it because it can free up disk space while also potentially improving system security by removing unneeded software.
  • Switching to a new web framework: Perhaps you're moving your web applications to a framework that doesn't use PHP. Uninstalling PHP would be part of the process.
  • Troubleshooting problems: In some cases, PHP may cause conflicts with other applications on your system. So, you should uninstall it for a troubleshooting approach to isolate the problem.
  • Security concerns: If your PHP version has a known security risk and you are unable to update to a repaired version, uninstalling may be a temporary solution until the problem is fixed.

Important considerations before uninstalling

Before you uninstall PHP Ubuntu, ensure that you understand the ramifications of eliminating it.  If you have any web applications relying on PHP, they will cease to function. It's always a good idea to back up any important data before making major system modifications.

Uninstalling PHP Ubuntu is a useful skill in maintaining a server. It frees up disk space, increases security by deleting unwanted applications, and helps with debugging issues or replacing web frameworks. Even if you don't want to uninstall right now, understanding the process allows you to make better decisions about your server's resource allocation and functioning.

Uninstall PHP 7.4 on Ubuntu

To uninstall PHP 7.4 on your Ubuntu system, run the following command. It will remove the core PHP package:

sudo apt remove --purge php7.4

Now, if you already have installed PHP-FPM, use the command below to uninstall it:

sudo apt remove --purge php7.4-fpm

At last, you need to remove the PHP configuration files. To do that, run the following command:

sudo rm -rf /etc/php/7.4

Uninstall PHP 8.1 on Ubuntu

The process of uninstalling PHP 8.1 is the same as uninstalling PHP 7.4. To remove PHP 8.1 from your Ubuntu system:

  • Remove the core package: sudo apt remove --purge php8.1
  • Remove PHP-FPM: sudo apt remove --purge php8.1-fpm
  • Delete configuration files: sudo rm -rf /etc/php/8.1

(Note: This step is optional. Using this command, the configuration directory specifically for PHP 8.1 will be removed. If you're sure you won't need PHP 8.1 again, we recommend using this command.)

  • Restart the web server: The specific command depends on your web server:

For Apache:

sudo systemctl restart apache2

For NGINX:

sudo systemctl restart nginx

Verify uninstallation: This step is also optional if you want to confirm PHP is uninstalled. All you need to do is run this command:

php -v

If PHP is successfully removed, this command should display "command not found."

In this blog post, we covered how to install PHP Ubuntu and uninstall it. Remember to back up your data before making any big system changes. With the knowledge learned from this guide, you'll be able to confidently manage your Ubuntu server! 

Update system packages: sudo apt update && sudo apt upgrade Install PPA prerequisites (one-time step):  sudo apt install software-properties-common Add the desired PHP PPA (replace `ondrej/php` with the specific PPA for your version): sudo add-apt-repository ppa:/php Update package list again: sudo apt update Install PHP and PHP-FPM (replace `php8.1` with your desired version): sudo apt install php-fpm

To install PHP 7.0 on Ubuntu, use the following commands: sudo apt update sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt install php7.0

To install PHP 8.2 on Ubuntu, use the following commands: sudo apt update sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt install php8.2

To install a specific version of PHP in Ubuntu, replace `phpX.X` with the desired version in the following commands: sudo apt update sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt install phpX.X For example, to install PHP 7.4, use `php7.4` in the last command.

Lisa P

Lisa P

Hello, everyone, my name is Lisa. I'm a passionate electrical engineering student with a keen interest in technology. I'm fascinated by the intersection of engineering principles and technological advancements, and I'm eager to contribute to the field by applying my knowledge and skills to solve real-world problems.