Permissions are as open as I know how to make them (777). Yes, I know this isn’t safe. Once I figure out the problem, I will make them safe.
# ls -lhd /srv/www/html/sigs/
drwxrwsrwx. 2 root root 4.0K Apr 13 19:37 /srv/www/html/sigs/
trial.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$file = '/srv/www/html/sigs/file.txt';
$mf = fopen($file, 'w');
fwrite($mf, 'hi');
fclose($mf);
echo $file;
?>
output
Warning: fopen(/srv/www/html/sigs/file.txt): failed to open stream: Permission denied in /var/www/html/trial.php on line 6
Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/trial.php on line 7
Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/trial.php on line 8
/srv/www/html/sigs/file.txt
I am pulling out my hair here :) I have tried setting the directory permissions to 777, 775, 755 I have tried setting the user and group to apache If I write to the /tmp directory, I don’t get any errors, but I also don’t get a file.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Spent a lot of time looking for this answer but it’s all in bits and pieces and no one every posts a solution (well most of the time) so here is my solution and it’s used on various web control panels as well.
install and use MOD_RUID2
Install PHP with CLI (this is standard on newer versions)
In your HTTPD.CONF file in the virtual hosts, you’ll add the following, replacing username with the user’s login name, and usergroup with the user’s group (These are usually the same)
<IfModule !mod_ruid2.c>
SuexecUserGroup username usergroup
</IfModule>
<IfModule mod_ruid2.c>
RMode config
RUidGid username usergroup
RGroups @none
</IfModule>
An example of a Virtual host conf is:
<VirtualHost *:443>
DocumentRoot "/home/imtheuser/public_html"
ServerName imtheuser.com
<IfModule !mod_ruid2.c>
SuexecUserGroup imtheuser imtheuser
</IfModule>
<IfModule mod_ruid2.c>
RMode config
RUidGid imtheuser imtheuser
RGroups @none
</IfModule>
<Directory "/home/imtheuser/public_html">
allow from all
Options None
Require all granted
</Directory>
</VirtualHost>
This will allow apache/php to write to a directory owned by the user. It’s much safer then setting your chmod to 0777.
Excellent point ryanpq. Thank you.
Verify web user name
# ps aux | grep apache
apache 17856 0.0 2.6 457444 26508 ? Sl 11:24 0:04 /usr/sbin/httpd -DFOREGROUND
apache 17893 0.0 2.8 459780 29156 ? Sl 11:25 0:01 /usr/sbin/httpd -DFOREGROUND
...
# ps aux | grep httpd
apache 17856 0.0 2.6 457444 26508 ? Sl 11:24 0:04 /usr/sbin/httpd -DFOREGROUND
apache 17893 0.0 2.8 459780 29156 ? Sl 11:25 0:01 /usr/sbin/httpd -DFOREGROUND
...
Verify owner/permissions for directory
# ls -lhd sigs
drwxr-sr-x. 2 apache apache 4.0K Apr 14 09:29 sigs
To eliminate a path issue, I moved trial.php into the sigs directory and updated the $file variable so it is attempting to write directly within that directory
# cat /var/www/html/sigs/trial.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$file = 'file.txt';
echo "file: $file<bR>";
$mf = fopen($file, 'w');
fwrite($mf, 'hi');
fclose($mf);
?>
Output of accessing the file: http://xxxxxx/sigs/trial.php
file: file.txt
Warning: fopen(file.txt): failed to open stream: Permission denied in /var/www/html/sigs/trial.php on line 7
Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/sigs/trial.php on line 8
Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/sigs/trial.php on line 9
Change permissions for full access
# chmod 0777 /var/www/html/sigs
# ls -lhd /var/www/html/sigs/
drwxrwsrwx. 2 apache apache 4.0K Apr 14 09:29 /var/www/html/sigs/
And try again: http://xxxxx/sigs/trial.php
file: file.txt
Warning: fopen(file.txt): failed to open stream: Permission denied in /var/www/html/sigs/trial.php on line 7
Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/sigs/trial.php on line 8
Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/sigs/trial.php on line 9
Exact same problem i have. Any solution offer? I think SELinux has controlled this. But not tested. Looking forward for any solution.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.