How to Enable Shell_Exec and Exec on PHP

How to enable exec() in php?

The setting you need to change is disable_functions and as per the description in the manual:

This directive must be set in php.ini For example, you cannot set this in httpd.conf.

So unfortunately, the answer to your question is no.

With that said, there's usually a workaround so that you don't need to use exec at all. Might be worth explaining in a different question what you're trying to do and see if it's possible.

How to execute shell_exec on EC2 Linux

You should remove these sudoers entries, they shouldn't be necessary in your case and it's a security risk.

I would also recommend to not relay on the output and use the exit status instead:

test.php

<?php
$command = escapeshellcmd('python3 /var/www/html/droptop/blacklist/profanity.py Some BadWord');
exec($command, $output, $return_var);

//echo $output;

if(!$return_var) {
echo json_encode(array("message" => "No Badword found."));
}
?>

profanity.py

import sys

from profanity_check import predict

def profanity(words):
return predict(words)

def main():
words = sys.argv[1:]
ret = profanity(words)
sys.exit(any(ret))

if __name__ == '__main__':
main()

Now, back to your original issue, the docs says

This function can return NULL both when an error occurs or the program
produces no output.

You can un-comment the //echo $output; line and assuming you're using Apache web server running as apache user you can try to run the command below, hopefully this will give you some clue:

$ runuser -l apache -s /bin/bash -c "php -f /var/www/html/droptop/api/event/test.php; echo"
{"message":"No Badword found."}

Possible issues:

  • your python installation is not in the $PATH for apache user

  • profanity_check module is not installed globally

How to enable shell_exec() function in CentOS 5.6?

Make sure your SELinux is disabled.

Enable system() and exec() functions on hosting?

If your host disabled these functions there will be no way to enable them. You can either consider contacting your host to see if they would enable them for a per account basis or if you are using Free hosting you may consider upgrading to a paid version of their hosting for those functions to be enabled. They are disabled by the host for security reasons. Your only course of action however if to contact your web host or try upgrading to paid hosting(if not already).



Related Topics



Leave a reply



Submit