Monitor OS Default Php And Restore If Changed

Discussion in 'Tips/Tricks/Mods' started by ahrasis, Nov 21, 2019.

  1. ahrasis

    ahrasis Well-Known Member

    I found that default php for Ubuntu may be changed when updating php in multiple php environment or thus this update may caused problem for ISPConfig and other programs relying on default php. So I tried this script (only for ubuntu 18.04 and default php7.2) with monit:
    Code:
    OUTPUT=`php -v | head -n 1 | cut -d ' ' -f 2 | cut -f1-2 -d'.'`
    if [ $OUTPUT = 7.2 ]; then exit 0; fi
    exit 1
    
    Code:
    check program default-php with path /usr/share/dphp/dphp.sh
     if status != 0 then exec "/usr/bin/update-alternatives --set php /usr/bin/php7.2"
    
    It seems working for now but if you see any things that can be improved in the aboves, do reply with your suggestion.

    EDITED:
    The above implementation actually creates zombie's process, which I don't like, so I modified the code and use a much simpler approach yet cover wider OS version as follows:

    1. Create an executable bash script for checking and restoring OS default php. E.g. /usr/share/default-php/default-php.sh
    Code:
    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides: Check default OS php & restore if it was changed
    # Required-Start:  $local_fs $network
    # Required-Stop:  $local_fs
    # Default-Start:  2 3 4 5
    # Default-Stop:  0 1 6
    # Short-Description: Check default OS php & restore if it was changed
    # Description: Check default OS php & restore if it was changed
    ### END INIT INFO
    
    osName=`grep -oP '(?<=^ID=).+' /etc/os-release`
    osVersion=`grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"'`
    phpVersion=`php -v | head -n 1 | cut -d ' ' -f 2 | cut -f1-2 -d'.'`
    #IF UBUNTU
    if [ $osName == ubuntu ] && [ $osVersion == 16.04 ] && [ $phpVersion != 7.0 ]; then /usr/bin/update-alternatives --set php /usr/bin/php7.0; fi
    if [ $osName == ubuntu ] && [ $osVersion == 18.04 ] && [ $phpVersion != 7.2 ]; then /usr/bin/update-alternatives --set php /usr/bin/php7.2; fi
    if [ $osName == ubuntu ] && [ $osVersion == 20.04 ] && [ $phpVersion != 7.4 ]; then /usr/bin/update-alternatives --set php /usr/bin/php7.4; fi
    #IF DEBIAN
    if [ $osName == debian ] && [ $osVersion == 8 ] && [ $phpVersion != 5.6 ]; then /usr/bin/update-alternatives --set php /usr/bin/php5.6; fi
    if [ $osName == debian ] && [ $osVersion == 9 ] && [ $phpVersion != 7.0 ]; then /usr/bin/update-alternatives --set php /usr/bin/php7.0; fi
    if [ $osName == debian ] && [ $osVersion == 10 ] && [ $phpVersion != 7.3 ]; then /usr/bin/update-alternatives --set php /usr/bin/php7.3; fi
    
    2. Create a con job for it to run every 10 minutes (or your preferred time) E.g. /etc/cron.d/default-php
    Code:
    */10 * * * * root /usr/share/default-php/default-php.sh 2>&1 | while read line; do echo `/bin/date` "$line" >> /var/log/default-php; done
    
    This will run the script every 10 minutes and if it founds, for example, that Ubuntu 18.04 default php version has been changed, php7.2 will then be restored.

    Feel free to use, modify and/or expand the script for your personal usages.
     
    Last edited: May 25, 2020
    nhybgtvfr and till like this.
  2. ahrasis

    ahrasis Well-Known Member

    Updated opening post with a better code and a more simpler approach.
     

Share This Page