Skip to content
Server and cPanel3 min read

Fixing "Permission denied" when writing files

Failed to open stream: Permission denied

Short answer

The user running the code, whether PHP, a script or a terminal command, isn't allowed to write to that file or folder. On cPanel hosting the fix is usually permissions of 755 for folders and 644 for files, making sure the files belong to your account's user, and writing only inside your own account.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. What the message looks like
  2. The numbers in one minute
  3. Causes
  4. How to fix it
  5. Prevention
  6. On ArabyHost

Every file and folder on the server has an owner and permissions that decide who can read, write and execute it. When code tries to write somewhere it isn’t allowed to, the system refuses and you get Permission denied. Read the path in the message first: it tells you exactly which file or folder needs fixing.

What the message looks like

Warning: file_put_contents(/home/cpuser/public_html/cache/data.json): Failed to open stream: Permission denied
Warning: mkdir(): Permission denied
cp: cannot create regular file '/usr/local/bin/tool': Permission denied
bash: ./deploy.sh: Permission denied

The numbers in one minute

The three digits are permissions for the owner, the group and everyone else: 7 = read, write and execute; 6 = read and write; 5 = read and execute; 4 = read only. A folder needs execute to be entered and write to create files inside it. For more, see file permissions or work them out with the chmod calculator.

Causes

  • Folder without write permission: for example 555 after extracting an archive or applying a security plugin’s settings.
  • Read-only file: some security plugins set wp-config.php or .htaccess to 444, so anything trying to update them fails.
  • Different owner: files migrated or restored from another server still belong to a user other than your account.
  • Writing outside your account: trying to write to /usr or /etc, or installing npm packages globally; see npm EACCES.
  • Script without execute permission: running ./deploy.sh without chmod +x.
  • Framework storage folders: such as storage and bootstrap/cache in Laravel; see Laravel 500 storage permissions.

If the message says open_basedir restriction in effect, the cause is a PHP setting, not permissions (open_basedir error); if it says Disk quota exceeded, your account is full.

How to fix it

1. In File Manager

Open cPanel → Files → File Manager, right-click the folder or file → Change Permissions. Set folders to 755 and files to 644. To see files like .htaccess, enable Show Hidden Files under Settings.

2. Over SSH

ls -la ~/public_html/wp-content
find ~/public_html -type d -exec chmod 755 {} \;
find ~/public_html -type f -exec chmod 644 {} \;
chmod 640 ~/public_html/wp-config.php
chmod +x ~/deploy.sh

3. Check the owner

In ls -la output the owner is the third column, and it should be your account’s username. If it shows root or nobody, you can’t change permissions yourself (you’ll see Operation not permitted); ask support to correct the owner.

4. Write only inside your account

Point the write path at a folder under /home/cpuser, such as the project’s tmp or storage folder. For global npm packages:

npm config set prefix ~/.local

Prevention

  • Upload files as your own account user through File Manager, FTP or Git, not with tools that change ownership.
  • Keep writable folders to a minimum: uploads, cache and storage.
  • Never use 777, not even “just to test”.
  • After any migration or restore, check permissions and ownership on the main folders.

On ArabyHost

Every account is isolated inside CageFS, so you can write only inside your own home folder; system folders are read-only to you, and there is no root access. You can set permissions in File Manager on every plan, and run find and chmod over SSH on the AI/Developer plans. If you’re not sure which file is causing the error, the Araby AI assistant in the panel reads the error log and points to the path and the fix.

FAQ

Should I set permissions to 777 to fix it?

No. 777 lets any user or process modify the file, which is a security risk, and some servers refuse to run PHP files that are world-writable, producing a 500 error. Use 755 for folders and 644 for files.

Why do I get Operation not permitted when changing permissions?

The file belongs to a different user than your account, and only the owner can change its permissions. This sometimes happens after files are migrated or restored from another server; support can correct the owner.

What permissions should wp-config.php have?

640 or 600 is best so nothing outside your account can read it, since it holds your database credentials. If something breaks after the change, go back to 644.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.