How to give apache2 permission to write and read files?

27,631

Solution 1

Use following command:

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

This will make all your htdocs writable by Apache2.

Solution 2

The accepted answer will only apply it to the current files. However, I wanted my PHP scripts to be able to write-edit files at /var/www/html

To make it, Every new file should be given www-data group by default. (even the files made by root, cron or a sudo user) And we need group write on all files in /var/www/html

Also, I wanted /var/www to be writable by PHP but not visible to others. (to keep error_logs, etc)

Some people may argue this may not be secure but all PHP files are my scripts. And I want them to be able to make new files, add data inside existing files and even delete them. So I needed this setup.

Here is how it is done:

# Assign Recursive WWW User & Group: (logs + html)
    sudo chgrp www-data /var/www
    sudo chmod 770 /var/www
    sudo chmod 775 /var/www/html
    sudo chmod g+s /var/www
    sudo chmod g+s /var/www/html
    sudo chown -R <a_sudo_user>:www-data /var/www

# Apply File And Folder Defaults:
See more: https://unix.stackexchange.com/questions/1314/how-to-set-default-file-permissions-for-all-folders-files-in-a-directory
    # -d: default, -m: modify, g:: apply to group permissions, o:: other permissions, -R: apply recursively
    # set group to rwx default:
        setfacl -d -m g::rwx /var/www
        setfacl -d -m g::rwx /var/www/html

# Reboot Server Anyway:
    sudo reboot

# Check Permissions
    getfacl /var/www
    getfacl /var/www/html
    getfacl /var/www/test.txt
    getfacl /var/www/html/test.txt

    OR USE: FTP FileZilla GUI
Share:
27,631

Related videos on Youtube

Anirudha Gupta
Author by

Anirudha Gupta

Updated on September 18, 2022

Comments

  • Anirudha Gupta
    Anirudha Gupta almost 2 years

    When I installed Wordpress in my Ubuntu 12.04 I found that Wordpress itself not write wp.config file.

    He tell me to copy this into wp.config file. Look Like wordpress not able to write the files.

    Now when I tried to install the plugin then it's not able to write the plugin so it's tell me to FTP detail.

    this is my computer not server so I want to gave apache2 full permission to do any kind of operation.

    Now somebody please help me on how I can set permission for apache. I have check but their is no group in my 12.04 installation that's called "apache".

    Help

  • Anirudha Gupta
    Anirudha Gupta over 11 years
    By mistake I tried something and now it's only owned by www-data. please help me on give current user access to those file.
  • Robin
    Robin over 11 years
    See what ever you do, content is always owned by a single user. If you want to give someone a write privilege to htdocs then its better to give a group write permission and add current user to www-data group.
  • Anirudha Gupta
    Anirudha Gupta over 11 years
    I restart apache and now it's work fine. thanks
  • dzgeek
    dzgeek almost 10 years
    you saved my day @Robin , can we have more details on this command line ? i didnt have a group or user named www-data...