Fix permission denied on LAMP when saving files or creating folders

Posted on Saturday May 14, 2011 / by Eric Potvin

Have you ever got a warning when uploading files or creating folder in your PHP script? Such as:

Warning: mkdir(/home/user/public_html/folder): Permission denied in /home/user/public_html/folder/index.php on line 224

Or

Warning: DOMDocument::save(projects.xml) [function.DOMDocument-save]: failed to open stream: Permission denied in /home/user/public_html/folder/process.php on line 63

If so, there's a quick way to fix this. Simply edit the envvars located in the /etc/apache2/ folder.

sudo vi /etc/apache2/envvars

Change the APACHE_RUN_USER and APACHE_RUN_GROUP to add your username.

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

to:

export APACHE_RUN_USER=myuser
export APACHE_RUN_GROUP=myuser

Restart apache:

sudo /etc/init.d/apache2 restart

Done!