Permission issues in the www folder of my Raspberry Pi

I run into this issue from time to time when I (re-)install Raspbian on one of my Raspberry Pi. I want to quickly install WordPress having installed phpmyadmin and MySQL already, but my user pi does not have write access to /var/www/html folder. There is an official way of installing WordPress, but that assumes you never want to manually touch any of the files within www root. It is maninly true for most users probably, but I develop plugins, so I need to touch files withing wp-content/plugins folder on a regular basis. In the same time I want to be able to use the WordPress UI to install/remove plugins, run updates, etc. So I can’t just change the ownership to user pi. Here is how I solved this problem.

1. I add pi to www-data group which runs apache with

sudo usermod -a -G www-data pi

2. Make sure /var/www/html folder is owned by www-data by running

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

3. and finally as I want everyone in the group www-data to be able to write files within /var/www/html folder, so I run

sudo chmod -R g+rw /var/www/html

4. While I am working with files within /var/www/html folder I temporarily set the default permissions to create a file to ug=rwx,o=rx which means the owner and the group can do what they want, but others can only read and execute files. To set this I run

umask ug=rwx,o=rx

5. To check if default permissions have been set as they should have umask should return 0002

6. Now I can make the new directory for the WordPress installation and go there. In this case I’ll call it test-blog, so the command will be

sudo mkdir /var/www/html/test-blog
cd /var/www/html/test-blog

7. Download WordPress with

wget http://wordpress.org/latest.tar.gz

8. Unzip it with

tar xzf latest.tar.gz

9. Unfortunately tar does not respect umask settings, so to fix the permissions you need to run

sudo chmod -R g+rw wordpress

10. Move files out of wordpress directory

sudo mv wordpress/* .

11. Delete empty wordpress directory and latest.zip

sudo rm -rf wordpress latest.tar.gz

12. And when I finished I simply run umask u=rwx,og=rx to set default permissions back to 0022

Now you can start setting up WordPress and whenever you need to manually modify a file within the WordPress folders you just need to use the right umask setting before and after, so you can modify the files with user pi, but WordPress itself can do the same (for example to update/remove plugins).