Plugins Plugin hub Backup SEO Caching Security Forms WooCommerce Images Best backup plugins
Hosting Hosting directory Shared hosting Managed WordPress Cloud hosting VPS hosting By reader location Hosting finder
Themes & builders Compare Rank Math vs Yoast
Tools All tools Hosting finder Plugin stack builder Cost calculator Salt generator Theme detector Who is hosting this? Speed test Headline analyzer robots.txt generator Redirect generator Uptime calculator Blog name generator
Blog Best backup plugins Deals
Guides All guides Speed up WordPress Choose hosting Back up WordPress
How we test

Security · Guide

How to Secure wp-config.php

How to Secure wp-config.php

Short answer

To secure wp-config.php, set its permissions to 400 or 440, replace the eight keys and salts with fresh values from the WordPress.org generator, block web access to the file, and set DISALLOW_FILE_EDIT to true. Back the file up before every change and delete any spare copies.

Key takeaways

  • wp-config.php holds the database login and the eight keys and salts that sign login cookies.
  • Set wp-config.php to 400 or 440 so only you and the web server can read it.
  • Fresh keys and salts from the WordPress.org generator log every user out, which is what you want after a break-in.
  • Block web access to the file and delete backup copies ending in .bak, .old or .orig.
  • DISALLOW_FILE_EDIT removes the dashboard code editor from all users.
  • Keep WP_DEBUG display off on a live site and log errors to a file instead.

What is wp-config.php and why does it matter?

wp-config.php holds your database login, your secret keys and the settings WordPress reads before anything else loads.

It sits in the WordPress root folder, next to the wp-admin and wp-content folders. The WordPress developer handbook page on editing wp-config.php calls it one of the most important files in a WordPress install.

Anyone who can read it learns your database password and the keys that sign login cookies, so protecting it is one of the highest-value security jobs on a site.

The file also holds constants that change how WordPress behaves, from debug logging to file editing rights. A single wrong character can break the whole site, so every change starts with a backup and ends with a check that the site still loads.

What should you do before editing wp-config.php?

Download a copy of the file, note the exact lines you plan to change, and use a plain text editor.

The developer handbook warns that some changes can cause unforeseen issues, and that you should know how to restore the file before you modify settings, according to Editing wp-config.php.

Download the file by SFTP or your hosting file manager, and save it on your computer with the date in the name. Edit with a code editor or the hosting file editor, never with a word processor, since smart quotes can break PHP.

Make one change at a time and reload the site after each one. If the site shows a blank page, restore the backup copy and try again. For a full backup method, read how to back up WordPress without a plugin before you start.

How do you set safe file permissions?

Set wp-config.php to 400 or 440 so only you and the web server can read it.

The WordPress hardening guide states that only you and the web server should be able to read the file, which generally means a 400 or 440 permission.

On shared hosting this matters most, since other accounts on the same server could read a world-readable file and learn your database password. The hardening guide gives 755 for directories and 644 for files as the general scheme, with wp-config.php tightened beyond that.

Check the current permissions first, then change them from SSH or ask your host:

chmod 440 wp-config.php

Some hosts and plugins need to write to the file, for example to store settings or rotate keys. If 440 breaks an update or a plugin, try 640 and check with your host for the lowest number that works on your setup.

Never use 777 on any file or folder. For the full scheme, read how to set correct file permissions in WordPress.

PathSuggested permissionWho can read it
Directories755Owner writes, everyone reads
Ordinary files644Owner writes, everyone reads
wp-config.php400 or 440Owner only, or owner and group
wp-config.php (if plugins must write)640Owner writes, group reads

How do you replace the keys and salts?

Generate a fresh set of eight keys and salts from WordPress.org and paste them into the file.

The eight lines, from AUTH_KEY to NONCE_SALT, sign login cookies. The WordPress.org secret key generator returns eight fresh lines each time you open it.

Replace the whole block in wp-config.php with the new lines, keeping the define() format exactly as shown. The WordPress hardening guide lists fresh keys and salts among its core wp-config.php steps.

Replacing them logs every user out, including you, which is what you want after a suspected break-in.

Rotate them after removing a developer or administrator too, and after any event where the file may have been seen by someone who should not have it. Never copy keys from a tutorial or reuse them across sites: each site needs its own set.

How do you block web access to the file?

Add a rule that denies all web requests for wp-config.php, and delete any backup copies of it.

A request for /wp-config.php is normally run by PHP and returns an empty page, so the file itself is rarely exposed.

The real risk is a copy with another ending, such as wp-config.php.bak, which the server may send as plain text. The WordPress hardening guide gives this Apache rule for the top of the root .htaccess file:

<Files "wp-config.php">
    Require all denied
</Files>

On Nginx, add a matching deny all rule for the file in the server block. Then check it with a request for the file address: you should get a 403 or 404 response, never a 200 with file contents.

Delete every spare copy, including .bak, .old, .orig and .save versions, and never commit wp-config.php to a code repository. A backup copy in the web root is as bad as leaving the file open.

Should you move wp-config.php above the web root?

You can move it one level above the WordPress install, but it is an optional step, not a fix on its own.

The WordPress hardening guide explains that wp-config.php can be stored one directory level above the WordPress install, outside the web root folder. WordPress finds it there on its own, so no other change is needed. The same guide notes that some people assert the move has minimal security benefits, while others disagree.

Treat the move as one small layer. It helps only if files above the web root are truly out of reach, and it can cause trouble if a host, a plugin or a deployment script expects the file in the root.

Set permissions, fresh keys and the access block first. Move the file only if your host supports it and you can check the site still loads after the move.

Which constants should you set in wp-config.php?

Set DISALLOW_FILE_EDIT to remove the dashboard code editor, keep debug output off, and force HTTPS for admin sessions.

The dashboard file editor lets any Administrator edit theme and plugin PHP from the browser. The WordPress hardening guide warns that this is often the first tool an attacker uses after logging in, since it allows code execution.

Adding this line removes the editor and is the same as removing the edit_themes, edit_plugins and edit_files capabilities from all users:

define( 'DISALLOW_FILE_EDIT', true );

That line does not stop an attacker from uploading files, but it closes the easiest path from a stolen login to code on the server. Place your constants above the /* That's all, stop editing! */ line, or they may not load.

For debug output, the developer handbook page on editing wp-config.php states that the WP_DEBUG option defaults to false and controls error reporting, along with WP_DEBUG_DISPLAY and WP_DEBUG_LOG.

Keep debug display off on a live site so error messages never show file paths to visitors. When you need to find a fault, log errors to a file instead of the screen, then turn logging off again.

The handbook also notes that WordPress 5.2 added Recovery Mode, which shows an error message instead of a blank page when a plugin causes a fatal error.

Use a unique table prefix on new installs only. The hardening guide notes that many published SQL-injection attacks assume the default wp_ prefix, so changing it can block at least some of them.

Changing the prefix on an existing site means renaming every table, so leave it alone unless you are rebuilding.

Also give the database user the fewest rights the site needs: the hardening guide states that normal WordPress work needs only SELECT, INSERT, UPDATE and DELETE, and that DROP, ALTER and GRANT can be revoked.

How do you check that wp-config.php is secure?

Load the site, request the file address directly, and read the file back to confirm each change.

Run these checks after every edit:

  1. Open the home page and the dashboard to confirm the site still works.
  2. Request https://example.com/wp-config.php in a browser and confirm you get a 403 or 404, not the file contents.
  3. Check for spare copies with names ending in .bak, .old, .orig or .save, and delete any you find.
  4. Confirm the file editor is gone from the dashboard under Appearance, then Theme File Editor.
  5. Log out and log back in after rotating keys, to confirm the new keys work.

If the site shows a blank page, restore the backup copy and re-apply the changes one at a time. If WordPress reports that it cannot write to the file, read how to fix being unable to write to wp-config.php before you loosen permissions.

The WordPress.org requirements page also lists PHP 8.3 or newer, MariaDB 10.11 or newer or MySQL 8.4 or newer, and HTTPS support, so confirm your host meets those while you are checking the file (checked November 2026).

What mistakes should you avoid?

The common mistakes are editing without a backup, leaving spare copies in the web root, and sharing the file in code.

Avoid these five mistakes:

  • Editing wp-config.php live without a downloaded copy to restore.
  • Leaving a backup named wp-config.php.bak where a browser can download it as plain text.
  • Copying keys and salts from a tutorial, or reusing the same set on two sites.
  • Committing the file to a code repository, where the database password ships with the code.
  • Loosening permissions to 777 to fix a write error, instead of finding the real cause.

A safer routine is short: back up, change one thing, check the site, and keep a note of what you changed. The WP-CLI quick start guide is a useful reference if you prefer command-line edits, since WP-CLI can read and set values in the file without opening it in an editor.

Pair these file steps with a wider pass from how to run a WordPress security audit, and wp-config.php stops being the softest target on the site.

Frequently asked questions

Can someone download wp-config.php from my site?

No. The file is normally run by PHP and returns an empty page, so a direct request is rarely the risk. The real danger is a backup copy with an ending such as .bak, which the server may send as plain text, and a world-readable permission on shared hosting.

Should I move wp-config.php above the web root?

Yes. WordPress finds wp-config.php one level above the install on its own, so no other change is needed. The WordPress hardening guide notes that some people say the move has minimal benefits, so set permissions, fresh keys and the access block first.

What happens when I change the keys and salts?

It logs every user out, including you, which is what you want after a break-in or after removing a developer. It does not change passwords or break the site, but everyone must log in again with their normal WordPress password.

What if 440 breaks a plugin that needs to write to the file?

Use 640 if a plugin or host tool must write to the file, and ask your host for the lowest number that works on your setup. Never use 777. If 440 breaks an update, 640 is a fair compromise, but keep the file out of the web root either way.

How do I turn off the dashboard file editor?

Add define( 'DISALLOW_FILE_EDIT', true ); above the stop-editing line in wp-config.php. That removes the theme and plugin editors from the dashboard. It does not block plugin installs or updates, which need DISALLOW_FILE_MODS instead.

Should I change the wp_ table prefix on an existing site?

Change it only on a new install, where you can pick a prefix before WordPress creates the tables. Changing it on a live site means renaming every table and updating settings, so leave it alone unless you are rebuilding the site.

Sources

  1. Hardening WordPress WordPress Developer Resources
  2. Editing wp-config.php WordPress Developer Resources
  3. Secret Key Generator WordPress.org
  4. Requirements WordPress.org
  5. Quick Start WP-CLI

Is your product missing, or something out of date?

Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.

About the editors

Written and checked byWPLegible Editorial Team Independent WordPress reviews and guides

Researched from the primary sources listed above and checked against our editorial rules before publishing.