2

From a PHP script I want to run a Perl script. I can do this fine from the terminal (as root user) but when run from my application (www-data user) I can't.

When developing the application on localhost I was able to do this fine as well, but the problem appeared when I switched to another server.

My webpage calls the PHP script with an AJAX POST request, which works fine for my other PHP scripts that do not make calls to Perl.

I have tried the following commands in my PHP script with no success:

system("/filepath/file.pl");
exec("/filepath/file.pl");
passthru("/filepath/file.pl");

The Apache logs on the server hosting my application show the following error:

sh: /file.pl: not found

From running the following code on my server I see that the web user is seen as www-data:

ps aux | grep apache

To test, I have also set the permissions for all files and directories use by my application to allow any type of access by any user, using chmod 777.

Please note: my application is only available to certain users (I use a .htaccess file to set these permissions and require the user to login) so if there is a way to change how the server sees this user (from www-data, to the credentials of the logged-in user) then perhaps that would help - how would I do this?

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
Lauragro
  • 21
  • 1
  • `/filepath` would start the ROOT of your drive's filesystem, e.g. ``c:\``. if `/filepath` is the root of your website, well, that won't work. you need to include the entire path: `/home/user/sites/example.com/public_html/filepath/file.pl`- type thing – Marc B Aug 24 '15 at 20:45
  • Does `'not found'` really indicate a permissions problem? – Don't Panic Aug 24 '15 at 20:49

2 Answers2

0

Try something like this

exec("/usr/bin/perl /full-path/script.pl",$output);
Carlos Garcia
  • 457
  • 5
  • 14
  • 5
    Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Aug 24 '15 at 21:15
0

Thanks for all the suggestions! In the end, it was a problem with security settings in the php.ini file in the Apache directory on the server hosting my application:

/etc/php5/apache2/php.ini

Safe mode was set to 'on', which was preventing php files to access/execute any local files. (The PHP script was also failing when attempting access to another, not-script, file: other.html).

To solve this without removing the safe mode setting for all applications on my server, I changed the above php.ini file to include the following:

safe_mode: on
safe_mode_include_dir: /filepath
safe_mode_exec_dir: /filepath

More information on these config variables can be found here: http://php.net/manual/en/ini.sect.safe-mode.php

Lauragro
  • 21
  • 1