sodcalculator

10 Tech How-To

How to Install PHP on Ubuntu 20.04 LTS

Guide 10-38805 · Updated · Step by step

This guide walks through installing PHP on Ubuntu 20.04 LTS, covering the default version, adding a PPA for newer releases, and installing common extensions. You’ll end up with a working PHP CLI and, if needed, Apache or Nginx integration.

What You’ll Need

  • Ubuntu 20.04 LTS server or desktop with sudo access
  • Internet connection
  • Terminal access (Ctrl+Alt+T on desktop, SSH for servers)

Step-by-Step

  1. Update package lists
    Open a terminal and run:
    sudo apt update
    This refreshes the package index from Ubuntu’s repositories.
  2. Install PHP (default version 7.4)
    Run:
    sudo apt install php
    This installs PHP 7.4, the default for Ubuntu 20.04, along with the CLI. Confirm with php -v.
  3. Install PHP extensions
    Most web apps need extensions. Install common ones with:
    sudo apt install php-mysql php-xml php-mbstring php-curl php-zip php-gd
    Adjust the list based on your project’s needs.
  4. Install PHP-FPM (for Nginx or Apache with mod_php)
    For Nginx, install PHP-FPM:
    sudo apt install php-fpm
    For Apache, you can use mod_php (installed automatically with the libapache2-mod-php package) or PHP-FPM with proxy modules. For simplicity, if using Apache, run:
    sudo apt install libapache2-mod-php
    Then restart Apache: sudo systemctl restart apache2.
  5. Install a specific PHP version (optional)
    To get PHP 8.x, add the Ondřej Surý PPA:
    sudo add-apt-repository ppa:ondrej/php
    sudo apt update
    Then install, e.g., PHP 8.2:
    sudo apt install php8.2 php8.2-cli php8.2-mysql
    You can have multiple versions installed and switch between them using update-alternatives.
  6. Verify the installation
    Create a test file in your web root (e.g., /var/www/html/info.php for Apache) with content <?php phpinfo(); ?>, then open it in a browser. You should see the PHP info page. For CLI, run php -v to see the version.
  7. Configure PHP (optional)
    Edit the php.ini file (location shown by php --ini) to adjust settings like memory_limit, upload_max_filesize, and date.timezone. After changes, restart PHP-FPM or Apache.

Common Mistakes

  • Forgetting to install extensions – Many apps fail with cryptic errors like “Class not found” because required extensions are missing.
  • Using the wrong PHP version – The default 7.4 may be outdated for some modern apps. Check your app’s requirements and install a newer version via the PPA.
  • Not restarting services – After installing or configuring PHP, you must restart Apache or PHP-FPM for changes to take effect.
  • Mixing mod_php and PHP-FPM – Having both can cause conflicts. Choose one method for serving PHP.

Tips

  • Use php -m to list loaded extensions and verify what you need is present.
  • For development, consider installing Xdebug for debugging: sudo apt install php-xdebug.
  • Keep PHP updated with sudo apt upgrade regularly to get security patches.
  • If you’re using Nginx, ensure the server block has location ~ \.php$ { ... } configured to pass requests to PHP-FPM.

Troubleshooting

PHP not working in browser

  • Check if Apache or Nginx is running: sudo systemctl status apache2 or nginx.
  • Verify the test file is in the correct web root and has proper permissions (644).
  • For Nginx, ensure PHP-FPM is running and the server block includes the correct fastcgi_pass (e.g., unix:/run/php/php7.4-fpm.sock).

Command not found: php

  • Check if PHP is installed: dpkg -l | grep php. If not, run sudo apt install php.
  • If you installed a specific version, the command may be php8.2 instead of php. Use update-alternatives --config php to set the default.

Extension not loading

  • Install the extension package (e.g., php-mysql for MySQL).
  • Check if the extension is enabled in php.ini (uncomment extension=mysqli if needed).
  • Restart the web server or PHP-FPM after changes.

PPA not found

  • Ensure software-properties-common is installed: sudo apt install software-properties-common.
  • If the PPA fails to add, check your internet connection and try again.

Frequently Asked Questions

What PHP version does Ubuntu 20.04 default to?

Ubuntu 20.04 ships with PHP 7.4 in its default repositories. You can install newer versions via the ondrej/php PPA.

How do I switch between PHP versions?

Use update-alternatives --config php to choose the default CLI version. For web servers, you may need to adjust the PHP-FPM socket or Apache module.

Do I need PHP-FPM if I use Apache?

No, Apache can use mod_php (via libapache2-mod-php). However, PHP-FPM is often recommended for better performance and is required for Nginx.

How do I install PHP extensions?

Use sudo apt install php-, e.g., php-mysql, php-curl. For a specific version, prefix with the version number, like php8.2-mysql.

Where is the php.ini file?

Run php --ini to see the loaded configuration file path. Typically it’s /etc/php/7.4/cli/php.ini for CLI and /etc/php/7.4/apache2/php.ini for Apache.

Can I install PHP without a web server?

Yes, installing the php package alone gives you the CLI. You can run PHP scripts from the terminal without any web server.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →