JSON Functions Installation in PHP 5.1.x on CentOS 5.4

A project I work on recently needed to access the JSON functions within PHP. The problem was that the server was running PHP 5.1.x at the time, and the functions are not installed by default. What to do?

YUM to the rescue (again)!

Simply login as root (su -) on the server. Once logged in, run the following:

yum install php-devel php-pear gcc

If this is already installed, you’re fine. Proceed with the following:

pear install pecl/json
cd /etc/php.d
echo
"extension=json.so" > json.ini service httpd restart

Once you do this, you can then access the json_* functions in PHP!

NOTE: Recently on a new server install running SELinux, the library failed to load using this install. I was getting:

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/modules/json.so' - /usr/lib/php/modules/json.so: cannot open shared object file: Permission denied in Unknown on line 0

After scratching my head, I found out that it was a SELinux security issue, as when I ran:

setenforce permissive

The error disappeared. The fix was to run the following as root:

chcon system_u:object_r:textrel_shlib_t /usr/lib/php/modules/json.so

Then I restarted Apache, and all was well. The JSON values then showed up under phpinfo().

Comments

comments

Posted in PHP Tagged with: , ,