Skip to content

Instantly share code, notes, and snippets.

@riandoza
Last active August 21, 2024 20:31
Show Gist options
  • Save riandoza/2184487a41f348d56889c11cc722b32e to your computer and use it in GitHub Desktop.
Save riandoza/2184487a41f348d56889c11cc722b32e to your computer and use it in GitHub Desktop.
Installing multiple PHP Versions [Ubuntu 22.04 LTS]

Multiple PHP Versions

For Ubuntu LTS Versions

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

For Debian

sudo dpkg -l | grep php | tee packages.txt
sudo apt install apt-transport-https lsb-release ca-certificates wget -y
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg 
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update

Install PHP 5.6

sudo apt install php5.6 php5.6-fpm
sudo apt install php5.6-{curl,gd,intl,mbstring,soap,xml,xmlrpc,zip,mysql,xdebug}

# Config path : /etc/php/5.6/fpm/pool.d/www.conf
# Listen url : /run/php/php5.6-fpm.sock

Install PHP 7.4

sudo apt install php7.4 php7.4-fpm
sudo apt install php7.4-{curl,gd,intl,mbstring,soap,xml,xmlrpc,zip,mysql,xdebug}

# Config path : /etc/php/7.4/fpm/pool.d/www.conf
# Listen url : /run/php/php7.4-fpm.sock

Install PHP 8.1

sudo apt install php8.1 php8.1-fpm
sudo apt install php8.1-{curl,gd,intl,mbstring,soap,xml,xmlrpc,zip,mysql,xdebug}

# Config path : /etc/php/8.1/fpm/pool.d/www.conf
# Listen url : /run/php/php8.1-fpm.sock

How to use

If you want check active PHP version

php -v

Switch the Default PHP Version for CLI

sudo update-alternatives --config php

Add alias for CLI shortcut (BASH or ZSH)

[for BASH]
echo -n 'alias version-php="update-alternatives --config php"' >> ~/.bashrc
source ~/.bashrc

[for ZSH]
echo -n 'alias version-php="update-alternatives --config php"' >> ~/.zshrc
source ~/.zshrc

Run this command without sudo

version-php

Set multiple PHP version inside Nginx, use listen URL

Composer for php7.4

$ sudo apt remove composer
then use this command to install composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment