Loading Dynamic libraries in php

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Cappuccinodude
Forum Newbie
Posts: 4
Joined: Tue Jul 08, 2008 2:03 pm

Loading Dynamic libraries in php

Post by Cappuccinodude »

Hi,

I'm having problems running a java code that requires certain dynamic libraries on Apache (on linux). Despite setting the LD_LIBRARY_PATH environment variable (specific to linux) to specify the path for the dynamic libraries, the error message the application gives suggests that it's not able to find the dynamic libraries required for that application despite the fact that the LD_LIBRARY_PATH variable is set. I set this variable through my php script using putenv() and it was set successfully however the error kept popping up. I then launched a bash script from my php script which in turn would launch the application after setting the LD_LIBRARY_PATH environment variable, however this didn't work either. After discussing with a few ppl, many suggested that it could be that PHP requires a few settings, may b there are things you need to check in PHP. I even included the library path in the apache's configuration files, i even have it in ld.so.conf (the basic configuration file to specify paths for dynamic libraries).

A few other ppl were able to reproduce the problem on apache linux through php but the code ran perfectly using servlets on tomcat. So there is something fishy.

The question is, in order to run applications that require shared/dynamic libraries through php on apache (on linux), are there any issues that one might encounter ? How to resolve them ?

Please note that the application runs without any problems from the command line however i'm unable to run it from the php script.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Loading Dynamic libraries in php

Post by Ollie Saunders »

putenv() in PHP will be temporary. Maybe try "export LD_LIBRARY_PATH=/path/to/lib" in .bashrc
Cappuccinodude
Forum Newbie
Posts: 4
Joined: Tue Jul 08, 2008 2:03 pm

Re: Loading Dynamic libraries in php

Post by Cappuccinodude »

unfortunately i've already tried that and it doesn't work :(. The variable is properly set since when i check the env variables, the LD_LIBRARY_PATH variable has the right path but still it just doesn't seem to work.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Loading Dynamic libraries in php

Post by Ollie Saunders »

OK I'm trying to understand this so I've got a number of questions.
the error message the application gives suggests that it's not able to find the dynamic libraries required for that application despite the fact that the LD_LIBRARY_PATH variable is set.
This error message is being produced by Java or PHP? What is it?
After discussing with a few ppl, many suggested that it could be that PHP requires a few settings, may b there are things you need to check in PHP
What were those discussions and what was suggested? Why do you think those suggestions were made?
The question is, in order to run applications that require shared/dynamic libraries through php on apache (on linux), are there any issues that one might encounter ? How to resolve them ?
Generally you can only run basic shell commands through PHP. It seems like an unusual thing to want to do. Can you show any code?
Please note that the application runs without any problems from the command line however i'm unable to run it from the php script.
Ahh so basically your saying that

Code: Select all

$ the_java_app
works fine but

Code: Select all

<?php echo shell_exec('the_java_app');
gives you errors from the_java_app complaining about this environment variable?

Have you considered trying perl or ruby? Do they yield the same results?
Cappuccinodude
Forum Newbie
Posts: 4
Joined: Tue Jul 08, 2008 2:03 pm

Re: Loading Dynamic libraries in php

Post by Cappuccinodude »

1. This error message is being produced by Java or PHP? What is it?

The program runs to a point where it seems the program requires one of the library's functionality that is required by the program. The application i'm trying to run is like a messenger client which requires certain libraries (which i have in the right locations). As soon as the client tries to sign in, an exception is generated by the java program. This all happens when i try to login through the webserver. Same behaviour is observed when i try to run the program by doing something like
'sudo -u any_user java my_application' however the application runs fine without sudo and just running it from the command line. I tried to launch the application through a bash script which would set all the variables just to make sure if any of the env variables were unset by sudo, they are set again i.e LD_LIBRARY_PATH etc

2. After discussing with a few ppl, many suggested that it could be that PHP requires a few settings, may b there are things you need to check in PHP

Ppl on the developer's forum for the API i used to make the application told me that they were able to regenerate the problem on their machines with the same setup (php-apache-linux) however they also said that they got the application running on the same setup with (java servlets-tomcat-linux). So some thought this might be a php issue but since i'm seeing the exact same behaviour when i try to run it through sudo, i'm not entirely convinced now that THAT is the problem. Moreover i was able to run the application through the php interpreter locally on the machine by doing
'php my_application_launcher.php'. However the same my_application_launcher.php runs into the same problem as mentioned in 1. when it is browsed from a remote machine (i.e launching it from the remote machine's browser)

3. No i have not tried perl or ruby

A few questions

If an application requires *.so libraries, does php automatically take care of that ? One does not need to worry about the fact whether to load them manually or not ?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Loading Dynamic libraries in php

Post by Ollie Saunders »

I must admit. I'm a little frustrated by this. Can you give me an overview of the major components of this application, how they are installed and run? Because if you can't you should probably find out.
exception is generated by the java program
What does it say?
This all happens when i try to login through the webserver. Same behaviour is observed when i try to run the program by doing something like
'sudo -u any_user java my_application'
I'm not familiar with Java. Is this a web-based application? How is it that you can run it both from the command line and "through the web server". What web server are we talking about, you've mentioned apache and tomcat?
I tried to launch the application through a bash script which would set all the variables just to make sure if any of the env variables were unset by sudo, they are set again i.e LD_LIBRARY_PATH etc
Then perhaps this isn't the issue.
Ppl on the developer's forum for the API i used to make the application told me that they were able to regenerate the problem on their machines with the same setup (php-apache-linux) however they also said that they got the application running on the same setup with (java servlets-tomcat-linux).
Is this a Java or a PHP application? Which bits are in which languages?
Moreover i was able to run the application through the php interpreter locally on the machine by doing
'php my_application_launcher.php'. However the same my_application_launcher.php runs into the same problem as mentioned in 1. when it is browsed from a remote machine (i.e launching it from the remote machine's browser)
PHP applications are almost never written to work like that. PHP exists inside the Apache web server and although you can write a web app to work like that you would have to re-implement a web server inside PHP to do it. Why would anyone bother? I don't think you can expect it to work like that. If this is in fact a PHP application at all. Of course if my_application_launcher.php was a script that started up Apache and stuff like that then I would be wrong in a sense. Or is this PHP calling upon Java some how?
If an application requires *.so libraries, does php automatically take care of that ? One does not need to worry about the fact whether to load them manually or not ?
Depends how it's written. Usually, no. Apache uses .so libraries and you can consult the documentation relevant to your version to see how that is done. PHP itself is a .so library for Apache. PHP can use .so libraries within it called extensions. They can be loaded dynamically with dl() or, more commonly and more performantly in php.ini.
Cappuccinodude
Forum Newbie
Posts: 4
Joined: Tue Jul 08, 2008 2:03 pm

Re: Loading Dynamic libraries in php

Post by Cappuccinodude »

Thanks for your interest. I figured out the problem. The problem was not with php, it was not with apache, it was not with anything. The only problem was that whenever i do sudo -u anyuser command ... the HOME environment variable seen by the command (and hence the application that runs) is that of the user who did the sudo, not the one who is "sudoed". If for any reason the application say needs to write to the Home directory , it would end up writing to the home directory of the user who did the sudo but since it won't have the permissions, it won't be able to do so. The libraries i was using somehow required to write some stuff into the HOME directory ; hence the error. When sudo runs a bash script which first resets the HOME env variable and then runs the application, it works fine. Similarly, when the application is launched by a browser on a different machine through a php script, it works provided you change the HOME env variable to say /tmp. On my machine, the HOME env variable Apache used was that of root i.e /root and hence i wasn't able to launch it.

Anyway thanks again for your interest
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Loading Dynamic libraries in php

Post by Ollie Saunders »

Thanks for coming back and telling us what the problem was.
Post Reply