Compiling php-python on Fedora Linux
Posted by Daniel | Posted in Development, Operating Systems, PHP, Software | Posted on November 12, 2012
0
<rambling>
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.
I enjoy research and testing new things that I don’t fully understand yet, to learn something new and hopefully gain a more complete knowledge of concepts and technology that I haven’t had either the time to research, or haven’t wanted to learn more about previously. I have a personal goal to constantly learning something new to keep from becoming complacent or self-assured of what I’ve learned.
</rambling>
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: http://pecl.php.net/package/python
Bad news! 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.
Good news! The maintainer appears to still be working on the package, but over a GitHub: https://github.com/jparise/php-python
Bad news! It still doesn’t compile on PHP 5.4 (in Fedora at least)
Good news! I got it to work with some minor tweaks.
Here is the process that works for me 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:
1 2 | 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:
1 | git clone https://github.com/jparise/php-python.git 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.
1 2 | wget http://www.dandev.com/other/post_content/php-python_20121112.diff patch -p0 < php-python_20121112.diff |
Build the module
1 2 3 4 5 | cd php-python phpize ./configure make make install |
After this is complete you need to edit your php.ini to enable the new module. Add a line like this:
1 | extension=python.so |
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: https://github.com/jparise/php-python/tree/master/examples
Good luck!

