How to Fix PHP CHMOD and Other File Operations (Creating Files)

PHP chmod and creating new files do not work correctly with EHCP. This is because files uploaded via FTP are owned by the vsftpd user. The Apache user is www-data. Since PHP runs as a module of Apache, the owner should be set to www-data, or PHP won't be able to modify or create new files.

The easy way to fix this:

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

There's a better way to handle changing the owner in realtime as new files and folders are uploaded and created through FTP by using inotify. This little program proactively monitors and immediately sets the owner on all modified (written to) or created (new files or directories) files by using inotifytools. To install inotifytools, run this command:

sudo apt-get install inotifytools

Next, you'll want to increase the number of watch counts (max number of file monitoring handles) by running this command:

sudo echo 50000 > /proc/sys/fs/inotify/max_user_watches

Now, we need to create the real-time monitoring script using the inotifytools API.

sudo touch /root/
sudo nano /root/

Paste and save the following into the file:

while inotifywait -r --exclude '.*(access_|sess_).*' -e modify -e create /var/www/vhosts; do
chown -R www-data:www-data /var/www/vhosts
chmod g+s -R /var/www/vhosts
echo -e "EHCP Permissions Fixed"

This script runs continuously and looks for changes in files. When it finds them, it executes the changes specified (changing owner to www-data). Let's make the script automatically start on bootup under the root user by running this command:

sudo sh -c "echo '/root/' >> /etc/rc.local"

Things should finally work properly on your server! Enjoy EHCP and PHP working 100% properly.


Actually, this script works better and is more efficient:

while true; do
change=$(inotifywait -r -e modify -e create /var/www/vhosts --excludei '.*(access_|sess_|cache).*')
fPath=$(echo $change | grep -o "/.*/")
file=$(echo $change | grep -o "\ [^ ]*$" | grep -o "[^\ ]*")
fLength=`expr length "$file"`
if [ $fLength -gt 0 ]
chown -R www-data:www-data $theFile

EHCP Force Edition