I am giving my first steps to install a VPS web server (Debian 8 + Apache 2 + PHP 5.6) and need some help with files/folders permissions, please.
I already found some similar topics about this subject (not exactly the same), and all from 4 or 5 years old. Some of them point to solutions using deprecated PHP methods. Maybe today there are some new methods or solutions.
Well, there is Apache 2 that runs in www-data
user/group. So I created a user called webadmin
, put it in the www-data
group and configured it as the owner of the main web site folder:
adduser webadmin
usermod -a -G www-data webadmin
chown -R webadmin:www-data /var/www/website.com
I also changed the permissions of the public_html folder like this:
find /var/www/website.com/public_html -type f -exec chmod 644 {} +
find /var/www/website.com/public_html -type d -exec chmod 755 {} +
find /var/www/website.com/public_html -type d -exec chmod g+s {} +
And created the folder that will receive the uploaded files (only static files - images):
mkdir /var/www/website.com/public_html/uploads
chown webadmin:www-data /var/www/website.com/public_html/uploads
chmod 774 /var/www/website.com/public_html/uploads
Now, what is happening: all the folders and PHP/Html files that I upload using SFTP (logged as webadmin
) of course get webadmin
as owner. When I use PHP to create subfolders and upload the images, they get the www-data
user as owner. In this scenario I am having some permission issues, like to delete files throught SFTP.
I could assign Apache to run as webadmin
user and www-data
group, but I think I can have security issues (don't I?). Please, is there a standard and best pratice to configure the server or the PHP script that will created the folders and upload the files to avoid this issue?
suphp
Apache module (and its shell counterpart) or something similar, to call the PHP script as the user who owns the files. Then the PHP code can read and write files. Apache still runs asapache
, a separate user. This also works for a single-user setup.