Adding apache to a user group file upload permission - php

I have a php script uploading files to a certain folder, currently they are uploading as a 'psacln' group, so that I can delete the files via FTP. This was all working fine when PHP was running as FastCGI, I had to change PHP to run as Apache Module in order to get a php extension to work. But now I can't delete files via PHP script because permission is denied. I assume because now the group 'Apache' is trying to delete the file that belongs to 'psacln'. How do I allow apache to delete those files?
EDIT: ls -alF
drwxr-xr-x 2 fugitiveink psacln 4096 Nov 13 14:05 92/
drwxr-xr-x 2 fugitiveink psacln 4096 Nov 13 06:57 93/
drwxr-xr-x 2 fugitiveink psacln 4096 Nov 13 14:12 95/
drwxr-xr-x 2 fugitiveink psacln 4096 Dec 21 18:56 96/
drwxr-xr-x 2 fugitiveink psacln 4096 Dec 21 08:30 97/
drwxr-xr-x 2 fugitiveink psacln 4096 Nov 13 14:26 98/
drwxr-xr-x 2 fugitiveink psacln 4096 Nov 13 14:28 99/

I assume that you have shell and root access to this system. If so, you can try adding the apache user (typically apache or www-data) to the /etc/group file.
The proper way to do this is to use usermod, though I typically just edit the file directly.
In short if your apache user is apache, try:
sudo usermod apache --append --groups psacln
This basically gives the apache user access to any files & directories that are owned by the psacln group.
If this doesn't work, post an example of your directory with the file permissions (ls -alF) and we can work from that.
EDIT:
To directly edit the groups file using nano (substitute with whichever editor you're comfortable with):
sudo nano /etc/groups
and find the psacln group and add the apache user:
psacln:x:130:apache
Note that the gid (130) will undoubtedly be different.

Set the permissions on the upload directory to 777 (wrx for all users). Can you still upload new files? If you can, you should be able to delete files.

Related

PDFTK Cannot Create User Directory

I am attempting to run pdftk as user www-data with the following command (as root):
sudo -u www-data /snap/bin/pdftk
But I am getting this error:
cannot create user data directory: /var/www/snap/pdftk/9: Read-only
file system
(Ubuntu 18.04, PDFTK installed with SNAP).
The /var/www/snap/pdftk and /var/www/snap/pdftk/9 directories have rw permissions for the www-data user:
drwxr-xr-x 3 www-data www-data 4096 Jan 23 15:44 ./
drwxrwxrwx 4 root root 4096 Jan 23 15:44 ../
drwxrwxr-x 4 www-data www-data 4096 Jan 23 15:44 pdftk/
drwxrwxr-x 4 www-data www-data 4096 Jan 23 15:44 ./
drwxr-xr-x 3 www-data www-data 4096 Jan 23 15:44 ../
drwxrwxr-x 2 www-data www-data 4096 Jan 23 15:44 9/
drwxr-xr-x 2 www-data www-data 4096 Jan 23 15:44 common/
lrwxrwxrwx 1 www-data www-data 1 Jan 23 15:44 current -> 9/
I need to be able to run PDFTK as www-data user because I want to call PDFTK from a PHP script served by apache2, as in:
<?php
`/snap/bin/pdftk file1.pdf file2.pdf cat output file3.pdf`;
NOTE: the pdftk wrapper for PHP, mikehaertl/php-pdftk, doesn't seem to want to save files either. I tried the ->saveAs() function but no file is generated, even when attempting to write to /tmp, which should have full write permissions. No errors in apache2 error log, so I'm betting it's the same issue.
I encountered exactly the same problem! In particular, I didn't face this problem when using pdftk in Ubuntu 18.10 with pdftk already included back, but faced this problem on a server with Ubuntu 18.04.
My workaround is NOT to install pdftk via snap. I installed pdftk with debian packages, following the instruction at https://www.webdesign101.net/install-pdftk-on-ubuntu-18-04-bionic/.
Everything then just works fine.

LAMP - PHP save files, but browser does not read

Images saved by PHP are linked to the www-data user, and therefore are not read by the browser unless I run it in ROOT mode. However, if I open a terminal and use CHOWN or CHMOD, I can solve this manually. But I do not plan to do this every time I upload an image.
Is there a way to make PHP save the images correctly? ie belonging to ROOT or my user?
Below is a portion of the directory listing. As soon as the file wi_10026.png' has been saved. Notice that the owner is different. This causes the browser to not be allowed to execute it.
-rw-r--r-- 1 www-data www-data 27150 Jan 2 14:44 wi_10026.png
-rwxrwxrwx 1 root root 43007 Set 11 2014 wi_cadeado.png
-rwxrwxrwx 1 root root 15603 Dez 23 18:39 wi_expirou.png
-rwxrwxrwx 1 root root 12139 Nov 16 2015 wi_guarda.png
-rwxrwxrwx 1 root root 13960 Out 20 23:50 wi_livro.png

Cannot run index.html from /var/www/html - 403 forbidden

I copied a few files from a local folder to the apache server folder /var/www/html, which includes an index.html as well.
I created a test file to check php version inside the folder and it gave the correct result of phpinfo().
But I cannot run localhost/index.html from the browser. I get the error-
Forbidden
You don't have permission to access /index.html on this server.
Apache/2.4.7 (Ubuntu) Server at localhost Port 80
I checked the owner info by running ls -l inside /var/www/html, and this is the result-
drwx--S--- 4 root www-data 4096 Mar 26 22:28 ch01
-rw-r--r-- 1 root www-data 20 Mar 26 22:16 check.php
-rw------- 1 root www-data 36911 Mar 26 22:28 fang.jpg
-rw------- 1 root www-data 2060 Mar 26 22:28 index.html
-rw-r--r-- 1 root www-data 19 Mar 26 22:28 pp.php
-rw------- 1 root www-data 1261 Mar 26 22:28 report.php
-rw------- 1 root www-data 77 Mar 26 22:28 style.css
I am trying to run the example code from head first into php and mysql.
I installed apache and php using this guide - https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu
-rw------- 1 root www-data 2060 Mar 26 22:28 index.html
The file is
owned by root (who can read and write to it (rw-))
a member of the www-data group (which can't do anything with it (---))
can't be touched by the public (---)
Your webserver is almost certainly running as www-data, so you need to either:
Change the ownership of the file: chown www-data index.html or
Give the group permission to edit it: chmod 660 index.html
You have similar issues with other files.
NB: The current ownership and permissions suggests that you are using the root account to manage the files for the website. Don't do that. Create an account with more limited access for that purposes. Only run as root when you really have to.
the default location is
/var/www/
and not '
/var/www/html/
you can access the index.html at
http://localhost:80/html/index.html

PHP is_writable() function always returns false for a writable directory

I'm trying to install a PHP-based software package in a Red Hat 7 Amazon EC2 instance (ami-8cff51fb) that has had Apache 2.4.6 and PHP 5.4.16 installed on it using yum. The installation fails because it says a particular directory needs to be writable by the webserver with 0755 or 0775 permissions.
The directory in question has 0775 permissions with root:apache ownership. I have verified that the httpd process is being run by the apache user and that the apache user is a member of the apache group.
If I edit /etc/passwd to temporarily give the apache user a login shell and then su to that account, I am able to manually create files as the apache user within the directory using the touch command.
I took a look at the source code of the installer script and identified that it's failing because PHP's is_writable() function is returning false for the directory in question. I created a separate test PHP script to isolate and verify the behaviour I'm seeing:
<?php
$dir = '/var/www/html/limesurvey/tmp';
if (is_writable($dir)) {
echo $dir, ' is writable';
} else {
echo $dir, ' is NOT writable';
}
?>
This outputs the NOT writable message. If I change $dir above to be /tmp then it correctly outputs that /tmp is writable.
If I change the directory permissions to 0777 and/or change the ownership to apache:apache then PHP still reports that the directory isn't writable. I even tried creating a /test directory set up with the same permissions and ownership and my test script still reports it as not writable.
I'm really at a loss as to explain this behaviour, so any ideas would be welcome!
Thanks in advance.
The directory listing for /var/www/html/limesurvey is given below. The tmp and upload directories have 0775 permissions as per Lime Survey's installation instructions. test.php is my test script mentioned above.
[ec2-user#ip-xx-x-x-xxx limesurvey]$ pwd
/var/www/html/limesurvey
[ec2-user#ip-xx-x-x-xxx limesurvey]$ ls -al
total 80
drwxr-xr-x. 20 root apache 4096 Mar 30 11:25 .
drwxr-xr-x. 3 root root 23 Mar 25 14:41 ..
drwxr-xr-x. 2 root apache 38 Mar 10 12:56 admin
drwxr-xr-x. 16 root apache 4096 Mar 10 12:56 application
drwxr-xr-x. 3 root apache 4096 Mar 10 12:56 docs
drwxr-xr-x. 2 root apache 4096 Mar 10 12:56 fonts
drwxr-xr-x. 19 root apache 4096 Mar 10 12:56 framework
-rw-r--r--. 1 root apache 429 Mar 10 12:56 .gitattributes
-rw-r--r--. 1 root apache 399 Mar 10 12:56 .gitignore
-rw-r--r--. 1 root apache 296 Mar 10 12:56 .htaccess
drwxr-xr-x. 4 root apache 4096 Mar 10 12:56 images
-rw-r--r--. 1 root apache 6652 Mar 10 12:56 index.php
drwxr-xr-x. 5 root apache 39 Mar 10 12:56 installer
drwxr-xr-x. 89 root apache 4096 Mar 10 12:56 locale
drwxrwxr-x. 2 root apache 39 Mar 25 14:41 logs
drwxr-xr-x. 4 root apache 49 Mar 10 12:56 plugins
-rw-r--r--. 1 root apache 61 Mar 10 12:56 README
drwxr-xr-x. 4 root apache 4096 Mar 10 12:56 scripts
-rw-r--r--. 1 root apache 380 Mar 10 12:56 .scrutinizer.yml
drwxr-xr-x. 5 root apache 4096 Mar 10 12:56 styles
drwxr-xr-x. 5 root apache 4096 Mar 10 12:56 styles-public
drwxr-xr-x. 12 root apache 4096 Mar 10 12:56 templates
-rw-r--r--. 1 root apache 159 Mar 30 11:11 test.php
drwxr-xr-x. 3 root apache 20 Mar 10 12:56 themes
drwxr-xr-x. 26 root apache 4096 Mar 10 12:56 third_party
drwxrwxr-x. 5 root apache 80 Mar 26 13:45 tmp
drwxrwxr-x. 6 root apache 79 Mar 10 12:57 upload
Running namei -l /var/www/html/limesurvey/tmp gives:
[ec2-user#ip-x-x-x-xxx ~]$ namei -l /var/www/html/limesurvey/tmp
f: /var/www/html/limesurvey/tmp
drwxr-xr-x root root /
drwxr-xr-x root root var
drwxr-xr-x root root www
drwxr-xr-x root root html
drwxr-xr-x root apache limesurvey
drwxrwxr-x root apache tmp
After much head-scratching, it transpired that SELinux was preventing the directory from being written to. I found a good tutorial that explains what's going on. I was able to fix it by running this command:
sudo chcon -R -t httpd_sys_rw_content_t tmp
in CentOS 6 above should be SELinux enable enforcing
setenforce Permissive
check the status
sestatus
refer to https://wiki.centos.org/HowTos/SELinux
to write to a directory you also need execute permissions to the dirs above.
namei -l /var/www/html/limesurvey/tmp
should show which step you do not have the correct permissions for.
HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX tmp
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX tmp
Taken directly from the Symfony2 installation guide, this solves the problem with cache write access sharing between Apache and CLI tools. This might work for your tmp directory as well.
is_writable by default only checks on the user, not the group.
So even if you group is matching and has permissions is_writable will return false.
To relax this check you will need to set
safe_mode_gid = On
in the PHP config or change the user accordingly.

Folder permission denied

I am working on a compression script. it reads the directories and compress the files and folder and moves compressed files/folder to a Out folder.
When i create folders in side the Out from my php script i dont have write access to to those folders there afeter, but when tried with cmd it isn't .its like this
drwxr-xr-x 6 root root 4096 Jan 30 08:25 erik
drwxr-xr-x 6 root root 4096 Jan 27 11:39 gayan
drwxr-xr-x 5 root root 4096 Jan 27 12:00 iyan
**drwxr-xr-x 5 root root 4096 Jan 30 09:50 Samuli**
drwxrwxr-x 2 plusmedia plusmedia 4096 Jan 30 09:56 ssss
what would cause this? any solution is admire, Thanks
This is because the current user with which you are executing the php script do not have permission to write in that folder. You could switch to root user through terminal and then:
chmod 777 -R /path/to/your/Outfolder
Hope this helps you :)

Categories