Skip to content
Advertisement

Shell script executed from php, but commands in sh script wont run

I’m making a PHP page with the purpose of creating and activating Apache VirtualHost files.

The pages generates the files and places it in /etc/apache2/sites-available/. After that a shell script is called by with:

shell_exec(“/bin/sh /usr/local/bin/myscript.sh”);

myscript.sh:

#!/bin/sh
file=$(ls -1t /etc/apache2/sites-available/ | head -1)
a2ensite "$file" 2>&1 >/dev/null
service apache2 reload 2>&1 >/dev/null
sleep 5

The script seems to be executed (the sleep time corresponds to the amount of time it takes to run and if I don’t use 2>&1 >/dev/null I get the output from a2ensite). But the site is never enabled.

It works fine if I run the script from terminal, so I’m guessing it’s some sort of permission issue. I’ve been playing around with sudoers and file permissions for two days now, but always with the same results.

Been adding stuff like

www-data ALL=NOPASSWD: /usr/local/bin/myscript.sh

and chmod 777 for testing purposes, but nothing.

Is there any definite way to do this? I’m running Ubuntu 16.04 and PHP7.

Advertisement

Answer

This is solved. The problem was not sudoers or file permissions. The commands were not executed correctly because Apache module mpm-itk was activated. Worked perfectly after I deactivated it.

I didn’t need mpm-itk, but if anyone with similar problems needs it activated you could try this: https://askubuntu.com/questions/491624/setresuid-operation-not-permitted-when-calling-via-php

(Thanks Myran)

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement