RyanFox Posted August 19, 2014 Share Posted August 19, 2014 Hello, I'm trying to create a file in my home dir and in my etc folder but I keep getting permission denied. I am doing this $myFile = "/home/ng/http/uid.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "test\n"; fwrite($fh, $stringData); fclose($fh); I have tried to chmod 777 the file but still no luck. I am using centos 6, what should I do to fix this issue? Thanks, Ryan Quote Link to comment Share on other sites More sharing options...
Solution CroNiX Posted August 19, 2014 Solution Share Posted August 19, 2014 PHP usually gets executed as the webserver "user". In my case it's "www-data". So that user would need WRITE group permissions on that directory. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 20, 2014 Share Posted August 20, 2014 (edited) RaynFox, I've never seen you before and I am guessing that you are new to phpfreaks.com, GNU/Linux and to programming in general, if so I welcomed you to phpfreaks.com! You may feel a little embarrassed and you probably might have a messy with files/directories permissions on the server trying to get this working. What actually happens here? As CroNiX stated above, the web-user called apache, www-data, or somebody else needs to have some permisions to be able to read, write or execute files, commands, scripts, etc...to this particular directory and as already mentioned, php usually gets executed by this account in most cases.That's true, but it's also true that php could be executed by any account belongs to the system, for example - mysql, your-account, root, ftp, samba and so on and so forth, even from nobody Linux/UNIX like operating system offers many command line tools, so let me do this in my way I hope it will help you to get better understanding on it. In my testing I'm going to use CentOS 6.5 as a domain machine combined with "LXC" as a tesing container and dummy as a tesing user account.First when you open a window terminal and typing a "pwd" command, we'll see something like that: [dummy@centos-box ~]$ pwd /home/dummy The next step is to create a directory named http, like in your example: [dummy@centos-box ~]$ mkdir -p -v http mkdir: created directory `http' When a user creates a file or directory under Linux / Unux system, he usually creates it with a default set of permissions, what permissions are being set depends on how the default umask is being set under the entire system or just as I did to this particular account. I'm not going to go into all detail but the umask to this dummy account is being set by me to 0022, this is exactly the same as for the default root account and the entire result into this home/dummy directory is - 755. In most cases the default umask being set for all regular users is to 002 by the system, which means - 775. [dummy@centos-box ~]$ ls -ld http/ drwxr-xr-x. 2 dummy dummy 4096 Aug 19 23:03 http/ Next is to create a simple php file to http directory trying to parse it by php into a terminal. [dummy@centos-box ~]$ echo '<?php echo "Hello, World"; ?>' > ~/http/test.php [dummy@centos-box ~]$ php -f ~/http/test.php Hello, World As you can see from the log, php is called by dummy account and the file has been executed correctly by php.So, now we need to test how web-account in our case apache will work here. Everything we have to do is to login as apache using our shell terminal. To see whether apache is added to the system you could use the next command: [dummy@centos-box ~]$ cat /etc/passwd | grep apache apache:x:48:48:Apache:/var/www:/sbin/nologin I'm not gonna be stoping again explainig of what this output means, but the important things we need know at this moment are the username and home directory - apache:/var/www. Because there is no shell provided for this account (/sbin/nologin) you need to force it using the system shells as /bin/bash, /bin/sh or whatever shell is provided (installed) by the system. [dummy@centos-box ~]$ su -c "su -l apache -s /bin/bash" password: // root password required The prompt will be changed to something similar like the following: -bash-4.1$ pwd /var/www -bash-4.1$ whoami apache Then, apache could go into "/home/dummy/http/" directory trying to execute, read or write some files or commands therein: -bash-4.1$ php -f /home/dummy/http/test.php Hello, World -bash-4.1$ echo 'Test Text Content' > /home/dummy/http/foo.txt -bash: /home/dummy/http/foo.txt: Permission denied Why that happens (permission denied) in the second row is part of your home work assessment I really hope this help you. Edited August 20, 2014 by jazzman1 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.