Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there any command to enable/disable a php extension easily from command line? (php.ini)

share|improve this question
    
No, but there are tools you can use to parse and modify the config (eg awk). –  jordanm Feb 26 '13 at 5:37
1  
just let us know what you configure to disable extension in php.ini so that we can make is easy from command line –  Rahul Patil Feb 26 '13 at 6:03
    
@RahulPatil I just comment extension=x.so to disable x. –  PHPst Feb 26 '13 at 6:42

2 Answers 2

You can simply use

sed -i.bkp 's/^extension=x.so/# extension=x.so/' /path/of/php.ini && /etc/init.d/httpd reload

-i.bkp take backup as php.php.bkp and write in to original file

&& if first command is success then reload httpd service.

but I just notice that sed giving exit status 0 when search patter not match so you can use

php_ini=/path/of/php.ini
__module=x.so
grep -q "^extension=$__module" $php_ini && { 
        sed -i.bkp "s/^extension=$__module/# extension=$__module/" $php_ini && 
        echo /etc/init.d/httpd reload; } || echo "cannot make requested change"

Or you can use below script for the enable and disable :

#!/bin/bash

php_ini=/path/of/php.ini
__module="$2"


[[ ! -f $php_ini ]] && { echo "Error: Can not found $php_ini" >&2; 
                         echo "Please define php.ini path in $php_ini"; 
                         exit 1; }

[[  -z $__module ]] && { echo "Error: Please Type Module Name:" >&2; 
                          exit 1; }

show_help(){
cat <<_EOF
        Usage:    To enable :
                  $0 -ie  <modulename>

                  To disable :
                  $0 -id  <modulename>

        example:
                  $0 -i xyz.so
_EOF

}

do_enable() {
grep -Eq "# extension=$__module$" $php_ini && {
sed -i.bkp "s/^# extension\=$__module$/extension=$__module/" $php_ini &&
echo /etc/init.d/httpd reload; echo "Changes Successfully Done"; } || echo "cannot make requested change"
}

do_disable() {
grep -q "^extension=$__module" $php_ini && {
sed -i.bkp "s/^extension=$__module/# extension=$__module/" $php_ini &&
echo /etc/init.d/httpd reload; echo "Changes Successfully Done"; } || echo "cannot make requested change"
}

Main() {

case $1 in
        -ie) do_enable ;;
        -id) do_disable ;;
         *) show_help ;;
esac
}

Main $*
share|improve this answer
1  
Extension could also be added in conf.d/*.ini files. Maybe some modification needs to be done ? –  GHugo Mar 28 '13 at 11:21
    
Yes, we need to do changes for the same, have you tried anything ? –  Rahul Patil Mar 28 '13 at 19:11

If you are using PHP5.4+ on Debian based distro you can use php5enmod to enable and php5dismod to disable PHP extensions.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.