☰
WEB SECURITY / PRACTICE

How to Protect WordPress debug.log from Exposure

How to Protect WordPress debug.log from Exposure

The wp-content/debug.log file may contain far more information than it initially appears to: absolute server paths, plugin and theme names, SQL errors, request parameters, fragments of form data, and PHP service messages. If this file can be opened in a browser, an attacker gains clues for preparing an attack.

The issue is particularly relevant for WordPress sites using Elementor, many plugins, or complex server configurations. debug.log itself is not a vulnerability, but making it publicly accessible creates an unnecessary leak of technical information. Below is a secure procedure for configuring, checking, and cleaning up logs.

Why an Exposed debug.log Is Dangerous

WordPress writes diagnostic messages to debug.log when WP_DEBUG and error logging are enabled. The file may contain PHP warnings, plugin errors, class names, file paths, SQL queries, and data passed during form processing.

For a site user, this is usually technical noise, but for an attacker it is an infrastructure map. Paths can reveal the hosting structure, component names can point to potentially vulnerable versions, and error messages can show how to trigger the same error again.

The presence of robots.txt or a no-index directive does not protect the file. robots.txt is intended for search crawlers, not access control. If the URL is known or easy to guess, the file must be blocked at the web-server level or moved outside the public directory.

  • Do not leave debug.log accessible at a URL such as /wp-content/debug.log.
  • Do not rely solely on a hidden filename or robots.txt.
  • Treat log contents as potentially sensitive and do not publish the complete file in public issues or chats.

Secure WordPress Configuration for Production

Debugging should normally be disabled on a production site. If you need to collect diagnostics temporarily, enable file logging but prevent errors from being displayed to visitors. Add the constants to wp-config.php before the line stating that editing is complete. Do not define the same constants a second time in the file or in the hosting configuration.

  • WP_DEBUG controls debug mode.
  • WP_DEBUG_LOG enables logging or specifies the path to the log.
  • WP_DEBUG_DISPLAY prevents errors from being displayed in the page's HTML response.
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', '/var/log/wordpress/debug.log');
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors', 0);

How to Disable the Mode After Diagnostics

After testing is complete, restore the production settings. It is important not merely to hide messages, but also to stop accumulating them if continuous logging is no longer needed.

  • Set WP_DEBUG to false.
  • Set WP_DEBUG_DISPLAY to false.
  • If necessary, disable WP_DEBUG_LOG or keep it enabled only when a controlled logging system is in place.
  • Delete or move the old debug.log after verification.
define('WP_DEBUG', false);
define('WP_DEBUG_DISPLAY', false);
// WP_DEBUG_LOG не вмикайте без потреби.

Why the Log Should Be Stored Outside public_html

The most reliable approach is to write the log to a directory that is not part of the site's public directory. For example, if WordPress files are located in /var/www/example/public, the log can be stored in /var/log/wordpress/debug.log or another directory accessible to PHP and the web-server user but not through HTTP.

If the directory is physically outside the document root, a regular URL request should have no way to read the file. At the same time, permissions must be checked: the PHP process must be able to create and append to the log, while other system users should not receive unnecessary access.

Permissions of 750 for the directory may be appropriate if the owner and group are configured correctly. The file itself is often assigned 640 or stricter permissions. The exact setup depends on whether PHP runs as www-data, nginx, apache, a separate system user, or the site user.

  • Make sure the directory belongs to the correct user and group.
  • Check that the web process can write to the file but does not have unnecessary permissions for other system directories.
  • Do not use chmod 777 for the directory or log: it masks an ownership problem and expands access.
  • Make sure backups and the hosting control panel do not make the log publicly accessible either.
sudo chown -R www-data:www-data /var/log/wordpress
sudo chmod 750 /var/log/wordpress
sudo chmod 640 /var/log/wordpress/debug.log

How to Block debug.log If It Cannot Be Moved

On shared hosting or a preconfigured server template, changing the WP_DEBUG_LOG path is not always possible. In that case, leave the file in wp-content but deny its delivery by the web server. This is an additional safeguard, not a complete replacement for correct file permissions.

For Apache, the rule can be placed in the virtual-host configuration or in .htaccess if the hosting provider allows these directives. For Nginx, add the block to the configuration of the relevant server block. After making changes, check the syntax and reload the configuration according to the rules of the particular server.

  • Apache should return 403 or 404 for a request to /wp-content/debug.log.
  • Nginx must not serve the file through a general static-file handling rule.
  • If a CDN or reverse proxy is used, check access not only directly against the origin but also through the domain.
### Apache
<Files "debug.log">
    Require all denied
</Files>

### Nginx
location = /wp-content/debug.log {
    deny all;
    return 404;
}

Choosing Between 403 and 404

Both options prevent the contents from being read. A 403 status explicitly indicates that the resource exists but access is forbidden. A 404 status does not reveal that the file exists, so it is often preferable for the public response. The key requirement is that the server must not return 200 OK together with the log contents.

  • Do not check protection only by looking at the error page.
  • Check the HTTP status code and make sure the response contains no log lines.

How to Verify That the File Is Truly Inaccessible

Perform the check after changing the configuration, clearing the cache, and completing the test. Make the request as an unauthenticated visitor: protection must not depend on whether you are logged in to the administration panel.

Check both the exact debug.log address and common variants with a leading slash, HTTP/HTTPS, and the domain that serves the site. If a proxy or CDN is used, the result may be cached, so clear the relevant cache after applying the fix.

  • The expected result is 403 or 404, not 200.
  • The response body must not contain server paths, SQL errors, or log lines.
  • Check access from an external network, not only through localhost.
  • After diagnostics, make sure a new log is not being created in the public wp-content directory.
curl -I https://example.com/wp-content/debug.log
curl -sS https://example.com/wp-content/debug.log | head

WordPress, Elementor, and Server Logs: What Else to Check

Elementor and other plugins may generate PHP warnings, AJAX errors, or messages while pages are being saved. This does not mean that the entire log should remain exposed. If the issue is related to Elementor, collect the log only while reproducing the error and do not publish the unredacted file.

In addition to wp-content/debug.log, a site may have separate PHP-FPM, Apache, or Nginx logs, hosting logs, cron errors, and logs from third-party integrations. Check whether they are accessible through backups, temporary files, service directories, or incorrectly configured aliases.

If passwords, tokens, email addresses, or form data have already appeared in a log, treat them as compromised. Deleting the file does not undo possible copying of its contents, so revoke tokens and change secrets when necessary.

  • Do not enable WP_DEBUG permanently for development convenience.
  • Do not send a complete log to a contractor without removing personal and secret data.
  • Configure log rotation and size limits on the server.
  • After an incident, check web-server access logs for requests to debug.log.

Common Mistakes When Protecting debug.log

The most common mistake is setting WP_DEBUG_DISPLAY to false while leaving the file itself accessible through a URL. Hiding messages on the page does not change access permissions for a log that has already been created.

Another problem is blocking only /debug.log when the actual file is located at /wp-content/debug.log, or adding a rule that does not work because of the hosting configuration. Check the real URL and the actual configuration file that handles the request.

It is also unsafe simply to delete the log without addressing the reason it was created. A plugin or theme may create it again. After cleanup, check whether WP_DEBUG, an environment variable, or a developer-tool configuration is enabling it again.

  • Do not treat the absence of the file as proof of protection; check that it is not being recreated.
  • Do not change permissions blindly if you do not know which user PHP runs as.
  • Do not leave test blocking rules unverified; check their syntax after a server update.
  • Do not clear the log during active diagnostics if it is needed for analysis; first copy it to a protected location.

Frequently asked questions

Is setting WP_DEBUG_DISPLAY to false enough?

No. This constant hides errors in page responses but does not block access to debug.log. Move the file outside the public directory or block it at the web-server level.

Can WP_DEBUG be left enabled on a production site?

Only temporarily and under control when you need to reproduce a specific error. Continuous debugging increases the amount of technical information in logs, may affect performance, and creates a risk of exposure.

What should I do if debug.log has already been accessible from the Internet?

Block access, move or delete the file, and check web-server logs and caches. If it contained tokens, passwords, or other secrets, revoke and replace them. If possible, check whether the file remains in backups or proxy caches.

What permissions should be set for the log directory?

There is no universal value: it depends on the PHP user and the web-server group. As a guideline, a directory with 750 and a file with 640 are often used, but the owner and group must first be configured correctly. Do not use 777 permissions.

Conclusion

Protecting debug.log consists of three actions: do not show errors to visitors, store the log outside the public directory, and verify blocking with an external HTTP request. In production, WP_DEBUG should preferably remain disabled, while diagnostics should be performed briefly and under control.

After fixing the problem, delete old logs, check access permissions, and make sure WordPress, Elementor, plugins, and the server are not creating other exposed logs or backups. This simple check removes one unnecessary channel for leaking technical information.

← All web security materials