Skip to content

Instantly share code, notes, and snippets.

@watain666
Last active October 19, 2018 03:11
Show Gist options
  • Save watain666/43482f6599d74fcffd34ed711e85e0f6 to your computer and use it in GitHub Desktop.
Save watain666/43482f6599d74fcffd34ed711e85e0f6 to your computer and use it in GitHub Desktop.
Using PHPBrew to switch php version for apache
#!/usr/bin/env bash
# Usage: bash phpbrewswitch
i=1
c=1
options=()
while [ $c -gt 0 ]
do
v=$(phpbrew list | sed -n "${i}p")
if [ -z "$v" ]; then
c=0
elif [ -n "$v" ]; then
options+=("$v")
fi
i=$[$i+1]
done
count=0
echo ''
echo 'Available versions:'
for i in "${options[@]}"
do
echo "$[$count+1] $i"
count=$[$count+1]
done
echo ''
while true; do
echo 'Which version should be enabled?'
echo 'Just type a number then press enter.'
read selected
if (( $selected > $count )); then
echo "${selected} isn't on the list!"
echo "please type before php version number"
echo ''
else
break
fi;
done
chosen="${options[$selected - 1]}"
chosen="$(echo -e "${chosen}" | tr -d '[:space:]')"
chosen="$(echo -e "${chosen}" | sed 's/\*//g')"
chosen="$(echo -e "${chosen}" | sed 's/php-//g')"
echo -e "\nphp-${chosen} to be enabled."
echo 'Need sudo permission for a2dismod/a2enmod/systemctl'
SOFILE=/usr/lib/apache2/modules/libphp${chosen}.so
CONFFILE5=/etc/apache2/mods-available/php5.load
CONFFILE7=/etc/apache2/mods-available/php7.load
source ~/.phpbrew/bashrc
if [ -f $SOFILE ]; then
phpbrew switch php-${chosen}
phpbrew list
else
echo "${SOFILE} isn't a file"
fi;
if [[ $chosen =~ ^5 ]]; then
FILECONTENTS="LoadModule php5_module $SOFILE"
CONFFILE=$CONFFILE5
sudo a2dismod php7 -q
sudo a2enmod php5 -q
elif [[ $chosen =~ ^7 ]]; then
FILECONTENTS="LoadModule php7_module $SOFILE"
CONFFILE=$CONFFILE7
sudo a2dismod php5 -q
sudo a2enmod php7 -q
else
echo 'This script only works on php 5 and 7';
exit
fi;
echo $FILECONTENTS > $CONFFILE
echo "AddType application/x-httpd-php .php" >> $CONFFILE
echo "Updated $CONFFILE"
sudo systemctl restart apache2
if [[ $? -eq 0 ]]; then
echo 'Restart apache2 success!'
else
echo 'Restart apache2 failure!'
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment