The following line was the best I read to this problem: "You need to disable safe mode in the php.ini located in the \Apache2\bin folder, not the php folder. Restart Apache2 service right after to load the new settings."

The solution is:

  1. stop all services from Wamp-Server and close the programm

  2. Open .../wamp/bin/apache/Apache2../bin/php.ini

  3. copy php.ini to desktop and open it

  4. set safe_mode_exec_dir (line after = is empty, so IT IS ON!!!) set it off!

  5. save

  6. copy back to dir (maybe you need admin rights)

  7. start wamp-server

  8. enjoy exec() and co.

Answer from Sturmi on Stack Overflow
🌐
GitHub
gist.github.com › MahbbRah › bd4e7c8905c118dffeacd44934a7c450
Enable exec shell_exec in php · GitHub
Enable exec shell_exec in php. GitHub Gist: instantly share code, notes, and snippets.
Discussions

PHP shell_exec() executing in C:/xampp/htdocs and not in C:/xampp/htdocs/folder - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I am trying to execute a file using shell_exec in PHP. However, whenever a .bat file is executed it's ran from cd C:/xampp/htdocs instead of a ... More on stackoverflow.com
🌐 stackoverflow.com
April 23, 2018
[SOLVED] shell_exec() problems
Hi, my probelm is that i have full access to the shell of the server (fedora 3.1) but running this code in a php file on xampp server that i have on the same server , i am not getting the output even when i am trying to pipe the output to an html file . shell_exec("cat `find ./logs -... More on forums.phpfreaks.com
🌐 forums.phpfreaks.com
8
August 2, 2007
python - Why doesn't shell-exec() run in PHP? - Stack Overflow
I wanna run python code in PHP and the PHP code is bellow: More on stackoverflow.com
🌐 stackoverflow.com
March 15, 2019
php - The shell_exec and exec() commands don't work on linux Ubuntu in my localhost Ububtu 20.04 - Stack Overflow
The following code only works in my Terminal but it doesn't work in my localhost xampp linux Unbuntu 20.04, Both shell_exec() and exec() don't work. When I go to localhot it executes all the code but not its two functions. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Apache Friends
community.apachefriends.org › board index › xampp in english (language english only!) › xampp for windows
Exec and shell_exec not working - Apache Friends Support Forum
January 18, 2011 - Apache likes Windows paths to be contained with quote marks " " which may also be an issue here but is not a requirement in Linux paths and may be part of the reason your script works in Linux but not in Windows XAMPP.. Hope this helps a little. Good luck. ... Joined: 17. March 2009 11:06 ... Post by peterwt » 18. January 2011 12:37 ... exec("C:\\Windows\\System32\\mspaint.exe",$output, $return); works fine for me and opens Paint.
🌐
Apache Friends
community.apachefriends.org › board index › xampp in english (language english only!) › xampp for windows
shell_exec() will not execute outside htdocs folder - Apache Friends Support Forum
July 26, 2010 - Maybe that is what you thought I was trying to run php outside the htdocs folder. Maybe because it (the shell command) is executing inside PHP, it is bound by the constraints of the server container. PHP on XAMPP is an Apache module. Thus - the script (in each instance) has no rights outside ...
🌐
Stack Overflow
stackoverflow.com › questions › 49972052 › php-shell-exec-executing-in-c-xampp-htdocs-and-not-in-c-xampp-htdocs-folder
PHP shell_exec() executing in C:/xampp/htdocs and not in C:/xampp/htdocs/folder - Stack Overflow
April 23, 2018 - I want it in the folder it was executed from. ... Save this answer. Show activity on this post. Solved. Used chdir('c:/xampp/htdocs/TeenHost/mc/folder/'); before using shell_exec
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
[SOLVED] shell_exec() problems - PHP Coding Help - PHP Freaks
August 2, 2007 - Hi, my probelm is that i have full access to the shell of the server (fedora 3.1) but running this code in a php file on xampp server that i have on the same server , i am not getting the output even when i am trying to pipe the output to an html file . shell_exec("cat `find ./logs -...
🌐
Apache Friends
community.apachefriends.org › board index › xampp in english (language english only!) › xampp for windows
Can't migrate website becuase shell_exec is disabled - Apache Friends Support Forum
February 10, 2023 - Enable ZIP in XAMPP XAMPP v8.2.00 Source: https://stackoverflow.com/questions/30558950/ziparchive-not-installed-in-xampp Stop the xampp and then remove the starting semicolon ( ; ) before ;extension=zip from your xampp/php/php.ini the following code, and start xammp again.
Find elsewhere
🌐
Memento
ragrawal.wordpress.com › 2006 › 07 › 03 › php-shell_exec-problems
PHP Shell_exec problems - Memento - WordPress.com
May 31, 2008 - Restart your webserver and try to run the php program again. “Shell_exec” command should be working now. Problem 2: Able to execute simple commands such as “cmd”, “cd”, etc., but not perl script
🌐
PHP
php.net › manual › en › function.shell-exec.php
PHP: shell_exec - Manual
... If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg: Won't always work: echo shell_exec("gunzip -c -t $path_to_backup_file"); Should work: ...
🌐
Stack Overflow
stackoverflow.com › questions › 55179823 › why-doesnt-shell-exec-run-in-php
python - Why doesn't shell-exec() run in PHP? - Stack Overflow
March 15, 2019 - Also, can you add after <?php this line error_reporting(E_ALL);? ... Save this answer. Show activity on this post. If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
🌐
Apache Friends
community.apachefriends.org › board index › xampp in english (language english only!) › xampp for windows
Is it possible to run Shell exec on Xamp? - Apache Friends Support Forum
May 5, 2009 - Pleeease help!! ... Joined: 01. February 2004 12:38 ... Post by Wiedmann » 07. May 2009 15:27 · By searching and googling i found these solution proposals so far Maybe this was not clear: You must do nothing in XAMPP/Win (standard installation) so shell_exec() works.
🌐
Apache Friends
community.apachefriends.org › board index › xampp in english (language english only!) › xampp for windows
No longer able to run php shell_exec() XAMPP windows - Apache Friends Support Forum
August 4, 2021 - Then the next day in the morning my app no longer worked! Odd thing is now I can not run the php code with shell_exec() commands on this mini PC. In fact on mini PC if I now try to run XAMPP / Apache PHP/ Filezilla as a program in user mode I am asked if I "want to allow NET (Something)..." ...
🌐
Ynet Interactive
ynetinteractive.com › blog › How-To-Enable-or-Disable-Shell_exec-in-PHP-17
How To Enable or Disable Shell_exec in PHP
First you need to find the location of PHP.ini. You will need to have root access to your server before you can perform the following steps Type this...
Top answer
1 of 2
1

1) Check the security setup

Can shell_exec() be run or not? If it can't then it's because you are having some security settings enabled.

The problem is that shell_exec() and exec() can be disabled for security reasons. This can be changed by editing PHP's disable_functions option.

INI settings are usually stored in specific *.ini files under the /etc/php/* directories. So you have to look for some settings in there. You’ll find several INI settings, depending on how PHP is run. To find them :

find /etc/php/ -iname 'php.ini'

This could output something like this :

/etc/php/8.2/fpm/php.ini
/etc/php/8.2/apache2/php.ini
/etc/php/8.2/cli/php.ini
/etc/php/8.2/cgi/php.ini

To see the settings and which INI file is loaded, add this at the beginning of your script and call it via your web server :

phpinfo();
die();

You'll see all the loaded INI files and what is set on the disable_functions option.

/etc/php/8.2/cli/php.ini is probably setting disable_functions to an empty string, meaning that in command line there's no restriction. But for the other INI files, this option might contain a list of functions that you won't be able to run. Edit this option if needed. But be always aware that giving your server the ability to run some binaries can lead to some security issues if your app is taking some user input to build the command.

Apache configuration can also set some PHP settings, just for a specific Virtual Host. So have a look at them if you can't find something in the php.ini files.

Depending on your PHP setup, you may also have to check safe_mode and suhosin.executor.* if you have the Suhosin hardening module installed.

2) Verify that you can run shell_exec

There are several ways to run a binary. shell_exec is equivalent to the built-in backtick operator. So if shell_exec is listed in the disable_functions option then you won't be able to use the backtick operator.

If you get an error 500 then it's typically because you cannot run shell_exec. If you check /var/log/nginx/error.log or /var/log/apache2/error.log with this command :

sudo tail -f /var/log/{apache2,nginx}/error.log

It will print both logs until you press CTRL+C. You may see this error :

PHP Fatal error:  Uncaught Error: Call to undefined function shell_exec()

PHP also offers exec(), passthru(), system(), and the proc_*() functions. So you've got several possibilities to try and run your command.

Have a try with a simple PHP code :

<?php

header('Content-Type: text/plain; charset=utf-8');

echo 'PHP user given with the "id" command: ' . `id`; // equivalent to shell_exec('id');
echo 'System date is ' . `date --iso-8601=seconds`;

This should output RAW text in your browser with :

PHP user given with the "id" command: uid=33(www-data) gid=33(www-data) groups=33(www-data)
System date is 2023-09-29T11:50:28+00:00

3) Use a full path to the binary

When shell_exec() is run from the web server, it's rather common that the binary isn't found in the PATH. Usually, the www-data user running PHP won't even have a shell. But this user can normally run some binaries, except if some settings at point 1 is blocking it.

To find the full path to a binary, use the which command :

which wkhtmltopdf

This should output the full path to the binary. Certainly something such as /usr/bin/wkhtmltopdf.

Now replace your PHP code with the full path to the binary:

header('Content-Type: text/plain; charset=utf-8');
echo shell_exec('/usr/bin/wkhtmltopdf --version');

Does it print wkhtmltopdf 0.12.6 like expected?

If no, then we have to investigate more at the next point.

4) Get the execution output and find errors

This can be done with the help of exec(), passthru() or system() as they all return the execution status code.

With the proposition of Volkerschulz, we'll also redirect the standard error stream to the standard output stream so that we can see some details that may have only be printed to the error stream.

This is done by redirecting stderr (2) into stdout (1) by adding 2>&1 after your command.
Just as info, stdin is 0.

In PHP, this becomes :

<?php

header('Content-Type: text/plain; charset=utf-8');

$command = '/usr/bin/wkhtmltopdf --wrong-option 2>&1';

$last_line = exec(
  $command,    // The command to execute.
  $output,     // A variable that will be filled with an array of all the lines returned.
  $result_code // The return status of the executed command.
);

var_export([
  '$command' => $command,
  '$output' => $output,
  '$last_line' => $last_line,
  '$result_code' => $result_code,
]);

This outputs :

array (
  '$command' => '/usr/bin/wkhtmltopdf --wrong-option 2>&1',
  '$output' => 
  array (
    0 => 'Unknown long argument --wrong-option',
    1 => '',
    2 => 'Name:',
    3 => '  wkhtmltopdf 0.12.6',
    ...
    ...
    95 => '',
  ),
  '$last_line' => '',
  '$result_code' => 1,
)

Now I'm able to see that I got a $result_code of 1 instead of 0.

In case the executable is having other errors, you'll see them in the $output array as the stderr stream should be visible. This should help you find how to fix the issue. Probably some other security problems due to the fact the www-data user might be missing some rights, paths or whatever.

PS: On my Vagrant box of Ubuntu 22.04, I managed to run wkhtmltopdf without errors on both Apache and NGINX.

Don't forget also that your www-data user should have write access to the destination folder where you are producing the PDF file.

5) Try re-installing wkhtmltopdf and/or use a PHP wrapper

You might have to re-install wkhtmltopdf if it's not properly working :

sudo apt remove wkhtmltopdf
sudo apt autoremove
sudo apt install wkhtmltopdf

You can also try using a PHP wrapper library for wkhtmltopdf called Snappy. It might be useful.

6) Switch to an alternative in pure PHP if necessary

Perhaps you won't manage to make wkhtmltopdf work that easily. It's a pity as it's effectively a fast and reliable solution.

But to create a PDF from HTML you could also use the Dompdf PHP library. I've used it in the past and was happy with it.

2 of 2
0

Instead of

shell_exec($wkhtmltopdfCommand);

try

$result = passthru($wkhtmltopdfCommand . ' 2>&1');
var_dump($result);

and it should tell you what's wrong.

🌐
CopyProgramming
copyprogramming.com › howto › how-to-use-shell-exec-xampp-php-in-a-mac
Php: Using shell_exec with XAMPP and PHP on a Mac: A Guide
March 27, 2023 - On *NIX systems, it is important to specify the exact location of the file when executing it, or it must be present in the $PATH directory. ... To enable direct command input without specifying the location, add , follow these steps to eliminate the need to provide it to your PATH variable . export PATH=$PATH:/Application/XAMPP/xamppfiles/bin
🌐
Reddit
reddit.com › r/phphelp › problem with exec() function
r/PHPhelp on Reddit: Problem with exec() function
December 24, 2022 -

Hi there

I'm working on an Arduino project and I want to make a connection to a serial port with PHP to send/receive data, after a lot of googling I couldn't figure it out, so I decided to use the shell_exec() function to run a python program that does this job, and the shell_exec() runs commands like "ls, pwd", but when I run python program it doesn't work, so how can I run the python program?

I use Ubuntu 20.04 and XAMPP as my local server.

PHP code:

<?php
$output = null;
$output = shell_exec("./turnled.py");
echo $output;
?>

python code:

#!/usr/bin/env python3

import serial
import time

arduino_port = serial.Serial("/dev/ttyACM0", 115200)
time.sleep(5)
arduino_port.write(b'1')
print("OK")
arduino_port.close()

the .py file is executable.

🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › using the raspberry pi › troubleshooting
PHP Command shell_exec with Apache - Raspberry Pi Forums
If you are still not finished with your project, it might be useful to make 'pi' the owner of all your files, you want to edit and then edit your files without sudo. When you are finished, give them all back to root. If everything is still working remove the line, you added in visudo. Make sure that you get the error message that I posted above ... sudo: no tty present and no askpass program specified Then remove the 'sudo' from your shell_exec commands (e.g.