Compiling php-python on Fedora Linux

So, I haven’t posted in a while, and I felt it was about time to get some fresh content up here so what follows is the result of some research and testing I did as a result of re-visiting a question on SuperUser that I responded to over a year ago that was never fully resolved.

So, you want to run python code in PHP, eh? Why you might ask? Well, I’m not quite sure. I haven’t run in to a use case for it myself, but I can see where you might need to do some heavy-duty maths or maybe there is a python library that you require for some custom algorithm or something.

Good news! There is a PECL library you can use for this task:
Unfortunately, it hasn’t been updated in over 4 years at the time of this writing and was in alpha at the time of the last release.
However! The maintainer appears to still be working on the package, but over a GitHub:
Unfortunately, it still doesn’t compile on PHP 5.4 (in Fedora at least)
However! I got it to work with some minor tweaks.

Here is the process that works with the current PHP version (5.4.8 at the time of this writing) linking to python 2.7:

Install build tools and minimum deps:

yum groupinstall "Development Tools"
yum install php-common php-pear php-devel python python-devel wget

Get a copy of the latest version of php-python:

git clone php-python

Apply the patch to fix C syntax errors. Note that this patch is likely to be broken and/or not necessary some time in the future when php-python is updated. But the changes are simple so you can apply them manually if they are still needed in future versions. Gist also available here:

patch -p0 < php-python_20121112.diff

Build the module

cd php-python
make install

After this is complete you need to edit your php.ini to enable the new module. Add a line like this:

Once php.ini is updated you should be able to see the "python" module listed in the "php -m" modules list, and you should be able to use the python functions. Restart Apache if you are using mod_php to load the updated php.ini file. The examples folder on GitHub has some code you can use to see the Python features in action:

Good luck!