Skip to content

Instantly share code, notes, and snippets.

@nd3w
Last active January 22, 2026 00:59
Show Gist options
  • Select an option

  • Save nd3w/ff2b06464121d1aabec7287f37f00d1a to your computer and use it in GitHub Desktop.

Select an option

Save nd3w/ff2b06464121d1aabec7287f37f00d1a to your computer and use it in GitHub Desktop.
Linux Aliases to Switch Between PHP Versions in Apache & Nginx

Assuming I have three different PHP versions on my system:

  1. PHP 5.6
  2. PHP 7.1
  3. PHP 7.2

Apache

To switch two between versions, add these aliases into your ~/.bashrc file:

alias php5671='sudo a2dismod php5.6 ; sudo a2enmod php7.1 ; sudo service apache2 restart ; echo 2 | sudo update-alternatives --config php'
alias php5672='sudo a2dismod php5.6 ; sudo a2enmod php7.2 ; sudo service apache2 restart ; echo 3 | sudo update-alternatives --config php'
alias php7156='sudo a2dismod php7.1 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; echo 1 | sudo update-alternatives --config php'
alias php7172='sudo a2dismod php7.1 ; sudo a2enmod php7.2 ; sudo service apache2 restart ; echo 3 | sudo update-alternatives --config php'
alias php7256='sudo a2dismod php7.2 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; echo 1 | sudo update-alternatives --config php'
alias php7271='sudo a2dismod php7.2 ; sudo a2enmod php7.1 ; sudo service apache2 restart ; echo 2 | sudo update-alternatives --config php'

To activate the alias, type command:

source ~/.bashrc

And you're on.

Type 'php5672' in terminal to switch between PHP 5.6 to PHP 7.2.

nginx

alias php8483='sudo update-alternatives --set php /usr/bin/php8.3 && sudo update-alternatives --set phar /usr/bin/phar8.3 && sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.3 && sudo systemctl stop php8.4-fpm && sudo sed -i "s/php8.4-fpm.sock/php8.3-fpm.sock/g" /etc/nginx/sites-enabled/* && sudo systemctl restart nginx php8.3-fpm ; echo 4 | sudo update-alternatives --config php'
alias php8384='sudo update-alternatives --set php /usr/bin/php8.4 && sudo update-alternatives --set phar /usr/bin/phar8.4 && sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.4 && sudo systemctl stop php8.3-fpm && sudo sed -i "s/php8.3-fpm.sock/php8.4-fpm.sock/g" /etc/nginx/sites-enabled/* && sudo systemctl restart nginx php8.4-fpm ; echo 5 | sudo update-alternatives --config php'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment