php exec() not executing binary symlinked from /usr/bin/ to /usr/libexec/ - php

I'm using PHP 5.5.6 and trying to use exec() to execute the gitolite binary that is located in /usr/libexec/gitolite/gitolite and is symlinked from /usr/bin/gitolite to that binary.
I am able to run other commands (such as id) that are located in /usr/bin/. If I copy or hardlink the binary to /usr/bin/gitolite, it also works just fine. But when it's symlinked to /usr/libexec/gitolite and I include 2>&1 at the end of the command string passed to exec(), the output is
sh: find: command not found
It seems to me like there's a readlink going on somewhere and it sees that /usr/libexec/gitolite is not in some allowed list of directories, but the safe_mode_exec_dir configuration directive doesn't exist in PHP 5.5.
For what it's worth, this is running in Apache 2.4.6.

Related

Lame conversion works in CMD but not in PHP

exec("lame audio.mp3 audio.wav")
Similar questions didn't helped.
This is the command that should be executed via PHP.
The audio.mp3 file presents in the directory and no permission issues.
Lame encoder is present at: C:\Program Files (x86)\Lame. This path is added into System Environmental Variables PATH.
lame command works in cmd, it displays the version as 3.99.3. (This means lame is installed and added to the PATH properly).
The PHP file present in the folder D:\XAMPP\htdocs\demo.php. Executing lame audio.mp3 audio.wav via cmd in the directory D:\XAMPP\htdocs runs properly and the mp3 file is converted to wave file without any issue.
The PHP exec is alone not working. Where I am missing?
Edit: The error.log file have the following error:
'lame' is not recognized as an internal or external command, operable
program or batch file.

PHP shell_exec() SASS --update

I am trying to execute sass --update via PHP shell_exec() function and get an error:
H:\SERVER\htdocs\path\to\project>sass --style compressed --update sass\:deployment\css\ 'sass' is not recognized as an internal or external command, operable program or batch file.
If I run the same command from the cmd manually - it works fine. The current folder is correct - checked with the getcwd()
When you run the command sass in a shell, your computer has to find the executable program called sass somewhere on your computer. To do this, it looks through a series of folders called your PATH. Your PHP server is most likely running with a different PATH than your command prompt, and as such, is not able to find the executable.
To fix this: From your command prompt, where sass DOES work, run this command: which sass (on Windows, instead use where sass)
That will tell you the exact location of sass (for example, it might be /usr/bin/sass or something like that). Once you have that value, replace "sass" in your PHP code with that entire location.

Change Default version of PHP that is called on the command line

is there a way to change the default version of PHP that is called on the command line?
I am trying to install Laravel using composer the problem is composer is calling php (which is version 4.4) when it needs to call php5.4, i have changed the binary in the header from #!/usr/bin/env php to #!/usr/bin/env php5.4 but this throws an error!
here is my php set-up
php: /usr/bin/php /usr/bin/php5.4 /usr/bin/php5.5 /usr/lib/php /usr/lib/php.ini-nourl /usr/lib/php.ini /usr/lib/php5.4 /usr/lib/php5.5 /usr/local/bin/php /usr/local/bin/php5.4 /usr/local/bin/php5.5 /usr/local/lib/php.ini-nourl /usr/include/php /usr/include/php5.4 /usr/include/php5.5 /usr/local/php /usr/share/php
There are a couple options here, but really this is not php specific, this is about basic execution path rules.
The #!/usr/bin/env php header means that when you run composer alone it runs it with php, so if you manually run it like php5.4 /path/to/composer you can bypass that header.
The other option is to define a symlink in /usr/local/bin/php or something that:
points to /usr/bin/php5.4
is in a directory that has precedence over /usr/bin inside your PATH environment variable.

Running PHP script from the command line

How can I run a PHP script from the command line using the PHP interpreter which is used to parse web scripts?
I have a phpinfo.php file which is accessed from the web shows that German is installed. However, if I run the phpinfo.php from the command line using - php phpinfo.php and grep for German, I don't find it. So both PHP files are different. I need to run a script which the php on which German is installed.
How can I do this?
You should check your server configuration files. Look for lines that start with LoadModule php...
There probably are configuration files/directories named mods or something like that. Start from there.
You could also check output from php -r 'phpinfo();' | grep php and compare lines to phpinfo(); from web server.
To run php interactively:
(So you can paste/write code in the console.)
php -a
To make it parse a file and output to the console:
php -f file.php
Parse a file and output to another file:
php -f file.php > results.html
Do you need something else?
To run only a small part, one line or like, you can use:
php -r '$x = "Hello World"; echo "$x\n";'
If you are running Linux then do man php at the console.
If you need/want to run PHP through fpm (FastCGI Process Manager), use cli fcgi:
SCRIPT_NAME="file.php" SCRIP_FILENAME="file.php" REQUEST_METHOD="GET" cgi-fcgi -bind -connect "/var/run/php-fpm/php-fpm.sock"
Where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.
On SUSE Linux, there are two different configuration files for PHP: one for Apache, and one for CLI (command line interface). In the /etc/php5/ directory, you will find an "apache2" directory and a "cli" directory. Each has a "php.ini" file. The files are for the same purpose (PHP configuration), but apply to the two different ways of running PHP. These files, among other things, load the modules PHP uses.
If your OS is similar, then these two files are probably not the same. Your Apache php.ini is probably loading the German module, while the the CLI php.ini isn't. When the module was installed (auto or manual), it probably only updated the Apache php.ini file.
You could simply copy the Apache php.ini file over into the cli directory to make the CLI environment exactly like the Apache environment.
Or, you could find the line that loads the German module in the Apache file and copy/paste just it to the CLI file.
I was looking for a resolution to this issue in Windows, and it seems to be that if you don't have the environment variables ok, you need to put the complete directory. For example, with a file in the same directory as PHP:
F:\myfolder\php\php.exe -f F:\myfolder\php\script.php

Is #!/usr/bin/env required to run PHP from command line?

Often times when I see PHP that is meant to be ran from the command line, it will have this line #!/usr/bin/env php at the top of the file like this...
#!/usr/bin/env php
<?php
// code
?>
I was wanting to know if this is meant just for when the file is ran on a Linux/Unix system or is needed for running on Windows as well?
The shebang line is required for auto-detection of the type of script. It enables this sort of usage:
[pfisher ~]$ chmod +x run-me.php
[pfisher ~]$ ./run-me.php
That line is not needed if you pass the filename as an argument to the php interpreter, like so:
[pfisher ~]$ php run-me.php
Edit: replace "hashbang" with shebang.
No it's not, you can directly use
#!/path/to/php
Running php (or anything else) through the env utility is a weak security measure. Dpending on the platform, will "fix" PATH, LIB, and other environment variables according to various config files and potentially remove some of the dangerous values in there (e.g. env on HPUX).
It is also to limit the scope of shell-expansions on certain environments. (See man 1 env on Linux).
the magic belongs to the executable flag (chmod +x FILE)
the shebang: and if it exists with that version you may expect
/usr/bin/env cliVersion -> /usr/bin/env php
php -v # tells you which version you have by default as: 'cli' in a shell
alternativly use e.g:
php8.0 /to/php/script.php
to run it without a shebang at the first line of the script.
(it will work even if it stays there but check the real php version on execution if important for you)
"standard"? over the last 10 years is /usr/bin/env depending on what version you set to be the default on your system:
debian systems (debian,unbuntu,kubuntu...): #root: update-alternatives --config php will guide you

Categories