Thursday, January 9, 2014

Script Change PHP Versions

Script Change PHP Versions


I am lazy to edit the vhost file to change the PHP version that I usually change,
So I create this script to help me complete this task. :D

This is the script to change PHP version which is base on where you keep the path of your PHP.
Download this script file and put somewhere on you computer.

*** Adjust this script base on your custom configurations.

How to run the script:
sudo ./change-php-version.sh
You will get the options to choose:

Please select the options, Enter the number only.
1. For list virtualhost
2. For list PHP version
3. For Change PHP version
4. For Exit

Note: If you choose option number 3 you need to provide other informations in order to change PHP version such as:
* virtualhost-name
* PHP version

As the following example:

Please Enter virtualhost file:
(example: local.demo)
local.demo

Please Enter PHP version number only:
(example: 5.3.8)
 5.4.9
If the virtualhost file exist and with PHP version is correct, Script will run without error.

Changing PHP Version to 5.4.9
 * Reloading web server config                                                              [ OK ]
Virtualhost local.demo changed to PHP version 5.4.9

Done, In this example, The script will change vhost file local.demo to PHP version 5.4.9.

==================== Here is the Script Content ================
#!/bin/bash
# Version 1.0
# Require user "root" to run this script.
if [ "$(whoami)" != 'root' ]; then
        echo "You have no permission to run $0 as non-root user."
        echo "Log in as user '"root"' or Use '"sudo"' !!!"
        exit 1;
fi
# Available options
echo ""
echo "Please select the options, Enter the number only."
echo ""
echo "1. For list virtualhost"
echo "2. For list PHP version"
echo "3. For Change PHP version"
echo "4. For Exit"
echo ""
read USERINPUT

USAGE="Pleaes enter the valid option"

if [ $USERINPUT = "1" ]
then
  CONFIG_OPTION="1"
fi

if [ $USERINPUT = "2" ]
then
  CONFIG_OPTION="2"
fi

if [ $USERINPUT = "3" ]
then
  CONFIG_OPTION="3"
fi

if [ $USERINPUT = "4" ]
then
  CONFIG_OPTION="4"
fi

if [ "$CONFIG_OPTION" = "" ]
then
  echo "$USAGE"
echo ""
  exit 1
fi

if [ "$USERINPUT" = "1" ]
then
echo "Available virtualhost files..."
echo ""
        if [ "$USERINPUT" = "1" ];
sudo ls /etc/apache2/sites-enabled/ >> /opt/php-versions/vhost.tmp ; cat /opt/php-versions/vhost.tmp ; rm /opt/php-versions/vhost.tmp
        then
echo ""
        else
           echo "$USAGE"
echo ""
        exit 1
fi
fi

if [ "$USERINPUT" = "2" ]
then
echo "Available PHP versions..."
echo ""
        if [ "$USERINPUT" = "1" ];
sudo ls /opt/php-versions/ |grep php5 >> /opt/php-versions/php-version.tmp ; cat /opt/php-versions/php-version.tmp ; rm /opt/php-versions/php-version.tmp
        then
echo ""
        else
           echo "$USAGE"
echo ""
        exit 1
        fi
fi

if [ "$USERINPUT" = "4" ]; then
        echo "Exit the programm"
        exit 1;
fi

################################################################
if [ "$USERINPUT" = "3" ]
then
echo ""
echo "Please Enter virtualhost file:"
echo ""
echo "(example: local.demo)"
echo ""
read VHOST
echo ""
echo "Checking if file virtualhost exist or not.."
echo ""

        if [ -f /etc/apache2/sites-available/$VHOST ];
        then
           echo "File virtualhost $VHOST exist..."
        else
           echo "File virtualhost $VHOST does not exists..."
        exit 1
fi
echo ""
echo "Please Enter PHP version number only:"
echo ""
echo "(example: 5.3.8) "
echo ""
read PHPVERSION
echo ""

if [ $PHPVERSION = "5.3.8" ]
then
  PHPPATH="/opt/php-versions/php538/"
fi

if [ $PHPVERSION = "5.4.9" ]
then
  PHPPATH="/opt/php-versions/php549/"
fi

if [ $PHPVERSION = "5.4.13" ]
then
  PHPPATH="/opt/php-versions/php5413/"
fi

if [ $PHPVERSION = "5.5.0" ]
then
  PHPPATH="/opt/php-versions/php550/"
 fi

if [ "$PHPPATH" = "" ]
then
  echo "This PHP Version does not exist. or not included. "
  exit 1
fi

#########################
# If $PHPVERSION = 5.3.8#
#########################
if [ $PHPVERSION = "5.3.8" ]
then
echo "Changing PHP Version to "$PHPVERSION""
# Remove "#" before Alias if more then 4 keep only 1.
sudo sed -i -e 's/#####Alias/#Alias/g' /etc/apache2/sites-available/$VHOST
# Comment all the line that exist the word "php5-default".
sudo sed -i -e 's/\/#Alias/' /etc/apache2/sites-available/$VHOST
# Uncomment the line which contain the PHP version that we want to enable
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php538\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php538\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php538\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php538\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php538\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php538\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php538\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php538\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php538\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php538\//g' /etc/apache2/sites-available/$VHOST
#service apache2 restart
sudo service apache2 reload
fi
#########################
# If $PHPVERSION = 5.4.9#
#########################
if [ $PHPVERSION = "5.4.9" ]
then
echo "Changing PHP Version to "$PHPVERSION""
# Remove "#" before Alias if more then 4 keep only 1.
sudo sed -i -e 's/#####Alias/#Alias/g' /etc/apache2/sites-available/$VHOST
# Comment all the line that exist the word "php5-default".
sudo sed -i -e 's/\/#Alias/' /etc/apache2/sites-available/$VHOST
# Uncomment the line which contain the PHP version that we want to enable
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default\//g' /etc/apache2/sites-available/$VHOST
#service apache2 restart
sudo service apache2 reload
fi
##########################
# If $PHPVERSION = 5.4.13#
##########################
if [ $PHPVERSION = "5.4.13" ]
then
echo "Changing PHP Version to "$PHPVERSION""
# Remove "#" before Alias if more then 4 keep only 1.
sudo sed -i -e 's/#####Alias/#Alias/g' /etc/apache2/sites-available/$VHOST
# Comment all the line that exist the word "php5-default".
sudo sed -i -e 's/\/#Alias/' /etc/apache2/sites-available/$VHOST
# Uncomment the line which contain the PHP version that we want to enable
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php5413\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php5413\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php5413\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php5413\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php5413\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php5413\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php5413\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php5413\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php5413\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php5413\//g' /etc/apache2/sites-available/$VHOST
#service apache2 restart
sudo service apache2 reload
fi
#########################
# If $PHPVERSION = 5.5.0#
#########################
if [ $PHPVERSION = "5.5.0" ]
then
echo "Changing PHP Version to "$PHPVERSION""
# Remove "#" before Alias if more then 4 keep only 1.
sudo sed -i -e 's/#####Alias/#Alias/g' /etc/apache2/sites-available/$VHOST
# Comment all the line that exist the word "php5-default".
sudo sed -i -e 's/\/#Alias/' /etc/apache2/sites-available/$VHOST
# Uncomment the line which contain the PHP version that we want to enable
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php550\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php550\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php550\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php550\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php550\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php550\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php550\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php550\//g' /etc/apache2/sites-available/$VHOST
sudo sed -i -e 's/#Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php550\//Alias \/fcgi-bin\/ \/var\/www\/cgi-bin\/php5-default_php550\//g' /etc/apache2/sites-available/$VHOST
#service apache2 restart
sudo service apache2 reload
fi

echo ""
echo "Virtualhost $VHOST changed to PHP version $PHPVERSION"
        else
echo ""
        exit 1
fi

####################### End Of Script #######################

No comments:

Post a Comment