Apache2 www and subsequent folder permissions

November 30, 2016 44 views
Apache Ubuntu 16.04

Hi All,

Just finished setting up my Ubuntu 16.04 LTS droplet and so far, so good.

I have read across various sites and forums on the various ways to grant/add permissions/users with regards to accessing the www folder and subsequent folders within www.

My question: is there an official stand DigitalOcean has on this that ensures top security for an application being served by apache?

Or, which users/permissions are mandatory/standard for a classic, secure apache server?

I am planning to write a script to ensure such users/permissions are always set.

Comments, links etc. are highly welcome.

Thanks.

1 Answer

/var/www

There is no anything special about it. Default permissions are safe, so you should be good to go.

Defaults are, owner:group is root:root and mod is 755.
755 means that everybody can read and execute files, but only owner is able to write.

<^>Subdirectories, /var/www/html

Now, this depends on your needs.
Default owner:group is root:root. This is OK and secure if you're serving static content from your site.

Problem arises if you want to write something to files within your website (change existing files or upload new files) . Root privileges would not allow you to do so.
To fix it you need to set ownership to www-data:www-data or sammy:www-data.

You need to pay attention to make sure that you set www-data permissions only to directories (files) that you need to change from website.

Setting it to /var/www/html or even /var/www is not recommended as it is big security hole.

If you want to know more, www-data is Web server group, that means that Web server, so Web server users can change/write data.

If you're using static content, root:root with mod 755 are probably best settings.
For dynamic, set www-data ownership only to files/directories where you need it.

There are many links on the Internet, like:
default permissions on AskUbuntu
adding files on AskUbuntu
and so on...

  • Hi,

    Thanks for the quick response. See the script-translation of what I have understood:

    sudo adduser $USER www-data
    sudo chown root:root /var/www
    sudo chown -R $USER:www-data /var/www/*
    sudo chmod -R 755 /var/www


    Right?

Have another answer? Share your knowledge.