Permission denied when executing php from command line, and crontab - php

I am attempting to create a cronjob under the user apache, but I get permission denied errors for files that are accessed by the program. The specific file that my php script cannot access is /var/www/html/amazon/amazon_data.txt. Here is me checking the permissions, and testing to see if I can write to the file:
bash-3.2$ whoami
apache
bash-3.2$ ls -l /var/www/html/amazon/amazon_data.txt
-rwxrwxr-- 1 apache apache 1082 Apr 3 15:43 /var/www/html/amazon/amazon_data.txt
bash-3.2$ vi /var/www/html/amazon/amazon_data.txt
Now I try to run a script that tries to access the file I get this warning:
bash-3.2$ /usr/bin/php /var/www/html/amazon/amazon_inventory_sync.php
PHP Warning: Module 'json' already loaded in Unknown on line 0
PHP Warning: fopen(amazon_data.txt): failed to open stream: Permission denied in /var/www/html/amazon/amazon_inventory_sync.php on line 26
Warning: fopen(amazon_data.txt): failed to open stream: Permission denied in /var/www/html/amazon/amazon_inventory_sync.php on line 26
Unable to open amazon_data.txt!bash-3.2$
Why can I access and edit the file with the user just fine, but not in the php script when executing it via command line? There is no issue when I run the script from a browser.
Edit: I can run it fine under the user soh, who is in the group apache. apache is also in the group apache.

The issue was due to the file being requested not being a literal location. This is the code which did not work:
$filename = "amazon_data.txt";
$file = fopen($filename, "a+") or die("Unable to open $filename!\n");
This may work fine when the script is run remotely via HTTP, but it may cause issues when running cron jobs or execution in the terminal. Changing the $filename to be the full location fixed this issue.
$filename = dirname(__FILE__)."/"."amazon_data.txt";
$file = fopen($filename, "a+") or die("Unable to open $filename!\n");
Why does this happen? I am guessing instead of the php file's folder being used, it was using a directory which the apache user did not have access to, such as the current working directory that the command in terminal was being executed from.

Related

Permission denied or Something when trying to run PHP script as a CRONJOB to create a .txt file

I am running Ubuntu 16.04. I am trying to run a PHP script located at var/www/html/CronScripts/ as a cronjob. All this script does is create a .txt or .out file in the same directory. Here is the script:
<?php
$handle = fopen("theFileHandle.out", "a");
fwrite($handle, "The quick brown fox jumps over a lazy dog.\n");
fclose($handle);
?>
Now the problem is that, when I execute this script by opening it in the browser, I get the following errors:
Warning: fopen(theFileHandle.out): failed to open stream: Permission denied in /var/www/html/CronScripts/index.php on line 3
Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/CronScripts/index.php on line 4
Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/CronScripts/index.php on line 5
SO my inference was that if this script is run as sudo, there won't be PERMISSION PROBLEMS; and since I am ultimately going to run the script in a cronjob, I executed the following command in terminal
sudo crontab -e
to add the follwoing cronjob to sudo user's crontab file.
* * * * * php /var/www/html/CronScripts/index.php > /var/www/html/CronScripts/output.out
After this, a file named output.out WAS created ** BUT IT WAS EMPTY.
THEN from my websearch I concluded that I was adding cronjob to sudo user's crontab, but apache2/php are run by user www-data, so that is probably the problem. So I switched user to www-data by executing su -s /bin/bash www-data and then to its crontab I added * * * * * php /var/www/html/CronScripts/index.php > /var/www/html/CronScripts/out.out BUT the output log file out.out is never created.
So my question is that why am I facing this problem and how do I solve this?
The original problem seems that the user www-data (the one executing apache and so the php script) does not have rights to write in the directory. You can solve that with chown and chmod, giving permission to read and write to the www-data user in this directory.
That solution could be a security problems, consider using a directory out of the scope of the apache server (e.g /varr/www/anotherplace) and ensure that you dont give execution rights to the files your script create.

Fedora PHP "failed to open stream: Permission denied"

So I've got Apache on web server running Fedora. I'm trying to write into the text file.
if(!empty($_POST['versionWrite'])){
$file = fopen(APP_DIR."/resources/version.txt", "w");
fwrite($file, $_POST['versionWrite'].PHP_EOL);
fclose($file);
}
And when I execute the code, I get this:
Warning: fopen(/var/www/spumprnagle/resources/version.txt): failed to open stream: Permission denied in /var/www/spumprnagle/head.php on line 28
Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/spumprnagle/head.php on line 29
Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/spumprnagle/head.php on line 30
This happens in every script working with files. And I have no idea what shall I do to grant Apache permissions to edit files.
Thanks for your time :).
OK, I add this answer for who are searching for answer on Fedora. I changed file permissions to 777 and file ownership to apache:apache but it keeps throwing the same error. finally I found that in some versions of Fedora SElinux prevents Httpd from writing files. The solution is to disable SElinux as follow:
vim /etc/selinux/config
then edit the file as follow:
SELINUX=disabled
Then Restart.
It worked for me.
Happy coding.
Problem: The apache user doesn't have the permission to write file.
Solution:
chown -R apache:apache path/to/directory
where apache is the default user for fedora and path/to/directory is the path of the directory containing the files with you want to write.
If you want to give the permission to a single file then omit -R
I would start by checking permissions on the file /var/www/spumprnagle/resources/version.txt to ensure that it's writeable by the web user. Since you tagged fedora here, I believe that's usually the apache user, so try chown apache:apache /var/www/spumprnagle/resources/version.txt.
If you're unsure of permission setting you can always temporarily set 777, e.g. chmod 777 /var/www/spumprnagle/resources/version.txt. If that resolves the issue, you can adjust the permissions down to something more reasonable (644).

PHP: fopen - Permission denied - chmod 777 already done

I am setting up a new CentOS 6.6 machine running PHP 5.5 and migrating our site to the server but am getting the error in my httpd error log:
PHP Warning: fopen(/ssd-media/www/app/storage/logs/laravel.log): failed to open stream: Permission denied in /ssd-media/www/index.php on line
I have already checked and the httpd process is being run as the user/group "apache" and have done the chmod -R 775 storage and verified that the file has the right user / group configured and permissions and then ran the following code:
<?php
$myfile = fopen("/ssd-media/www/app/storage/logs/laravel.log", "w") or die("Unable to open file!");
$txt = "test\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
I have moved the DocumentRoot to /ssd-media/www which is not where CentOS is installed, it is an SSD RAID that is only for the apache / mysql files. Other than that it is a fairly simple configuration for which I followed this guide:
https://webtatic.com/packages/php55/
What other reasons would I get a permissions denied on that file? I tried 777 permissions as well to no avail.

fopen() fails to open stream: permission denied, yet permissions should be valid

So, I have this error:
Warning: fopen(/path/to/test-in.txt) [function.fopen]: failed to open stream: Permission denied
Performing ls -l in the directory where test-in.txt is produces the following output:
-rw-r--r-- 1 $USER $USER 1921 Sep 6 20:09 test-in.txt
-rw-r--r-- 1 $USER $USER 0 Sep 6 20:08 test-out.txt
In order to get past this, I decided to perform the following:
chgrp -R www-data /path/to/php/webroot
And then did:
chmod g+rw /path/to/php/webroot
Yet, I still get this error when I run my php5 script to open the file. Why is this happening? I've tried this using LAMP as well as cherokee through CGI, so it can't be this.
Is there a solution of some sort?
Edit
I'll also add that I'm just developing via localhost right now.
Update - PHP fopen() line
$fullpath = $this->fileRoot . $this->fileInData['fileName'];
$file_ptr = fopen( $fullpath, 'r+' );
I should also mention I'd like to stick with Cherokee if possible. What's this deal about setting file permissions for Apache/Cherokee?
Check if the user that PHP runs under have "X" permission on every directory of the file path.
It will need it to access the file
If your file is: /path/to/test-in.txt
You should have X permission on:
/path
/path/to
and read permission on /path/to/test-in.txt
Another reason of this error may be that the directory of file does not exist.
I just add php code before fopen:
if(!file_exists(dirname($file)))
mkdir(dirname($file));
This help me out.

Permission denied when opening or creating files with PHP

I can't create files with php, because the file dosent got permission for that.
I get this error:
Warning: fopen(test.txt): failed to open stream: Permission denied in /web/com/example.com/index.php on line 20
Warning: fwrite() expects parameter 1 to be resource, boolean given in /web/com/example.com/index.php on line 21
Warning: fclose() expects parameter 1 to be resource, boolean given in /web/com/example.com/index.php on line 22
This is the code I was using:
<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
?>
Simple as that! This is example code from w3schools.
So I need help to find out how to give the file the needed permissions.
I'm uploading files to my site with the net2ftp FTP web application, if it matters.
The folder your PHP script is trying to write to will probably be owned by the root user. Your PHP script is more than likely running under the www-data user if you're using a default Ubuntu/Apache/PHP setup.
As such you need to:
chown -R www-data:www-data folder
chmod -R g+w folder
If you find PHP is running under a user that is different from www-data then just change the user a group in the first line of code.
PS. change "folder" for your actual folder name.
The user running PHP (usually the apache user) doesn't have write permission on the folder the script is running in. Try using an absolute path, like "/tmp/test.txt" -- tmp is usually writable by any user, but the contents tend to be wiped out on reboot.
I am using Ubuntu and i solved it by running the command below in one directory up from the folder(.i.e uploads) i was writing the files in.
sudo chmod 777 ./uploads

Categories