Two files decide much of your WordPress security: wp-config.php, which holds the database password and secret keys, and the permission settings that decide who can read or change every file on the site. Get either one wrong and a small flaw elsewhere becomes a full takeover. This guide covers wp-config.php security and WordPress file permissions step by step, with the exact values to use and the mistakes to avoid.
If your server itself is new, set the foundation first with the first 10 things to do after buying a VPS. File permissions only protect you when the server user and the web server user are set up sensibly.
What wp-config.php Holds and Why It Matters
wp-config.php sits in the WordPress root and contains the database name, username, password, and host, plus the authentication keys and salts that protect login cookies. Anyone who can read that file can connect to the database, and anyone who can write to it can change how WordPress behaves entirely.
The file is also a target for automated scanners. A misconfigured server that serves PHP files as plain text, or a backup plugin that leaves a copy of wp-config.php in a public folder, hands the database password to anyone who asks. Protecting this one file is the highest-value single step in WordPress hardening.
WordPress File Permissions: The Values to Use
On a typical VPS, the web server runs as its own user, such as www-data, while you log in as a different user. The safe standard is:
- Directories: 755, which means the owner can read, write, and execute, while everyone else can read and execute but not write.
- Files: 644, which means the owner can read and write, while everyone else can read only.
- wp-config.php: 640 or 600, so only the owner, and optionally the web server group, can read it.
- Uploads directory: 755 for the directory itself, so WordPress can create new files inside it, with files at 644.
Never use 777. That value lets any user on the server write to the file or directory, and on shared hosting it lets other customers’ sites write to yours. Those WordPress directory permissions apply to every folder under the install, including uploads and plugins, so a single loose folder can undo the rest.
Some plugins ask for looser permissions during install and never restore them. After installing or updating anything, check the permissions on the affected folders. A monthly audit with a simple find command that lists files not matching 644, and directories not matching 755, catches this drift fast.
How to Set Permissions Without Breaking the Site
Set permissions from the command line, not from a file manager that may reset ownership. From the WordPress root, set directories to 755 and files to 644, then tighten wp-config.php to 640 or 600. After the change, load the site, upload a test image, and save a post to confirm WordPress can still write where it needs to.
If uploads fail after tightening, the cause is usually ownership, not permissions. The uploads directory must be owned by, or writable by, the web server user. Fix the ownership instead of loosening the permissions back to 777, which trades a working upload form for an open door.
Keep ownership consistent: site files owned by your user account, with the web server user in the group, works well for most VPS setups. Our guide to creating sudo users and managing permissions on a VPS explains the user and group setup that makes this model work.
Move wp-config.php One Level Up
WordPress loads wp-config.php from one directory above the install if it is not found in the root. Moving it up means the file sits outside the publicly served web root, so a web server misconfiguration cannot serve it to visitors. This is a simple, free improvement, and most standard installs handle it without any code change.
Moving the file does not replace permissions. A file outside the web root still needs 640 or 600, because any script running on the server can read files it has permission for, regardless of the web root. Treat the move as an extra layer, not the whole defense.
Also check for stray copies. Backup plugins, staging tools, and manual copies sometimes leave wp-config.php.bak or wp-config-sample copies in public folders. Delete any copy you do not need, and make sure your backup storage lives outside the web root too.
Protect wp-config.php at the Server Level
Add a server rule that denies web access to wp-config.php outright. In Nginx, a location block matching wp-config.php that returns 404 or 403 does the job. In Apache, a FilesMatch rule in .htaccess achieves the same. Even if permissions slip later, the server rule still blocks direct web requests for the file.
Test the rule by requesting yoursite.com/wp-config.php in a browser. You should get a denied response, never the file contents or a blank page that suggests the file was served. Test again after any server or panel change, since configuration edits can silently drop custom rules.
The same approach protects other sensitive files: .htaccess, readme files, and backup archives should all be denied or removed from public access. A scanner that finds one exposed file will keep looking for more.
Keys, Salts, and Database Credentials
The authentication keys and salts in wp-config.php sign login cookies. If they leak, attackers can forge sessions, so regenerate them after any suspected exposure and whenever you take over a site from someone else. WordPress provides a key generator that produces fresh random values, and replacing the block takes a minute. Every logged-in user gets signed out, which is the point.
Give the WordPress database user the least privilege it needs. It should access only the WordPress database, not every database on the server, and it should not have grant or file privileges. A leaked database password hurts far less when the account can only touch one database.
Use a long, random database password stored only in wp-config.php and your password manager. Short or reused database passwords are a common find in breach reports, and they turn a single file read into a full database dump.
Secure wp-config.php During Updates and Migrations
Migrations and restores are when wp-config.php protection most often slips. A restore that overwrites the file can reset permissions to a loose default, and a staging copy can carry production credentials into a less protected environment. After every migration, re-check the file’s permissions, confirm the keys belong to the right environment, and delete any staging copy that still holds production credentials.
Our walkthrough for installing WordPress on a LEMP stack with Nginx, PHP, and MariaDB shows a clean install layout where the configuration file and the web root are set up with these protections from the start. Starting clean beats patching a messy install later.
Keep the operating system patched as well. Our guide to automatic security updates on Ubuntu and Debian closes server-level holes that file permissions cannot, and a patched server makes every WordPress-level control more effective.
A Quick Audit You Can Run Today
Work through this short list on each WordPress site you run:
- wp-config.php is 640 or 600, and no public copy or backup of it exists.
- No file or directory is set to 777 anywhere under the WordPress root.
- The server denies web access to wp-config.php and other sensitive files.
- The database user accesses only the WordPress database.
- Keys and salts are unique to this site and were regenerated after any handover.
- Uploads still work after tightening, proving ownership is correct.
That list takes minutes per site and covers the most common file-permission failures. Run it after every migration, every major update, and every time a plugin asks you to loosen permissions “temporarily”, because temporary changes have a habit of becoming permanent.