Sunday, August 21, 2011

Getting PHP to talk Perl

PHP IS a great language to enable dynamic webpages. Perl is awesome when it comes to string parsing. Getting the two to work together, one can accomplish miracles!

I tried to get this going on my home server and immediately ran into a few issues. Looks like the folks working on these technologies like to ensure job security by intentionally breaking things, and not making them straightforward (well, usually they are, but very often one needs to roll up their sleeves and hack things together to get things going. Such is the way of the Linux / open-source world).

So here i document the hoops I needed to jump through to have this work. Hopefully, this will help others out there (and someday myself if I need to repeat this).

1. Install apache. Since I was running Backuppc, this was already done and running for me. For Ubuntu 10.04 this can also be done with apt-get or Synaptic package manager. In my installation, apache serves pages from /var/www
2. Install php5. This can be done from Synaptic
3. Install php-pear
4. Install php5-dev. This is needed for phpize command below:

sudo apt-get install php5-dev

5. Restart apache

sudo /etc/init.d/apache2 restart

6. Check to see if php is installed and working correctly with Apache. I created the following simple test files

index.html

<html>

<body>
this is a test. Click the following links for various test files and
scripts

<table>
<tr> <td><a href="./phpinfo.php"> phpinfo </a></td>
<td> php test</td>
</tr>
<tr>
<td><a href="./test1.php"> perl test </a></td>
<td> calling perl from php </td>
</tr>
</table>


</body>

</html>




phpinfo.php

<?php

phpinfo();
?>




test1.php

<?php


print "Hello from PHP! ";
$perl = new Perl();
$perl->require("./test1.pl");
print "Bye! ";

?>




test1.pl

print "Hello from perl!<br><br>";





At this point, php was working for me, but obviously the Perl integration wasn't (since PHP didn't know about any Perl module / extensions). I also had Perl already installed on my machine. If not, this can be done through Synaptic or apt-get.

According to several websites, the easy way to download and install Perl extensions doesn't work (see PECL bug website in required reading below). So here is what I had to do to get this to work for me.

1. download and extract latest Perl module extension from PECL.

# cd ~/temp/php-extension-perl
# wget http://pecl.php.net/get/perl-1.0.0.tgz
# wget http://pecl.php.net/get/perl-1.0.0.tgz
# tar zxf perl-1.0.0.tgz
# cd perl-1.0.0

2. Next steps / building and installation require root privileges (can actually be done as non-root, preferred and then copy over the .so, but i just did them as root for convenience)


# sudo su
# phpize
# ./configure --with-perl=$(which perl) --with-php-config=$(which php-config)
# make


At this point, I got compilation issues with PHP_VERSION not being defined. Some links in the required reading section below called for mangling a header file and adding a #define PHP_VERSION 0. Instead, I simply modified the makefile. Changes are shown by the diff below


# diff Makefile Makefile.orig

51c51
< DEFS = -DPHP_ATOM_INC -I$(top_builddir)/include -I$(top_builddir)/main -I$(top_srcdir) -DPHP_PERL_VERSION=0
---
> DEFS = -DPHP_ATOM_INC -I$(top_builddir)/include -I$(top_builddir)/main -I$(top_srcdir)



Right after this, I got linker issues because it couldn't find the perl library. The issue is that Perl advertises the Perl library as "libperl.so", while the Perl 5.3 installation creates no such file / link. This causes the linker error (See required reading section for "-lperl nonsense"). I fixed this by adding a link in the right location. By doing the following:


# find /usr/lib -iname libperl* -print
# pushd /usr/lib
# ln -s libperl.so.5.10.1 libperl.so
# popd
# make
# make install


make install above did something funky and tried to copy the perl.so library to within the temp folder. I don't understand why it did this, but I definitely didn't want to point apache or perl or any other system thing to a temp directory. So I copied this over manually (perhaps there's a better way to use configure and have this done, but I didn't have the time or the patience to do so.)


# pushd /etc/php5/apache2
# mkdir modules
# cp ~/temp/php-perl-extension/perl-1.0.0/modules/perl.so ./modules


Edit php.ini under Apache (/etc/php5/apache2/php.ini) to have it recognize and load the Perl module. Changes are shown by the diff below (ALWAYS backup the original file before changing it!).


# diff php.ini php.ini.2011-08-21-2305
807,809c807
<
< extension_dir = "./modules"
<
---
> ; extension_dir = "./"
944,945d941
< extension=/etc/php5/apache2/modules/perl.so
<



Next, restart apache

sudo /etc/init.d/apache2 restart


At this point, I was good to go. Clicking on my phpinfo test links gave me the following:

perl

Perl supportenabled
Extension version no value
Revision $Revision$
Perl version 118.53.46.49.48.46.49

And clicking on the test perl link gave me:


Hello from PHP! Hello from perl!

Bye!


Source links and recommended reading:

Using Perl code from PHP

How to compile php 5.3x to support extensions

How to integrate Perl with PHP 5.3 on opensuse 10.2

Cannot find -lperl nonsense

Integrating Perl and PHP

PECL: None of the possible methods of installation work

PECL: Perl package

Apache httpd




No comments: