Managing PHP 5.4 Extensions on Ubuntu

My shiny new VPS* runs Ubuntu 12.10 (official subtitle: Quantal Queztal. Local nickname: Quirky Kestrel) and therefore has PHP 5.4 installed. It’s very new so every command I type is missing, and today I realised that included a PECL module (pecl_http, of course). So I aptitude install php5-pear and then get tangled in dev packages (clue: look which libcurl you have already installed to figure out which of a long list of -dev packages to choose), managing finally to emerge with a pecl install http that completes successfully with the words:

configuration option "php_ini" is not set to php.ini location
You should add "extension=http.so" to php.ini

I’ve been using Ubuntu for some time however, and we don’t put settings straight into php.ini, there’s a directory called /etc/php5/conf.d/ where all the various module configurations live, or you can enable things just for when PHP is called by apache or from the CLI. However today I hopped into /etc/php5/ and saw this:

.
├── apache2
├── cli
├── conf.d
└── mods-available

Hmmm … mods-available ?

php5enmod

What’s happened here is that all debian-flavoured unixes have adopted this standard for their PHP 5.4 packages, so if you’re using debian, ubuntu, or any of their relatives with PHP 5.4, you’ll see a directory structure like this. When you add a module to PHP, you’ll add a file to the mods-available directory enabling the module and adding any config specific to it. If you want to enable the module, just do:


php5enmod http

This simply creates a symlink from the usual conf.d directory to point to where the real files are in mods-available, prefixed with a number that indicates the priority of the module. By default, the priority is 20.

Using this approach means we can toggle things on and off without commenting out big chunks of config files and leaving them lying around – if this seems familiar then that’s no surprise; debian-like linuxes manage their apache configuration in just the same way. Any packages that you install using aptitude will use these exact same commands to set up the configuration and then symlink it correctly. To unlink, use the delightfully predictably-named php5dismod :)

This new structure was news to me, so I thought I’d make a note of it – lots of people are upgrading at the moment I know, so if that includes you, here’s a little something to help you along the way!

* The new shiny server is a Linode. If you’re signing up for a Linode and don’t have any friends with referral codes, you can use mine: http://www.linode.com/?r=8deff7b7d89fc12b559a1013a1cc2bb823b7fd08 – this blog is hosted there and it all helps!

11 thoughts on “Managing PHP 5.4 Extensions on Ubuntu

  1. Thanks for this, I have been trying to downgrade my php version from 5.4 to 5.3 on a vagrant box and a bunch of commands kept failing that referred to config files in php5/mods-available. Glad to see it’s because the dir doesn’t exist for 5.3 and not something I was doing wrong :)

  2. It appears that there has been a slight change: There is conf.d per sapi.
    [code]
    ├── cli
    ├── conf.d
    ├── fpm
    ├── conf.d
    └── mods-available
    [/code]
    Such structure make sense since it is possible to enable/disable mod/extension per sapi.
    [code]vagrant@vagrant:~$ sudo php5enmod apc
    vagrant@vagrant:~$ ls -l /etc/php5/cli/conf.d/
    total 0
    lrwxrwxrwx 1 root root 32 Aug 12 09:35 05-opcache.ini -> ../../mods-available/opcache.ini
    lrwxrwxrwx 1 root root 28 Aug 12 09:35 10-pdo.ini -> ../../mods-available/pdo.ini
    lrwxrwxrwx 1 root root 28 Aug 12 09:38 20-apc.ini -> ../../mods-available/apc.ini
    lrwxrwxrwx 1 root root 29 Aug 12 09:35 20-json.ini -> ../../mods-available/json.ini
    vagrant@vagrant:~$ ls -l /etc/php5/fpm/conf.d/
    total 0
    lrwxrwxrwx 1 root root 32 Aug 12 09:35 05-opcache.ini -> ../../mods-available/opcache.ini
    lrwxrwxrwx 1 root root 28 Aug 12 09:35 10-pdo.ini -> ../../mods-available/pdo.ini
    lrwxrwxrwx 1 root root 28 Aug 12 09:38 20-apc.ini -> ../../mods-available/apc.ini
    lrwxrwxrwx 1 root root 29 Aug 12 09:35 20-json.ini -> ../../mods-available/json.ini
    vagrant@vagrant:~$ sudo php5dismod -s cli opcache
    vagrant@vagrant:~$ ls -l /etc/php5/cli/conf.d/
    total 0
    lrwxrwxrwx 1 root root 28 Aug 12 09:35 10-pdo.ini -> ../../mods-available/pdo.ini
    lrwxrwxrwx 1 root root 28 Aug 12 09:38 20-apc.ini -> ../../mods-available/apc.ini
    lrwxrwxrwx 1 root root 29 Aug 12 09:35 20-json.ini -> ../../mods-available/json.ini
    [/code]

  3. Pingback: managing php extensions with php5enmod | Ideato's hideout

  4. Pingback: Installing and configuring Mcrypt and json for PHP 5.5 in Ubuntu | Shaun Freeman

    • vagrant@ansible-apache-mod-php-56-ubuntu-1404:~$ cat /etc/php5/mods-available/gd.ini
      ; configuration for php GD module
      ; priority=20
      extension=gd.so

      They’re comming from the (priority) comment in the original file(s)

  5. Thanks for this. but i do the way what you said and nothing usefully!!!

    In order to download IMAP C-Clients, run these commands on your terminal:
    1,apt-get install php5-imap
    2,php5enmod imap
    3,service apache2 restart

    now, i show the phpinfo(), and no imap!!!

    my php ver:5.4.38 apache:2.4.7

  6. Pingback: Enable PNCTL Extension to PHP (Ubuntu 14.04) | linuxserversite

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.