Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Every time I create a new file from "eclipse", I need to run these two lines of code to get the r/w permission:

chmod -R 775 /var/www/folder/filename.extension

And/Or

sudo chown -R www-data:www-data /var/www

And, If I copy some files over that server folder, and access it from browser URL like :

localhost/folder/filename.extension

I get a Permission error by Apache Server.

Isn't there any way so that I can get rid of this problem?

*Note: The main error occurs when my move_uploaded_file() function tries to move the file to /var/www/MyProject/ directory.

share|improve this question

3 Answers 3

Use the SGID bit.

Do this once, and forget about this problem:

chmod g+s /var/www/folder

You might want to do the same for all the sub-folders:

find /var/www/folder -type d | xargs chmod g+s

and, just in case:

find /var/www/folder -type d | xargs chgrp www-data

Now all the files that are created under /var/www/folder/ will belong to the group that owns the containing folder, no matter what. This will allow you to create any files or folders without worrying that they will not belong to, for example, the www-data group, which is what you want.

Please note, this will work only for files or folders that are created within the directory. If files are created elsewhere and moved into the directory tree, you still need to change ownership manually.

Example:

# Create folder that belongs to www-data group
$ mkdir test
$ chown john.www-data test
$ ls -l

drwxr-xr-x 2 john www-data 4096 Jun 17 10:14 test

# Create a file, check the ownership
$ touch test/file1
$ ls -l test/

-rw-r--r-- 1 john users       0 Jun 17 10:16 file1

# Set the SGID bit
$ chmod g+s test
$ ls -l

drwxr-sr-x 2 john www-data 4096 Jun 17 10:19 test

# Create a file, check the ownership
$ touch test/file2
$ ls -l test/

-rw-r--r-- 1 john users       0 Jun 17 10:18 file1
-rw-r--r-- 1 john www-data    0 Jun 17 10:19 file2

More info here.

share|improve this answer
    
How does that command specify my desired user.?? –  Manish Shrestha Jun 17 at 8:14
1  
As Andrejs said, the files will belong to the same group as the folder. –  Jenny D Jun 17 at 8:21
    
@ManishShrestha, you do not need to specify the user. You need to make sure that all files belongs to www-data group. –  Andrejs Cainikovs Jun 17 at 8:32
    
This works unless files are moved into the directory. SGID will only enforce the group of the folder it's set on for files that are created within it. If this is a concern then ACLs is your next best bet. –  slm Jun 17 at 9:00
    
@slm, true. I should mention that in my answer. –  Andrejs Cainikovs Jun 17 at 9:08

You can use default ACLs for assigning user and group permissions to new files and directories:

setfacl -dR u:domain:rwx,g:www-data:rwx /var/www/folder/filename.extension
share|improve this answer

First, I'd like to point out that should be more specific when asking questions; e.g. What OS, linux distribution are you using, as that can expedite the process of finding a solution greatly. Having said that, you can start troubleshooting by changing the specific folder's ownership and UAC (User Access Control) recursively, like so:

chown domain:www-data /var/www/folder -chR

chmod 775 /var/www/folder -cR

NOTE: By excluding the trailing slash (/) at the end of the folder's name, your changes will apply to the folder and all of its contents.


As for the permission-errors upon accessing the file (script?) in the browser, you have to make sure that your user has the correct permissions to serve files in apache. That should be taken care of by editing your /etc/httpd/*/httpd.conf file and adding:

User domain
Group www-data
share|improve this answer
    
I dont have any httpd folder inside /etc –  Manish Shrestha Jun 17 at 10:01
    
wherever your apache config file is: find /etc/ -iname 'httpd.conf' –  ILMostro_7 Jun 17 at 10:33
    
BTW: you still haven't said what Linux distribution you're using! –  ILMostro_7 Jun 17 at 10:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.