Skip to content

Safely migrate Cpanel from DSO to SuPHP

    Cpanel has the ability to change between loader technologies. For a shared hosting environment which hosts CMS s like Joomla, DSO mood will not be 100% success when considering nasty permission problems coming from the default httpd user. As a solution SuPHP provides better PHP execution method by running PHP files using the original user which owns that shared host. In Cpanel, the first time when migrating from DSO to SuPHP or any other CGI-based loader, it has no way to ensure file permissions and settings are in order to use the CGI-based loader environment. Since CGI-based loaders not has the ability to parse PHP settings in .htaccess files, and it will throw and 500 Internal Server error when these are occurred. In particular, SuPHP runs with a set of strict file permission requirements for files/directories. Unless it’ll also throw an 500 error, which sometimes leads to an annoying situation for host-masters. This automated script will fix the ownerships of the cpanel shared hosting server and at the same time it’ll fix the file permission issues of all the user directories. Run the script using root. 🙂

     

    USERFILE=/etc/domainusers
     
    if [ ! -e $USERFILE ]
    then
     echo "!! Looks like you're trying to run this script on a non-cPanel server. Better luck next time. !!"
     exit 1
    fi
     
    DEFPHP=( `/usr/local/cpanel/bin/rebuild_phpconf --current|grep -E "PHP.* SAPI:"|awk -F':' {'print $2'}` )
     
    if  echo ${DEFPHP[@]} | grep 'suphp' 1>/dev/null
    then
     echo "!! SuPHP Detected !!"
    else
     echo "!! SuPHP is not yet enabled or compiled in. Please check your Apache+PHP settings."
     exit 1
    fi
     
    USERS=( `cat $USERFILE|awk -F':' {'print $1'}` )
     
    for I in "${USERS[@]}"
    do
       USERDIR=/home/$I/public_html
            cd $USERDIR
       echo "Fixing $I..."
            echo -e "tEnsuring proper file ownership"
            find -type f ! -perm 644 -exec chmod 0644 {} ;
            echo -e "tEnsuring proper directory ownership"
            find -type d ! -perm 755 -exec chmod 0755 {} ;
     
       # Skip this junk if they don't even have it!
       USERHTA=$USERDIR/.htaccess
       if [ ! -e $USERHTA ]
       then
        continue
       fi
     
       # Grab the existing PHP settings from .htaccess (ugly but it works!)
       PARAM=( `grep '^php_value|^php_admin_value|^php_flag|^php_admin_flag' $USERHTA 2>/dev/null|awk '{print $2}'` )
            VAL=( `grep '^php_value|^php_admin_value|^php_flag|^php_admin_flag' $USERHTA 2>/dev/null|awk '{print $3}'` )
     
       # Comment out all php_* settings
       sed -i 's/^php_/#php_/' $USERHTA
     
            if [ ${#PARAM[@]} -gt 0 ]
            then
                    FILE=$USERDIR/php.ini
                    cat /usr/local/lib/php.ini > $FILE
          chown $I:$I $FILE
     
       # Updates each value from .htaccess in new user-level php.ini
            C=0
            for J in ${PARAM[@]}
            do
                    V=${VAL[$C]}
          echo -e "tUpdating $J to $V in php.ini"
                    sed -i 's/^('$J') = .*$/1 = '$V'/i' $FILE    # Turning quoting on/off prevents problems with double quotes in $V
                    C=$C+1
            done
            fi
    done
    echo "Fixing public_html ownership..."
    /scripts/chownpublichtmls
    echo "All Done!"
    

     


    Last modified on Thursday, 15 March 2012 23:37

    Author

    Leave a Reply

    Your email address will not be published. Required fields are marked *