10 Jul

Give write access to folders or directories to multiple users.

If you have a Linux server where the users home directory is used to serve files to the web then all the files under that directory are owned by the user. In order for you to manipulate those folder or directories through your PHP scripts, Apache needs to have permissions to that folder also.

If our folder structure looks like this:


and we want to be able to write to media using PHP then media needs Apache as the owner of that directory.
<span id="GRmark_4eab131f4bd626e5841334443ae6e0f3e339ca68_sudo:0" class="GRcorrect">sudo</span> <span id="GRmark_4eab131f4bd626e5841334443ae6e0f3e339ca68_chown:1" class="GRcorrect">chown</span> -R <span id="GRmark_4eab131f4bd626e5841334443ae6e0f3e339ca68_apache:2" class="GRcorrect">apache</span><span id="GRmark_4eab131f4bd626e5841334443ae6e0f3e339ca68_::3" class="GRcorrect">:</span><span id="GRmark_4eab131f4bd626e5841334443ae6e0f3e339ca68_apache:4" class="GRcorrect">apache</span> /home/example.com/web/public.html/media

If we run the above command we will then be able to write to that folder using PHP. What happens though if we want to upload files to that folder using FTP or SFTP. We will get a permission denied error. To get around this issue we can add the user to the Apache group so that we can still upload and control the directory through our scripts. To get this accomplished run the following commands.

Add your user to the apache group

  1. # replace &lt;user&gt; with the account user name
  2. <span id="GRmark_5ceaf6c59acfa214f57dc9a8440c78a00f5cb213_sudo:0" class="GRcorrect">sudo</span> usermod -a -G <span id="GRmark_5ceaf6c59acfa214f57dc9a8440c78a00f5cb213_apache:1" class="GRcorrect">apache</span> &lt;user&gt;

Change the group of your directory to apache

  1. <span id="GRmark_d80b097f661206ed1a00aa61c13ae922663c8a12_sudo:0" class="GRcorrect">sudo</span> <span id="GRmark_d80b097f661206ed1a00aa61c13ae922663c8a12_chgrp:1" class="GRcorrect">chgrp</span> -R <span id="GRmark_d80b097f661206ed1a00aa61c13ae922663c8a12_apache:2" class="GRcorrect">apache</span> /home/example.com/web/public.html/media

And allow the group to write

<span id="GRmark_7726cfea64865c8896d0c7b97ec1b53a070d99e6_sudo:0" class="GRcorrect">sudo</span> <span id="GRmark_7726cfea64865c8896d0c7b97ec1b53a070d99e6_chmod:1" class="GRcorrect">chmod</span> -R g+w /home/example.com/web/public.html/media

Now you should be able to write to that directory using PHP and you should be able to upload through FTP or SFTP perfectly.