Linux user apache has no permission to file which it owns - php

So I am trying to get shell_exec to give me the response of the program, but it is not giving me any response when I put it into echo. I have tried rewriting the output variable without a input variable, and have tried to produce errors hoping the error would give me some sort of output, but still nothing. Here is the code I am having issues with
<?php
$key = $_GET['key'];
$output = shell_exec("node totp.js ".$key."");
echo "Your output is = $output";
?>
Thanks!
Edit:
I tried defining where node is /usr/bin/node and where the totp.js is /var/www/html/totp.js and it gives an error in my log: No such file or directory. I took off everything other than trying to execute node, and I get the same no such file error. When I go back to what I had to begin with, I now get an error that command is not found, which was not appearing before. Is there a way to allow PHP to roam outside of the execution folder, I.E. the htdocs/html folder?
Edit 2:
Tried changing permissions, chowning, discovered my PHP runs under the user apache, which now owns and has 755 permissions to my entire web folder, and now it recognizes the /usr/bin/node.... but it has no permissions. I have tried adding permissions in every possible way, I've tried giving it 777 although I knew I would not leave it at that, and it still has no permission to access it. I am clueless, and I guess this has become a linux issue rather than a php issue... lol - Also changing the title of the post as it is no longer a shell_exec issue, it is an issue with permissions
Edit 3: Something is royally wrong, it now gives 2 error messages. One is "No such file or directory" and the other is "Permission denied". It gives both, leading me to believe it is running on 2 seperate users/2 seperate locations. Idk what I should do, so I am going to reset my server as I have not done much, and start again. Saving the code first to see if that clears it up for me.

Related

Laravel Cache - file_put_contents file not found

I have this really strange error that I'm struggling to figure out.
I have a command that will cache the homepage. If I run cache:clear then command:updateCaches
And then try and load the site it tells me
file_put_contents(/home/sites/staff.habbocreate.com/storage/framework/cache/data/80/b6/80b69166730ee957730d0770e96781dbd90ae953): failed to open stream: No such file or directory (View: /home/sites/staff.habbocreate.com/resources/views/frontend/home.blade.php)
Its like being logged out causes the error. Except, I can cache the first time without being logged in via my browser and that works no problem.
Here is HomeController.php https://pastebin.com/P1uNNWEs
I don't expect anyone to figure it out, just an idea why it might be going wrong. Any help would appreciated though.
Ok turns out it was my own stupidity causing the error.
I was running the command via terminal so when the data files were created, they were created with the users and group root.
Then when I tried to view it in the browser, the user www-data was unable to access them.
try use public word
file_put_contents(/home/sites/staff.habbocreate.com/public/storage/framework/cache/data/80/b6/80b69166730ee957730d0770e96781dbd90ae953);

File not found with subdomain and php's system() function

In a php script i want to check certain file types, which i do like this:
$info = explode('/',system("file -bi -- uploads/img/test.gif"));
if ( $info[0])
echo 'ok';
This is working just fine, as long as the url is without subdomain, f.e domain.com, but is not working under en.domain.com. It appears, that the file is just not found.
On the other hand, if in the same script i check for existance of the file:
if ( file_exists("uploads/img/test.gif"))
echo 'exists';
The file is found, wheter the current url is domain.com or en.domain.com. Why is the file not found, if the system() function is used?
solved
Friends, i finally figured out the solution, after nearly three working days. Maybe you can imagine how relieved i am.
the solution was in php options to activate mod_rewrite. this was allready activated in regular domain, but not for subdomains. guess i could have thought of this earlier. Even though your answers didnt lead me to how to solve this, i still thank you for your input, as it was going in the same directions i was trying to solve it myself the past days.
The shell command will not necessarily take your file root to be the same as the php command (one will be the user's root directory and the other will be the webserver I think), use absolute path(s) in the shell command and this will probably work how you want it.
You could work out where the two url's are with the file command by using pwd, e.g.
echo system('pwd');
Or you could use finfo-open instead of the shell command which should avoid the problem.

Get file contents in Joomla

I'm trying to get a file's content into a variable but I don't get anything when I read the file. I used both methods JFile::read() and file_get_contents() but both return the same thing: a blank string, not an error , not any boolean values or anything.
I want to mention that I'm working on a Linux machine (just for 2 days) and recently I changed the permissions for the entire machine to 777 ( I don't know if this affects something or not).
Is the a connection between my OS, permissions and the php's file_get_contents()? or Joomla restricts file reading?
Also I want to mention that my file_get_contents() function was added manually by me in the index.php file , also the file I want to read was manually added in the same folder with index.php.
We had the same problem with one of our clients, it turned out it was a firewall issue. It was very hard to debug this issue. I suggest you check with your networking team.
I am assuming, of course, that you have set the PHP error reporting level to the maximum and that you have checked the error logs.
It seemed the problem was from my system . I messed it up when I changed the permissions so I have to re-install it.
Never change the permissions on a Linux for the entire file system.

Replacing PHP file - file doesn't work anymore

I have a weird problem I cannot seem to solve (as a php noob).
I am working on simple php site (no sql involved). Everything was working perfectly till the moment I decided to copy over the files and edit them on another computer. All worked fine on the other computer as well. Then I took the files and copied them to the first computer again. Here came the problem: if I try to open ANY file copied from the second computer, it doesn't show anything, just a blank page. Even if the file was not edited at all on the second computer.
For example my index.php:
1. Copied from comp 1 to comp 2, no edits done
2. Copied from comp 2 to comp 1
3. Opened in the browser -> blank page
What's going on here and how to solve it? Is it some cache that apache is keeping? How to clear it? Both computers with OSX, however the second one was running MAMP, while the first had php/apache/sql set up.
Your permissions need to be set up correctly. Do the following:
Change the owner of the group to your user
chmod 0755 all the directories
chmod 0644 all the files
Then you are good to go!
You need to make sure that your permissions are set up correctly.
Apache Permissions
The blank page is displaying because Apache is encountering a PHP error somewhere, but is not set to display PHP errors.
Check out what is going on in your Apache error log and work from there. You could also look in the Developer Tools of a browser like Chrome, or use Firebug, to see what response your browser is getting from the server (most likely a HTTP 500 error).
I wouldn't recommend blindly changing ownership on files and directories until you know what is going on.

php file not outputting results

I have developed a script but whenever i open admin panel it shows just a blank page, however the same script works fine on my another server. I tried changing chmod of files and folders 644, 755, 775 and 777 but still it outputs a blank page.
The main script works fine but its only admin panel thats not working. I check .htaccess also same on both servers.
Any idea what is wrong?
Thank You.
Check the error log of your HTTP server (e.g. Apache). In 99% of cases of a blank page, PHP has experienced a fatal error and exited before generating any output.
If this script works on another server, I'd check that the script can find everything it needs to include or require. Files not found are common fatal errors when moving a script from one environment to another. For instance, if you deployed the file to the new server without configuring the include_path correctly.
Re your comment about the notice you got:
Notice: Undefined index: submit in /var/www/admin/index.php on line 8
the function on line 8 is if($_POST['submit'] == 'Login')
This means your $_POST array does not contain a field 'submit'. Referencing a non-existant array index in PHP is an E_NOTICE. You can fix this in the following way:
if (array_key_exists('submit', $_POST) && $_POST['submit'] == 'Login')
put this at the top of your admin page
ini_set('display_errors',1);
error_reporting(E_ALL);
The servers probably have different things installed which is causing an error that you can see.
This may sound stupid, but seeing as you all you said was it "outputs a blank page", are you sure there's no output at all? Have you checked the page source, just in case? Remember that if your page starts with a bad HTML tag or something similar, there will be output but that doesn't mean anything is actually rendered in the browser.
Also make sure output buffering isn't on... I can't remember if this can cause issues when trying to debug, but who knows?

Categories