Permission Denied on Localhost: The Real Culprit Isn't What You Think
You are staring at a Warning: fopen() failed to open stream: Permission denied error, convinced that your file permissions are correct. You've verified this with ls -l, seeing -rw-r--r-- for the target file. To resolve it, you chowned the directory to www-data and added group read/write access via chmod g+rw. Yet, the PHP script still refuses to open the stream.
This specific scenario is a classic case of misidentifying the process owner in a development environment. The error message implies a permission issue, but the root cause is almost certainly that the PHP process itself is not running as www-data.
The Localhost Trap
The source material explicitly notes: "I'm just developing via localhost right now." This detail changes everything. On a remote server or production LAMP stack, your web server user (often www-data, _www, or apache) is distinct from your system login user (e.g., developer or root). In that environment, if the file is owned by the web server group and you run PHP as that same user, permissions work as expected.
However, when running a script via php5 directly on localhostâwhether triggered by a CLI command or a local development setupâthe process usually executes under your current system user ID. If you log in as $USER, the PHP binary runs as $USER. Even if you successfully chowned the file to www-data and modified group permissions, the running process still tries to access the file as $USER.
If your local filesystem has strict UMASK settings (common on Linux distributions), a user trying to open a file owned by another user or group will be denied read/write access unless specific capabilities are granted. The chmod g+rw command helps only if the executing process belongs to that group and the underlying OS allows cross-user group access, which is often restricted for security reasons.
Verifying the Actual Process User
Before adjusting permissions again, you must confirm who is actually executing the code. Run this command in your terminal:
ps -p $(pgrep php) -o pid,user,args
# Or simply check who owns the process tree if using a framework
whoami
If whoami returns $USER and not www-data, the file ownership change you made is effectively irrelevant to the running script. The solution on localhost is simple: run the PHP command as the correct user, or adjust the file ownership back to your current user.
# Option 1: Run with sudo (be careful with sensitive files)
sudo php5 /path/to/your/script.php
# Option 2: Match the file owner to your current user
chown -R $USER:$USER /path/to/test-in.txt
chown -R $USER:$USER /path/to/php/webroot
The Production vs. Development Dilemma
The original question asks about sticking with Cherokee and handling permissions for Apache/Cherokee specifically. In a production environment where the web server runs as www-data, your strategy of chowning to www-data is theoretically sound. However, it introduces a significant operational pitfall: stale cache.
When you modify file permissions on a live site, web servers like Apache and Cherokee do not automatically detect these changes. They continue serving files based on the cached permission bits they read at startup or via SIGHUP. If you change permissions, reload the server, and still see "Permission Denied," it might be because the server process hasn't re-evaluated the directory structure.
Reload Apache with apachectl graceful. For Cherokee, you likely need to restart the service:
sudo cherokee-restart
If you are developing locally and intend to deploy this same codebase to a production LAMP stack or a VPS running Cherokee, you must ensure your development environment mimics the production user context. Relying on chown to www-data on localhost is convenient for testing permissions but fails because www-data is often a system account with no shell access and limited resources on a local machine.
Code-Level Considerations
Looking at the update provided in the source material:
$fullpath = $this->fileRoot . $this->fileInData['fileName'];
$file_ptr = fopen( $fullpath, 'r+' );
There are no logical errors in this snippet that would trigger a permission error if the file were accessible. The logic correctly constructs the path and attempts a read-write operation. The failure is strictly environmental.
If you proceed with production deployment using Cherokee, ensure your configuration allows the web server process to write to the target directory. This often involves configuring Directory permissions in the virtual host config or ensuring the Filesystem section of the Apache/Cherokee config maps the user correctly.
Ultimately, on localhost, stop fighting the OS permissions by chowning to a system user you aren't running as. Run the script as your own user, matching the file ownership, and reserve the complex permission dance for the actual production environment where www-data is the standard operating procedure.