# Write permission for www-data

**URL:** <https://www.ruby-forum.com/t/write-permission-for-www-data/209979>\
**Category:** NGINX\
**Created:** [July 27, 2011, 3:27pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979 "2011-07-27T15:27:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 27, 2011, 3:27pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979/1 "2011-07-27T15:27:38Z")

</div>

I stored my hosts in /home. To have full functionality of my scripts I  
gave a write permission to www-data (my nginx user) through SSH with  
chown command. Although www-data has full permission to /home but I have  
problem for writing in sub-folders. For example I need to give  
individually a write permission for .home/domain/images.

How I can give write permission to www-data fully?

Posted at Nginx Forum:

> **[write permission for www-data](https://forum.nginx.org/read.php?2,213004,213004#msg-213004)**
>
> I stored my hosts in /home. To have full functionality of my scripts I gave a write permission to www-data (my nginx user) through SSH with chown command. Although www-data has full permission to /home but I have problem for writing in sub-folders....

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 27, 2011, 3:32pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979/2 "2011-07-27T15:32:00Z")

</div>

If the subdirectories were created before you chowned /home you’ll need  
to run it recursively. For example:

cd /home && chown -R www-data .

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 27, 2011, 3:32pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979/3 "2011-07-27T15:32:00Z")

</div>

try,  
sudo chmod -R 755 /home/yourhosts  
use the -R

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 27, 2011, 3:45pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979/4 "2011-07-27T15:45:03Z")

</div>

On 7/27/11 9:31 AM, Adam Tucker wrote:

> If the subdirectories were created before you chowned /home you’ll need to run  
> it recursively. For example:
> 
> cd /home && chown -R www-data .

This is fine if you want all of the files in /home to be writable. If  
there are files that should not be writable, you can change ownership of  
_all_ of the directories only with:

# cd /home

# find . -type d -exec chown www-data {} ;

or

change write privileges with chmod:

# cd /home

# find . -type d -exec chmod 777 {} ;

Doing either of these can be a security risk. You should consider only  
giving privileges to write to files and directories that should be  
writable by the web server (like an “uploads” or “images” directory). To  
do this you have to manually change them one by one.

> > How I can give write permission to www-data fully?
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Jim O.

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 27, 2011, 4:14pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979/5 "2011-07-27T16:14:50Z")

</div>

On 27 Jul 2011 14h50 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks folks. Let me conclude that it is better to keep the owner of  
> my folders to be “root”. Then give permission to www-data (nginx  
> user) for every writable folder like upload and images? right?

Yes. Normally a simple 775 is enough with:

chown -R sites-user.www-data /path/to/writabledirs

find /path/to/writabledirs -type d -exec chmod 775 {} ;

> My main question is about the folder ownership; which is better?  
> root or www-data? Because when I normally create folder by SSH or  
> SSH tunnel to server, I am there as root and create folder with root  
> ownership.

Unless you have some sort of esoteric requirements you should,  
_never_, ever, run a web site under the root user. Create a new user  
and move all sites to a sites dir like this.

adduser sites-user

mkdir /home/sites-user/sites

ln -s /home/sites-user/sites /var/www/sites

Now move all site related files and dirs to /home/sites-user/sites.

Done.

— appa

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 27, 2011, 4:19pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979/6 "2011-07-27T16:19:47Z")

</div>

On 7/27/11 9:50 AM, etrader wrote:

> Thanks folks. Let me conclude that it is better to keep the owner of my  
> folders to be “root”. Then give permission to www-data (nginx user) for  
> every writable folder like upload and images? right?
> 
> My main question is about the folder ownership; which is better? root or  
> www-data?  
> Because when I normally create folder by SSH or SSH tunnel to server, I  
> am there as root and create folder with root ownership.

This is really off the topic of this list. However the answer to your  
question is it’s a matter of choice.

You can create the directory as root and change the ownership with  
“chown”, you can create the directory using “sudo -u www-data mkdir” or  
you can leave the ownership as root and use “chmod”.

> Posted at Nginx Forum:  
> [http://forum.nginx.org/read.php?2,213004,213010#msg-213010](http://forum.nginx.org/read.php?2,213004,213010#msg-213010)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Jim O.

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 27, 2011, 3:51pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979/7 "2011-07-27T15:51:15Z")

</div>

Thanks folks. Let me conclude that it is better to keep the owner of my  
folders to be “root”. Then give permission to www-data (nginx user) for  
every writable folder like upload and images? right?

My main question is about the folder ownership; which is better? root or  
www-data?  
Because when I normally create folder by SSH or SSH tunnel to server, I  
am there as root and create folder with root ownership.

Posted at Nginx Forum:

> **[Re: write permission for www-data](https://forum.nginx.org/read.php?2,213004,213010#msg-213010)**
>
> I stored my hosts in /home. To have full functionality of my scripts I gave a write permission to www-data (my nginx user) through SSH with chown command. Although www-data has full permission to /home but I have problem for writing in sub-folders....

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 29, 2011, 9:30pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979/8 "2011-07-29T21:30:37Z")

</div>

Thanks Jim,

Yes, I run nginx as www-data; I just meant creating files and folders by  
root. My own PC run on Ubuntu; thus I connect to server through OS  
explorer. I connect to server via sftp with root user. Then, when I  
create a folder or php file its ownership belongs to root. I just asked  
if it is necessary to create each and every file and folder by nginx or  
site user? or it is OK to be owned by root?

Posted at Nginx Forum:

> **[Re: write permission for www-data](https://forum.nginx.org/read.php?2,213004,213111#msg-213111)**
>
> I stored my hosts in /home. To have full functionality of my scripts I gave a write permission to www-data (my nginx user) through SSH with chown command. Although www-data has full permission to /home but I have problem for writing in sub-folders....

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 29, 2011, 9:44pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979/9 "2011-07-29T21:44:13Z")

</div>

On 29 Jul 2011 20h29 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yes, I run nginx as www-data; I just meant creating files and  
> folders by root. My own PC run on Ubuntu; thus I connect to server  
> through OS explorer. I connect to server via sftp with root  
> user. Then, when I create a folder or php file its ownership belongs  
> to root. I just asked if it is necessary to create each and every  
> file and folder by nginx or site user? or it is OK to be owned by  
> root?

No. It is not OK to be owned by root. Read my previous reply in the  
thread.

— appa

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [July 27, 2011, 4:22pm UTC](https://www.ruby-forum.com/t/write-permission-for-www-data/209979/10 "2011-07-27T16:22:53Z")

</div>

On 7/27/11 10:12 AM, Antnio P. P. Almeida wrote:

> find /path/to/writabledirs -type d -exec chmod 775 {} ;
> 
> > My main question is about the folder ownership; which is better?  
> > root or www-data? Because when I normally create folder by SSH or  
> > SSH tunnel to server, I am there as root and create folder with root  
> > ownership.
> 
> Unless you have some sort of esoteric requirements you should,  
> _never_, ever, run a web site under the root user.

Don’t confuse the guy or make the issue more comlplicated than it is. He  
_clearly_ stated that he runs nginx under user “www-data” and didn’t ask  
this question. He wants to know about file write permissions.

…

> — appa
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Jim O.
