Last active
January 10, 2018 20:45
-
-
Save maxime-rainville/fe8782de41ce5835b0d58b180b5749c4 to your computer and use it in GitHub Desktop.
This is a simple convenience script to switch between PHP 5 and PHP 7 on UBUNTU both for the CLI and APACHE.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
############################################################################### | |
# This is a simple convenience script to switch between PHP 5 and PHP 7 on | |
# UBUNTU both for the CLI and APACHE. | |
# | |
# Just call the script with the first argument set to `apache` or `cli`. The | |
# script will try to dectect if you are currently using PHP 5.6 or PHP 7.1 | |
# and switch to the other version. | |
# | |
# This script assumes you installed PHP5.6 and PHP7.1 in parallel using the | |
# method describe in | |
# https://askubuntu.com/questions/761713/how-can-i-downgrade-from-php-7-to-php-5-6-on-ubuntu-16-04 | |
# | |
# DO NOT USE IN PRODUCTION ENVIRONMENT. | |
# | |
# USE AT YOUR OWN RISK. IF IT BLOWS IN YOUR FACE, IT AIN'T MY FAULT! | |
# | |
############################################################################### | |
# PHP7 MINOR version to use | |
PHPSEVENMINOR=1 | |
# Make sure the user has root | |
if [ "$EUID" -ne 0 ]; then | |
echo "Please run as root" >&2 | |
exit 10; | |
fi | |
# The use wants to update the PHP version for APACHE. | |
if [ $1 = "apache" ]; then | |
apacheModPath=/etc/apache2/mods-enabled | |
if [ -f $apacheModPath/php5.6.conf ]; then | |
a2dismod php5.6 >/dev/null | |
a2enmod php7.$PHPSEVENMINOR >/dev/null | |
service apache2 restart >/dev/null | |
echo APACHE now using PHP 7 | |
elif [ -f $apacheModPath/php7.$PHPSEVENMINOR.conf ]; then | |
a2dismod php7.$PHPSEVENMINOR >/dev/null | |
a2enmod php5.6 >/dev/null | |
service apache2 restart >/dev/null | |
echo APACHE now using PHP 5 | |
else | |
echo "Unknown version of PHP" >&2 | |
exit 10 | |
fi | |
exit | |
fi | |
# The user wants to update the PHP version for the CLI | |
if [ $1 = "cli" ]; then | |
PHPVERSION=$(php -v|grep --only-matching --perl-regexp "((5\.6)|(7\.$PHPSEVENMINOR))"|head -1) | |
if [ $PHPVERSION = "5.6" ]; then | |
update-alternatives --set php /usr/bin/php7.$PHPSEVENMINOR >/dev/null | |
echo CLI now using PHP 7 | |
elif [ $PHPVERSION = "7.$PHPSEVENMINOR" ]; then | |
update-alternatives --set php /usr/bin/php5.6 >/dev/null | |
echo CLI now using PHP 5 | |
else | |
echo "Unknown version of PHP" >&2 | |
exit 10 | |
fi | |
exit | |
fi | |
# The user didn't provide the first parameter or it was non-sense. | |
echo "First parameter must be provided and must be either \`apache\` or \`cli\`" >&2 | |
exit 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment