Skip to content

Instantly share code, notes, and snippets.

@Kyngo
Created April 11, 2022 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kyngo/96f4f9ae48e98fa6d167f4d954f1eab0 to your computer and use it in GitHub Desktop.
Save Kyngo/96f4f9ae48e98fa6d167f4d954f1eab0 to your computer and use it in GitHub Desktop.
Quickly change your PHP version on Ubuntu
#!/bin/bash
if [ $# -ne 1 ]; then
echo "params: [version] (8.0, 5.6, 7.4 ...)"
exit 1
fi
COMMAND_EXISTS=`which php$1`
if [ $? -eq 0 ]; then
# editing nginx vhosts
NGINX_EXISTS=`which nginx`
if [ $? -eq 0 ]; then
for idx in `ls /etc/nginx/sites-available`; do
echo "editing file /etc/nginx/sites-available/$idx ..."
sudo sed -i -e "s/[0-9]\.[0-9]-fpm/$1-fpm/g" /etc/nginx/sites-available/$idx
done
sudo systemctl restart nginx
echo "updated nginx"
fi
# editing apache2
APACHE_EXISTS=`which apache2`
if [ $? -eq 0 ]; then
cd /etc/apache2/mods-enabled
APACHE_PHP_MODULE=`ls php*.conf | cut -f1,2 -d.`
cd - >/dev/null
if [ "$APACHE_PHP_MODULE" != "" ]; then
sudo a2dismod $APACHE_PHP_MODULE >/dev/null
fi
sudo a2enmod php$1 >/dev/null
if [ $? -ne 0 ]; then
echo "Failed to update the PHP version for Apache2!"
exit 1
fi
sudo systemctl restart apache2
echo "updated apache2"
fi
# editing cli
sudo update-alternatives --set php $COMMAND_EXISTS
echo "updated cli"
php -v
else
echo "php version does not seem to be installed :("
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment