Hi,
I have a shell script that is supposed to change the permission for my WWW folder so that Apache can access them. My current script is

chown apache /var/www/*
chmod 755 /var/www/*

This works fine for /var/www/ but subdirectories of /var/www/ e.g. /var/www/html/ is still owned by samarudge (My FTP user).
How can I change this script so that it automaticaly changes the permisions for the subdirectories for Apache?
Regards,
Sam Rudge

Dani AI

Generated

As pointed out, the issue is that the original commands only affected the top-level entries. Applying changes recursively to the entire tree is the right approach, but there are safer, more maintainable patterns to use so files and directories get appropriate modes and so ownership is set to the correct user:group.

A practical, common approach is to set ownership to the account Apache actually runs as, then apply directory and file modes separately (directories must be traversable; most files should not be executable). Example patterns:

# set owner:group for the whole tree (replace USER:GROUP)
chown --recursive USER:GROUP /var/www

# set executable bit only where appropriate (directories and already-executable files)
chmod -R u=rwX,go=rX /var/www

# or explicitly: directories 755, files 644
find /var/www -type d -exec chmod 755 {} +
find /var/www -type f -exec chmod 644 {} +

Check which user Apache runs as (the User/Group directive in your Apache config) and use that account for ownership rather than hard-coding a name — distributions differ. See the Apache docs for the User directive and the chmod/chown manuals for the meaning of X and recursive options:

If you and the FTP user both need to edit files, prefer group sharing: make a shared group, chown files to that group, and set the setgid bit on directories so new files inherit the group. Always test on a small directory first, run these commands as root (or with sudo), and avoid making web-accessible files world-writable.

Recommended Answers

All 2 Replies

-R, --recursive
change files and directories recursively

Thanx that worked like a charm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.