To PHP specific version of PHP, we will use SURY third-party repository.

Following steps must be used to install specific version:

Install repository and dependencies

  1. Update the repository cache
sudo apt update
  1. Install dependencies
sudo apt install -y curl wget gnupg2 ca-certificates lsb-release apt-transport-https
  1. Import public keys
wget https://packages.sury.org/php/apt.gpg

sudo apt-key add apt.gpg
  1. Add SURY repository and update the system
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.list
sudo apt update

Install PHP

Install PHP with the following command:

sudo apt install -y php7.x php7.x-cli php7.x-common

Replace in this command php7.x by the version desired.

You can also have multiple PHP versions on your system. Each PHP version will have separate configuration files under /etc/php/7.X directory.

Set default PHP version

If your system have multiple PHP versions, you can set a version as the default version with the following command:

sudo update-alternatives --set php /usr/bin/php7.x

Replace in this command php7.x by the version desired.

Once you installed the PHP, check the version

php -v

the output should be:

PHP 7.4.0 (cli) (built: Nov 28 2019 20:41:26) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.0, Copyright (c), by Zend Technologies

PHP for Web Server

  1. Apache

To install apache PHP module, type the following command:

sudo apt install -y apache2 libapache2-mod-php7.x

Replace in this command php7.x by the version desired.

  1. Nginx

To use PHP with Nginx, you must install the FastCGI Process Manager (FPM) to handle PHP requests:

sudo apt install -y php7.x-fpm

Replace in this command php7.x by the version desired.

Once you have installed FastCGI manager, add socket details in Nginx virtual host.

server {

# ...

  location ~* \.php$ {
    fastcgi_pass unix:/run/php/php7.x-fpm.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  }
}

Replace in this command php7.x by the version desired.

PHP extensions

Extensions depends of PHP version. To install them, you can install the specific package. For example to install MySQL support:

sudo apt install -y php7.x-mysql

Replace in this command php7.x by the version desired.

Next Post Previous Post