How to Install PHP on a Linux Cloud Server

For some, it is important to know how to install PHP as part of the LAMP Stack along with the Linux operating system family, Apache web server, and MySQL database management system. It’s also part of the LEMP Stack which replaces Apache with NGINX. Many of the most popular content management systems (CMSs) depend on an up-to-date PHP version:

SSH into your Linux server, then read on to learn:

If you don’t need cPanel, don't pay for it. Only pay for what you need with our Cloud VPS solutions

check markCentOS, Debian, or Ubuntu    check markNo bloatware    check markSSH Key management made easy

How to Install PHP on CentOS 8

  1. Enable the Extra Packages for Enterprise Linux (EPEL) repository.
    CentOS 7:
    sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

    CentOS 8:
    sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
  2. Install the Remi repo for PHP versions:
    CentOS 7:
    sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm

    CentOS 8:
    sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm
  3. View available PHP streams:
    sudo dnf module list php
  4. Enable the needed PHP module. To enable PHP 8, for example:
    sudo dnf module enable php:remi-8.0 -y
  5. You can use the following command to search for any other important PHP modules, replacing “package” with your search query:
    sudo dnf search php-package
  6. Install any needed packages, again replacing “package” with the actual package name:
    sudo dnf install php-package
  7. Run the following command to install PHP on Apache.
    sudo dnf install php php-cli php-common

How to Install PHP on Debian 10

  1. Update your repos:
    sudo apt update
  2. Install the required dependencies first:
    sudo apt -y install lsb-release apt-transport-https ca-certificates
  3. Add the DEB.SURY.ORG GPG key:
    sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
  4. Add their repo for PHP on Debian:
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list
  5. Update your repos again:
    sudo apt update
  6. Install your needed PHP version(s).
    PHP 7.4:
    sudo apt -y install php7.4

    PHP 8.0:
    sudo apt install php8.0

    If prompted, press Y, then Enter to confirm the installation.
  7. View your active PHP version:
    php -v

How to Install PHP on Ubuntu 18.04

  1. Update your repos:
    sudo apt-get update
  2. Ensure the following repo is installed:
    sudo apt -y install software-properties-common
  3. Add the PHP Personal Package Archive (PPA) by Ondrej:
    sudo add-apt-repository ppa:ondrej/php

    Press Enter to confirm adding the PPA to your system.
  4. Update your repos:
    sudo apt-get update
  5. Install your needed PHP version(s).
    PHP 7.4:
    sudo apt install -y php7.4

    PHP 7.3:
    sudo apt install -y php7.3

    PHP 8.0:
    sudo apt install php8.0 libapache2-mod-php8.0
  6. (For PHP 8.0) Restart Apache:
    sudo systemctl restart apache2
  7. View your active PHP version:
    php -v
  8. You can use the following command to search for any other important PHP modules, replacing “7” and “package” with your search query:
    sudo apt-cache search php7.* | grep package
  9. Install any needed packages, again replacing “7.4” and “package” with the package name:
    sudo apt install php7.4-package

How to Change Your PHP Version

  1. Specify the default PHP version, if you installed more than one PHP version:
    sudo update-alternatives --config php
  2. Type the selection number on the left that corresponds to your preferred PHP version. Then press Enter.
    There are 2 choices for the alternative php (providing /usr/bin/php).
      Selection    Path             Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/php7.4   74        auto mode
      1            /usr/bin/php7.3   73        manual mode
      2            /usr/bin/php7.4   74        manual mode

How to Configure PHP-FPM on NGINX

The steps below cover how to install PHP-FastCGI Process Manager (PHP-FPM) alongside the NGINX reverse proxy server.

Running Ansible for IT automation? Configure PHP-FPM and NGINX on your remote systems quickly with our Ansible roles.

  1. Stop the Apache service:
    sudo systemctl disable --now apache2
  2. Install PHP-FPM for your OS and PHP version(s).

    CentOS:
    sudo dnf install php php-cli php-common php-fpm

    Debian/Ubuntu (replace the version if necessary):
    sudo apt-get install php8.0-fpm
  3. If you receive any notices to enable important modules after the installation completes, run the commands as necessary.
  4. You can make changes in the PHP-FPM configuration file using your preferred text editor – nano, Emacs, vim, etc. CentOS:
    nano /etc/php-fpm.d/www.conf

    Debian/Ubuntu:
    nano /etc/php/7.4/fpm/pool.d/www.conf

To see all of your PHP settings easily after you learn how to install PHP, create a phpinfo.php file. This could help you troubleshoot PHP related issues from your web browser.

Create a PHPinfo.php file after learning how to install PHP

Learn how to get the most out of your Linux Cloud Server Hosting.

J
Jacqueem Technical Writer

Technical writer focused on cybersecurity and musicianship.

More Articles by Jacqueem

Was this article helpful? Let us know!