cherault Posted December 6, 2019 Share Posted December 6, 2019 Hi all, Working with: Linux Debian Lighttpd Server PHP7.0 I want to launch some shell scripts pressing HTML buttons. All the files are placed in /var/www/html The rights are done by: sudo chown www-data:www-data /var/www/ So, I wrote a very simple code: HTML test code (index.html): <form action="openApp.php"> <input type="submit" value="Open Script"> </form> PHP test code (openApp.php): <?php exec('test.sh'); header('Location: http://localhost/index.html?success=true'); ?> But no luck, nothing is working. Note that the shell script just create an empty file like that: touch /home/$USER/testFile Also, my Lighttpd configuration file is like that: server.modules = ( "mod_access", "mod_alias", "mod_compress", "mod_fastcgi", "mod_redirect", ) May be I am wrong with that piece of code. Do I need some Javascript to do the job ? May be the rights are wrong ? Could you help me please ? I am stuck with that. Thank you for your help and support Link to comment Share on other sites More sharing options...
luisrosety Posted December 6, 2019 Share Posted December 6, 2019 Have you checked permissions for Apache user? (Usually www-data user) What happens if you try creating the file with PHP file command? <?php $handle = fopen("yourPathToTheFile", "w"); ?> Also check if you are running PHP in safe mode. Link to comment Share on other sites More sharing options...
cherault Posted December 6, 2019 Author Share Posted December 6, 2019 Thanks for your reply and proposition. Unfortunately, it doesn't work. I am not using Apache but Lighttpd, I think it is similar. Do you know how to check the safe mode in PHP ? Is it in php.ini ? I will check that point and get back to you. One more time thanks. Link to comment Share on other sites More sharing options...
cherault Posted December 6, 2019 Author Share Posted December 6, 2019 (edited) I tried your solution, but I am stuck with that. Nothing works ! If my HTML page is like that with the post method: <form action="openApp.php" method="post"> <input type="submit" value="Open Script"> </form> Is that correct to call the PHP script ? Or maybe using type=button is better than submit ? All the files (HTML, SHELL and PHP) are managed by the www-data user. Is that correct too ? Another point: When I go to /var/www/html, and launching just my PHP script: cd /var/www/html php openApp.php the script works fine, and write something in a file text like this: <?php $myfile = fopen("/home/$USER/file.txt", "w") or die("Unable to open file!"); $txt = "writing test\n"; fwrite($myfile, $txt); fclose($myfile); ?> But when I use the HTML page, nothing works. So I deduce that is a permission access problem right ? Because the PHP script works, and the HTML works too. So, where I am wrong ? Edited December 6, 2019 by cherault Link to comment Share on other sites More sharing options...
luisrosety Posted December 6, 2019 Share Posted December 6, 2019 Have you enabled lighttpd log? It can provide very useful information. Link to comment Share on other sites More sharing options...
cherault Posted December 6, 2019 Author Share Posted December 6, 2019 Not yet, but I will do it asap. Thanks for the info Link to comment Share on other sites More sharing options...
dsonesuk Posted December 6, 2019 Share Posted December 6, 2019 (edited) Try step by step 1. give submit a name attribute with value, and check if openApp.php can read value, using $_GET or $_POST which ever you use at the time. Edited December 6, 2019 by dsonesuk Link to comment Share on other sites More sharing options...
cherault Posted December 6, 2019 Author Share Posted December 6, 2019 This is the logs...I have some problems with the rights access: COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME lighttpd 716 www-data cwd unknown /proc/716/cwd (readlink: Permission denied) lighttpd 716 www-data rtd unknown /proc/716/root (readlink: Permission denied) lighttpd 716 www-data txt unknown /proc/716/exe (readlink: Permission denied) lighttpd 716 www-data NOFD /proc/716/fd (opendir: Permission denied) Link to comment Share on other sites More sharing options...
cherault Posted December 6, 2019 Author Share Posted December 6, 2019 Thanks dsonesuk, I will get a try to see what is going on Link to comment Share on other sites More sharing options...
cherault Posted December 6, 2019 Author Share Posted December 6, 2019 Ok, doesn't work...I don't what to do. I though it xas simple to build an HTML interface to write some data within a text file, but it isn't !!!! This is why I love C++ 😉 More seriously, is there is a way to understand what's going on with HTMl and PHP. I admit i'm really stuck with that !!! Thanks for your help and support Link to comment Share on other sites More sharing options...
dsonesuk Posted December 6, 2019 Share Posted December 6, 2019 try adding full path to php file in action, just in case its relative path issue. Link to comment Share on other sites More sharing options...
cherault Posted December 6, 2019 Author Share Posted December 6, 2019 Ok, I tried your solution like this: <?php $myfile = fopen("/var/www/html/newfile.txt", "w") or die("Unable to open file!"); $txt = "testwriting\n"; fwrite($myfile, $txt); fclose($myfile); ?> The HTML file is like that: <!DOCTYPE html> <html> <head> <style> </style> </head> <body> <form action="openApp.php"> <input type="submit" name ="value" value="Open Script"> </form </body> </html> But it still doesn't work. The access are the next: -rw-r--r-- 1 www-data www-data 173 déc. 6 12:53 index.html -rw-r--r-- 1 www-data www-data 0 déc. 6 12:34 newfile.txt -rwxrwxr-x 1 www-data www-data 174 déc. 6 13:06 openApp.php What do you think about this ? Link to comment Share on other sites More sharing options...
luisrosety Posted December 6, 2019 Share Posted December 6, 2019 Try adding these lines at the beginning of your PHP script: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); Link to comment Share on other sites More sharing options...
cherault Posted December 6, 2019 Author Share Posted December 6, 2019 (edited) Thanks Luisrosety, I did it but nothing appears... The php script is : <?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); exec("/home/$USER/script"); ?> Is it possible that the web browser (Firefox) block some informations ? Edited December 6, 2019 by cherault Link to comment Share on other sites More sharing options...
Ingolme Posted December 6, 2019 Share Posted December 6, 2019 You should use single-quotes in the exec() command, otherwise it thinks that $USER is a PHP variable. I would expect the above code to throw an error similar to "Undefined variable $USER". Link to comment Share on other sites More sharing options...
cherault Posted December 6, 2019 Author Share Posted December 6, 2019 Thanks Ingolme for the info. But it doesn't solve my problem. I am sure the HTML and PHP code are correct. When I launch the PHP in console mode, everything works fine. But when I launch it through HTML...nothing happen. So, I suppose there is some problems with the permissions, of Firefox is blocking something....I really don't know. I just want to activate a shell script from an html button...I must be easy no ? Link to comment Share on other sites More sharing options...
luisrosety Posted December 6, 2019 Share Posted December 6, 2019 I have tried the following: 1.The php that directly writes the file on the same directory (/home/pi/www/test). 2. Navigate with the browser to the directory http://myserver/test and the file is not created and php gives the error: Warning: fopen(file.txt): failed to open stream: Permission denied in /home/pi/www/html/test/index.php on line 6 Unable to open file! 3. Then I give "rwx" permission to others: chmod o+wx /home/pi/www/test 4. Then going to the same directory http://myserver/test the file gets created The php file (index.php) <?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $myfile = fopen("file.txt", "w") or die("Unable to open file!"); $txt = "writing test\n"; fwrite($myfile, $txt); fclose($myfile); ?> I have done the tests with lighttpd. Launching the creation of the file directly with the script gives more information. You must have permission issues. Link to comment Share on other sites More sharing options...
cherault Posted December 7, 2019 Author Share Posted December 7, 2019 (edited) Hi Luisrosety, Thank you for the help. I did all the steps you gave me, but no luck. Who is the user in /home/pi/www/test ? Is it root or www-data ? Actually, I delete and reinstall lighttpd and php7.0. The actual permissions and owner are root. Do I need to move to www-data like that : sudo chown -R www-data:www-data /var/www/html or sudo chown -R <user>:www-data /var/www/html Edited December 7, 2019 by cherault Link to comment Share on other sites More sharing options...
cherault Posted December 7, 2019 Author Share Posted December 7, 2019 Ok, some news from my tests: I did that: sudo usermod -a -G www-data <user> sudo chgrp -R www-data /var/www/html/ sudo chmod -R g+w /var/www/html/ After these modifications, when I launch the php script, I saw : Notice: Use of undefined constant - assumed '' in /var/www/html/test.php on line 9 But the file was written in /var/www/html So I can say this solve the problem partially. Whatever, Thank you to all of you for your time, help and support Regards, Link to comment Share on other sites More sharing options...
cherault Posted December 7, 2019 Author Share Posted December 7, 2019 At least, one question: Using the base of the php script for writing a file, and using the same HTML page: PHP (test.php): <?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $myfile = fopen("file.txt", "w") or die("Unable to open file!"); $txt = "writing test\n"; fwrite($myfile, $txt); fclose($myfile); ?> HTML (index.html): <!DOCTYPE html> <html> <head> <style> </style> </head> <body> <form action="test.php" method="POST"> <input type="submit" name="Envoyer" value="Open Script"> </form </body> </html> I tried to modify the PHP file to launch a shell script : SHELL (test.sh) #!/bin/bash cd /home/$USER/Prog/C++/Debug/ ./test exit 0 So my PHP file look like this: PHP (test.php) <?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); exec("test.sh"); header('Location: http://localhost/index.html?success=true'); ?> But it doesn't work ! I have the same permissions as before, so the rights access are corrects. So I think it comes from the PHP script, but I don't know why it doesn't work. Do you have an idea ? Thanks for all Link to comment Share on other sites More sharing options...
luisrosety Posted December 8, 2019 Share Posted December 8, 2019 (edited) What happens when to access directly the php document with your browser? Let's say your path to the php document is http://localhost/test.php What happens if you access this document like this instead of launching it with the form? Edited December 8, 2019 by luisrosety Link to comment Share on other sites More sharing options...
cherault Posted December 9, 2019 Author Share Posted December 9, 2019 Unfortunately it doesn't work ! Even if I put the files inside /var/www/html...no luck. What kind of permissions do I need to set to be able to activate a shell script which will launch a program outside /var/www/html ? Link to comment Share on other sites More sharing options...
luisrosety Posted December 10, 2019 Share Posted December 10, 2019 Quote Have a look at this discussion Link to comment Share on other sites More sharing options...
cherault Posted December 11, 2019 Author Share Posted December 11, 2019 Thank you for the informations. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now