BLOG POSTS
    MangoHost Blog / Installing and Managing PHP on Ubuntu: Step-by-Step Tutorial for 2024
Installing and Managing PHP on Ubuntu: Step-by-Step Tutorial for 2024

Installing and Managing PHP on Ubuntu: Step-by-Step Tutorial for 2024

The popularity and distribution of web server and PHP combinations on Ubuntu servers can vary, but here are some estimates based on surveys, reports, and industry trends:

Configuration Percentage
PHP + Apache 48%
PHP + NGINX 30%
Apache without PHP 10%
NGINX without PHP 8%
NGINX with Apache 4%

PHP remains widely used in 2024 due to:

  • Web Development Focus: Optimized for creating dynamic websites.
  • Widespread Adoption: Supported by major CMS platforms.
  • Strong Community: A rich ecosystem of frameworks and libraries.
  • Easy to Learn: Simple syntax encourages new developers.
  • Performance Improvements: Recent versions enhance speed and efficiency.
  • Legacy Systems: Many existing applications continue to rely on PHP.

Stable Version Table for Ubuntu LTS

Ubuntu LTS Version Release Date Default PHP Version
Ubuntu 18.04 April 2018 PHP 7.2
Ubuntu 20.04 April 2020 PHP 7.4
Ubuntu 22.04 April 2022 PHP 8.1
Ubuntu 24.04 April 2024 PHP 8.3

Table of Contents

  1. PHP Versions in Ubuntu
  2. How to Install PHP on Ubuntu?
  3. Install PHP Modules on Ubuntu
  4. Why Uninstall PHP from Ubuntu?
  5. How to Uninstall PHP on Ubuntu?
  6. Final Words
  7. FAQ

PHP Versions in Ubuntu

The accessible PHP versions in Ubuntu vary depending on the version of Ubuntu you are using and the repositories you have enabled. Below is a brief overview of the default PHP versions available for various Ubuntu releases:

  • 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): Version is PHP 8.3.

Note that these versions are the defaults available through the official Ubuntu repositories. You can install additional PHP versions by using Personal Package Archives (PPAs) if needed. For instance, if you require an older version like PHP 5.6, you can add a PPA that includes it for compatibility reasons.

Considerations

  • Security Upgrades: Newer PHP versions typically offer enhanced security features and more frequent updates. For optimal security, use the latest stable version whenever possible.
  • Application Compatibility: Ensure the PHP version you select is compatible with your web applications. Refer to your application’s documentation if unsure.
  • Multiple Versions: Ubuntu supports having multiple PHP versions installed simultaneously. This is beneficial for development but ensure your web server is configured to use the correct version for each application.

How to Install PHP on Ubuntu?

Understanding how to install PHP on Ubuntu allows you to set up a local web development environment, giving you the ability to test and create dynamic web applications and websites. This section will guide you through the process of installing PHP on Ubuntu using different methods.

Prerequisites

Before you begin, ensure you have the following:

  • An Ubuntu system
  • Sudo privileges
  • Access to the terminal

Method 1: Install PHP with Apache

Installing PHP with Apache on Ubuntu is a popular method for creating a local development environment. Follow these steps to install PHP with Apache:

  1. Update and upgrade your system:
    sudo apt update && sudo apt upgrade

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

  2. Install PPA prerequisites:
    sudo apt install software-properties-common

    This installs the necessary tools to work with Personal Package Archives (PPAs).

  3. Add PHP PPA:
    sudo add-apt-repository ppa:ondrej/php

    This step adds a PPA that provides various PHP versions.

  4. Update package lists again:
    sudo apt update

    Run this command again to ensure the package lists include the new PPA repository.

  5. Install Apache and PHP:
    sudo apt install apache2 php libapache2-mod-php

    Replace <version> with the desired PHP version (e.g., 7.4 or 8.1).

  6. Restart Apache:
    sudo systemctl restart apache2

    This command restarts the Apache service to activate the changes.

  7. Verify PHP Installation:There are two ways to verify the installation:
    • Check the version:
      php -v

      This command displays the installed PHP version.

    • Create a test page: Create a file named info.php in your web server’s document root (usually /var/www/html):
      sudo nano /var/www/html/info.php

      Paste the following code into the file:

      <?php
      phpinfo();
      ?>

      Save the file and access it via your browser at http://<your_server_ip>/info.php.

Method 2: Install PHP with NGINX

Unlike Apache, NGINX requires additional configuration to handle PHP. Follow these steps to install PHP with NGINX:

  1. Update and upgrade your system:
    sudo apt update && sudo apt upgrade
  2. Install PPA prerequisites:
    sudo apt install software-properties-common
  3. Add PHP PPA:
    sudo add-apt-repository ppa:ondrej/php
  4. Update package lists:
    sudo apt update
  5. Install NGINX and PHP:
    sudo apt install nginx php-fpm

    Replace <version> with the desired PHP version (e.g., 7.4 or 8.1).

  6. Configure NGINX to use PHP-FPM:Edit the default NGINX configuration file:
    sudo nano /etc/nginx/sites-available/default

    Uncomment the lines within the location ~ \.php$ block to enable PHP processing. Save the changes and restart NGINX and PHP-FPM:

    sudo systemctl restart nginx
    sudo systemctl restart php-fpm
  7. Verify PHP Installation:
    echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php

    Access the test script in your browser at http://<your_server_ip>/phpinfo.php.

Install PHP Modules on Ubuntu

PHP modules, also known as extensions, add functionalities to the core PHP package. To install a specific PHP module, use the following command format:

sudo apt install php-

Replace <version> with the PHP version you’re using (e.g., 7.4, 8.1) and <module_name> with the specific module name (e.g., mysql, gd, curl).

For example, to install the MySQL module for PHP 7.4:

sudo apt install php7.4-mysql

To view all active PHP modules on your system:

php -m

This command lists all enabled PHP modules, helping you verify installations and diagnose issues.

Why Uninstall PHP from Ubuntu?

There are various reasons to uninstall PHP from your Ubuntu system, including:

  • No Longer Needed: If you are no longer using PHP or the applications that require it, uninstalling it can free up system resources.
  • Switching to a New Web Framework: If you are migrating to a different web framework that doesn’t require PHP, you may choose to remove PHP.
  • Troubleshooting Conflicts: Sometimes, uninstalling and reinstalling PHP can resolve conflicts or issues with other software.
  • Addressing Security Concerns: If a particular PHP version has known vulnerabilities and you can’t update, uninstalling it might be a temporary solution until you can upgrade or migrate.

Considerations Before Uninstalling

Before proceeding with uninstallation, ensure that removing PHP will not disrupt any critical web applications. Always back up your important data and configurations before making significant system changes.

How to Uninstall PHP on Ubuntu?

Uninstalling PHP from Ubuntu can free up disk space, enhance system security, and help resolve software conflicts. Here are the detailed steps to uninstall different versions of PHP.

  1. Remove the core PHP package:
    sudo apt remove --purge php7.4

    This command removes the PHP 7.4 package and associated configuration files.

  2. Remove PHP-FPM:
    sudo apt remove --purge php7.4-fpm

    This command removes the PHP FastCGI Process Manager (FPM) for PHP 7.4.

  3. Remove configuration files:
    sudo rm -rf /etc/php/7.4

    This command deletes any remaining PHP 7.4 configuration files.

Uninstall PHP 8.1

  1. Remove the core PHP package:
    sudo apt remove --purge php8.1

    This command removes the PHP 8.1 package and associated configuration files.

  2. Remove PHP-FPM:
    sudo apt remove --purge php8.1-fpm

    This command removes the PHP FastCGI Process Manager (FPM) for PHP 8.1.

  3. Remove configuration files:
    sudo rm -rf /etc/php/8.1

    This command deletes any remaining PHP 8.1 configuration files.

  4. Restart your web server:
    sudo systemctl restart apache2
    sudo systemctl restart nginx

    This ensures that your web server is running correctly without the uninstalled PHP version.

  5. Verify the uninstallation:
    php -v

    This command checks if PHP is still installed. If uninstalled successfully, the command should return an error or a message stating PHP is not found.

Final Words

This guide has covered the essential steps to install and uninstall PHP on Ubuntu. Whether you are setting up a new development environment or resolving issues by uninstalling PHP, these instructions provide a comprehensive foundation. Always remember to back up your data before making significant changes to your system. With the information provided, you should be able to manage PHP installations on your Ubuntu server confidently.

FAQ

Q1: Can I install multiple PHP versions on Ubuntu?
A: Yes, you can install multiple PHP versions on Ubuntu. You need to ensure your web server is configured to use the correct PHP version for each application. Using PHP-FPM pools and specific version directives in your web server configuration files can help manage this.

Q2: How do I switch between different PHP versions on Ubuntu?
A: To switch between different PHP versions, you can use the `update-alternatives` command. This allows you to configure the default PHP version used by the command line. Additionally, you may need to adjust your web server configuration to use the desired PHP-FPM version.

Q3: Is it necessary to use a PPA for PHP on Ubuntu?
A: While not strictly necessary, using a PPA allows you to install versions of PHP that are not available in the default Ubuntu repositories. This is particularly useful if you need to use a newer or older PHP version than what is provided by your Ubuntu release.

Q4: How do I secure my PHP installation on Ubuntu?
A: To secure your PHP installation, follow these best practices:

  • Keep PHP updated to the latest stable version to receive security patches.
  • Disable unnecessary PHP functions in your `php.ini` file.
  • Use appropriate file permissions for your PHP scripts.
  • Regularly audit and review your PHP code for security vulnerabilities.


This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked