60

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

3
  • No, but there are tools you can use to parse and modify the config (eg awk).
    – jordanm
    Commented Feb 26, 2013 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 Commented Feb 26, 2013 at 6:03
  • @RahulPatil I just comment extension=x.so to disable x. Commented Feb 26, 2013 at 6:42

8 Answers 8

30

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

3
  • 7
    Note this works by adding/removing links in /etc/php5/{apache2,cli,...}/conf.d/ Commented Jan 8, 2016 at 14:23
  • 1
    @Pierre-OlivierVares This comment was the really relevant information here.
    – Xatenev
    Commented Jan 22, 2017 at 16:25
  • phpenmod is only available for root users, because it's located in /usr/sbin
    – stollr
    Commented Jun 2, 2020 at 10:04
21

You can enable an extension from the command line using:

php -d extension=/path/to/extension.so

-d is used to pass ini values via the command line. Unfortunately there is no way to disable an extension on the command line if it has been configured in the php.ini file. (You can follow the other answers of course but there is nothing you can do using -d or whatever option of the php command.)

12

On Lubuntu I needed pdo_sqlite.

Enable manually:

$ sudo php5enmod pdo_sqlite

If doesn't work check:

$ ls /etc/php5/mods-available

The result list was missing pdo_sqlite.ini. We have to install it.

$ sudo apt-get install php5-sqlite

Or for php7:

$ sudo apt-get install php7-sqlite3

Extension sqlite3 is auto-enabled in CLI and in Apache during installation process, and now we have mods-available: pdo_sqlite.ini, sqlite3.ini.

Disable extension with:

$ sudo php5dismod pdo_sqlite
1
  • 1
    You can also use straight phpenmod and phpdismod without version number. After this you may need to restart apache server
    – Ahmed Ali
    Commented Oct 3, 2020 at 8:32
7

You have to use -n and then to append each needed extension using -dextension

Example:

php -n -dextension=json.so -dextension=phar.so composer.phar update
1
  • the order of the argument is not relevant. you can also do php -d extension=json.so -d extension=phar.so -n composer.phar update Or mix -d around -n. I tested this while I had to hack openssl.so back into pecl command via PHP_PEAR_PHP_BIN. Commented Apr 22, 2020 at 15:04
5

You can specify -n to avoid any extensions loading from php.ini. This can improve some performance when you're using some (e.g. XDebug). E.g.

php -n -r "phpinfo();"
2
  • 1
    Any way to prevent loading an extension (like memcached) from loading, on a hosted server, although it is set up initially? I don't control my host setup, just the scripts on it. I'm suspecting it is somehow messing up with my sessions and would like to make sure it is off since I don't really use it..
    – userfuser
    Commented Mar 21, 2017 at 14:28
  • 1
    Note, this blocks loading ALL settings from php.ini
    – Otheus
    Commented Dec 5, 2021 at 18:46
4

usage: phpenmod [ -v ALL|php_version ] [ -s ALL|sapi_name ] module_name [ module_name_2 ]

So use phpenmod -s cli yourextension

2
  • This command is used in newer Debian versions, like Stretch, where PHP5/PHP7 can be run side-by-side.
    – Windigo
    Commented Dec 8, 2018 at 7:55
  • phpenmod is usually only available for root users, because it's located in /usr/sbin
    – stollr
    Commented Jun 2, 2020 at 10:06
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 $*
2
  • 2
    Extension could also be added in conf.d/*.ini files. Maybe some modification needs to be done ?
    – GHugo
    Commented Mar 28, 2013 at 11:21
  • Yes, we need to do changes for the same, have you tried anything ? Commented Mar 28, 2013 at 19:11
0

Please Check this

All the installed PHP modules configuration files are available under /etc/php/PHP_VERSION/mods-available directory

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .