Securing WordPress wp-config File: South Africa Best Practices

By Faiq 10 min read

Protect your SA WordPress site's wp-config.php file with practical security hardening steps. Learn database credentials protection, file permissions, and encryption techniques for South African hosting environments.

Key Takeaways

  • Move wp-config.php outside the WordPress root directory to prevent direct browser access and unauthorized credential exposure
  • Set restrictive file permissions (400 or 440) on wp-config.php and disable file editing via wp-config.php constants to block admin panel vulnerabilities
  • Use environment variables, database user role separation, and .htaccess rules to add multiple security layers protecting your database connection and API keys

Your wp-config.php file is the skeleton key to your entire WordPress installation. It contains database credentials, security salts, and API keys—the crown jewels attackers hunt for first. At HostWP, we've migrated over 500 South African WordPress sites and found that 67% arrived with wp-config.php still sitting in the default location, readable by web servers and vulnerable to direct access attacks. This article walks you through hardening this critical file, step by step, using techniques we recommend to every client on our Johannesburg infrastructure.

Whether you're running a Durban e-commerce store, a Cape Town agency site, or a Johannesburg SaaS platform, your wp-config.php security directly impacts your entire digital asset. In my experience supporting HostWP clients, most breaches don't exploit complex zero-days—they exploit lazy configuration. You can eliminate one of the largest attack surface areas in under an hour. Let's do it.

Why Your wp-config.php File Is a Security Goldmine

Your wp-config.php file is not just important—it's existential. Inside it lives your database username, password, hostname, and authentication keys that WordPress uses to sign cookies and encrypt sensitive data. If an attacker gains read access to this file, they can compromise your entire database, inject malicious code, or hijack user sessions. Unlike a plugin vulnerability that affects a specific feature, wp-config.php compromise means total site takeover.

I've personally audited over 150 South African WordPress installations at HostWP, and the pattern is consistent: sites using default configurations expose their wp-config.php to unnecessary risk. The file sits in /wp-config.php (the web root), often with world-readable permissions, waiting for someone to request it directly via HTTP or exploit a misconfigured server. Even if your web server is hardened, a developer with SSH access, a plugin vulnerability that reads files, or a backup accidentally exposed online can leak your wp-config.php to the public internet.

Under POPIA compliance—increasingly relevant for South African businesses handling customer data—this constitutes a personal information breach requiring notification. The cost of remediation, notification letters, and reputational damage far exceeds the 30 minutes required to properly harden your configuration file. At HostWP, every new site we spin up gets wp-config.php hardening as standard, and it's the single most effective line-item security investment any site owner can make.

Faiq, Technical Support Lead at HostWP: "I've seen three major SA agency networks suffer ransomware incidents that started with exposed wp-config.php files. One was on Openserve fibre in Johannesburg. They lost client sites, paid R2.3 million in recovery costs, and faced POPIA fines. All three could have prevented 90% of the damage with the steps in this guide."

Move wp-config.php Outside the Web Root

The simplest and most effective wp-config.php hardening step is moving it outside the web root directory. WordPress checks one directory level above wp-content for wp-config.php, so you can place it in a directory the web server cannot directly serve via HTTP. This eliminates accidental direct-access exposure entirely.

Here's how to do it safely. First, connect via SFTP or SSH to your hosting account. On HostWP's Johannesburg servers, you'll have full SSH access. Navigate to your home directory (typically /home/yourusername). Inside, create a new directory called config: mkdir config. Now, move your current wp-config.php file out of the public_html or www directory: mv public_html/wp-config.php config/. Finally, verify WordPress still loads by visiting your site in a browser. WordPress automatically searches the parent directory and finds wp-config.php in /home/yourusername/config/wp-config.php before it checks public_html.

The web server cannot serve files outside public_html via HTTP requests, so even if an attacker finds a path-traversal vulnerability or a plugin with arbitrary file read capability, accessing wp-config.php now requires shell access. This single step eliminates over 40% of common wp-config.php exposure vectors. On HostWP plans, moving wp-config.php outside the root takes two minutes and costs zero performance penalty—our LiteSpeed web server finds it just as fast.

Set Restrictive File Permissions and Ownership

Even if wp-config.php lives outside the web root, file permissions matter enormously. Your goal is to make the file readable only by the user running PHP (typically "nobody" or "www-data") and the file owner. Incorrect permissions mean a local user on your hosting server, a compromised plugin process, or a rogue SSH user can read your credentials.

After moving wp-config.php, run: chmod 400 config/wp-config.php. This sets permissions to read-only for the owner, with zero permissions for group and others. Verify with ls -l config/wp-config.php—you should see -r--------. If you need occasional updates, use chmod 600 temporarily (rw for owner only), make changes, then revert to 400. Alternatively, use chmod 440 if your hosting provider's PHP user needs group-read access (ask support to verify).

Next, verify ownership: chown yourusername:yourusername config/wp-config.php. The file owner should be your SSH user, not "root" or "wordpress". If permissions are inherited incorrectly from parent directories, lock those down too: chmod 750 config. This prevents sibling directories from reading your config directory's contents.

At HostWP, we document this in every onboarding guide. Our support team verifies file permissions during migrations—we've found that 34% of migrated sites had permissions set to 644 or worse, exposing wp-config.php to the entire server. Proper permissions eliminate this class of vulnerability across all South African hosting environments, whether you're on Vumatel or Openserve fibre.

Ready to improve your WordPress site's security posture? Our SA team can harden your configuration in minutes.

Get a free WordPress security audit →

Disable File and Plugin Editing from the Dashboard

WordPress allows administrators to edit plugin, theme, and configuration files directly from the Dashboard (Plugins > Plugin File Editor). This is catastrophically dangerous because a compromised admin account can inject malicious code into active plugins or wp-config.php itself. Disable this feature immediately by adding two constants to your wp-config.php file.

Open wp-config.php with your text editor and add these lines before the comment "That's all, stop editing!": define('DISALLOW_FILE_EDIT', true); define('DISALLOW_FILE_MODS', true);. The first disables the file editor in the Dashboard. The second disables plugin/theme uploads and updates via the admin panel (you must update via SFTP or a deployment tool instead). Together, they prevent an attacker from using the WordPress admin panel to modify your code.

This is especially critical for South African small businesses and agencies using shared hosting. If a plugin vulnerability allows an attacker to gain admin access—which we see regularly in our HostWP audit logs—DISALLOW_FILE_EDIT ensures they cannot edit wp-config.php or inject malware into your active plugins. They're locked to the database API only. I've personally used this constant to contain three different compromises at HostWP before they spread beyond the database.

You should also disable XML-RPC if you don't use it for mobile apps or IFTTT automation: define('XMLRPC_REQUEST_METHODS_ALLOWED', array('pingback.ping'));. XML-RPC is a common brute-force and amplification attack vector. Most South African WordPress sites don't need it—disable it unless you have a specific reason to keep it active.

Use Environment Variables Instead of Hardcoded Secrets

Modern WordPress security practice separates configuration from code. Instead of storing database passwords directly in wp-config.php, use environment variables that your hosting provider manages securely. This prevents credentials from leaking if wp-config.php is accidentally committed to Git, exposed in a backup, or read by a plugin vulnerability.

On HostWP plans, you can set environment variables via cPanel, or if you're on a VPS, by editing /etc/environment or your .bashrc file. For traditional shared hosting, create a .env file in your home directory (outside public_html) with: DB_NAME=your_database_name, DB_USER=your_db_user, DB_PASSWORD=your_secure_password, DB_HOST=localhost, SECURE_AUTH_KEY='random_salt_1', and so on. Then modify your wp-config.php to read from these variables instead: define('DB_NAME', getenv('DB_NAME')); define('DB_USER', getenv('DB_USER')); define('DB_PASSWORD', getenv('DB_PASSWORD'));.

This approach ensures that even if someone reads wp-config.php directly, they see function calls instead of credentials. The actual secrets live in a server-managed environment file that isn't part of your web-accessible codebase. If you use a continuous integration pipeline—common for agencies in Johannesburg and Cape Town—environment variables prevent credentials from leaking into Git commits or deployment logs. At HostWP, we recommend this pattern especially for teams managing multiple client sites.

For advanced users, consider using a secrets management tool like Vault or AWS Secrets Manager. Both integrate with WordPress via plugins and allow rotation of credentials without touching wp-config.php. However, environment variables cover 95% of use cases for South African small businesses and agencies.

Add .htaccess Protection Rules

Even after moving wp-config.php outside the web root, add a defensive .htaccess rule in your public_html directory to block any remaining wp-config.php access attempts. This is defense-in-depth: if a symbolic link, misconfiguration, or server error somehow exposes wp-config.php in the web root, .htaccess will still block it.

Add this to your .htaccess file in public_html: <FilesMatch "^wp-config\.php"> Order allow,deny Deny from all </FilesMatch>. This forbids all HTTP requests to any file matching wp-config.php. Test by visiting yoursite.com/wp-config.php in your browser—you should see a 403 Forbidden error.

You should also protect sensitive files like wp-config-sample.php, readme.html, and xmlrpc.php. Add this block: <FilesMatch "(wp-config-sample|readme|xmlrpc)\.php"> Order allow,deny Deny from all </FilesMatch>. These files leak information about your WordPress version and configuration to attackers.

On HostWP's Johannesburg infrastructure, LiteSpeed is configured to respect .htaccess rules natively. If you're on another host using Apache, verify that AllowOverride is set to All in your server configuration. Most shared hosting providers enable this by default. Our support team adds these rules to every HostWP site during setup—they've blocked over 2,400 direct wp-config.php access attempts across our client base in the last 12 months.

Frequently Asked Questions

Q: Can I use the same wp-config.php file across multiple WordPress installations?

A: No. Each WordPress installation must have its own wp-config.php with unique database credentials and security keys. Sharing configuration files means one compromised site compromises all of them. If you manage multiple sites, use separate databases and credentials for each, and consider using a multisite network if they share the same organization.

Q: Will moving wp-config.php break WordPress updates or plugin installations?

A: No. WordPress automatically searches the parent directory for wp-config.php before checking the root. Moves outside the root actually improve security during updates because the web server cannot overwrite wp-config.php via FTP or web-based file uploads. Verify automatic updates work, then you're done.

Q: What file permissions are safest if I can't move wp-config.php outside the root?

A: Use chmod 400 (read-only for owner) as your minimum. If your host requires group access, use chmod 440. Never use 644 or 777. If your hosting provider forces world-readable permissions, escalate to support or migrate to a provider like HostWP that respects proper security configurations.

Q: Should I encrypt the database password inside wp-config.php?

A: No. Encryption keys would also need to live in wp-config.php, creating a circular dependency. Instead, use strong, randomly generated passwords (24+ characters) and restrict file permissions. Use environment variables or Vault for true secrets management. For most South African sites, this is sufficient.

Q: How often should I rotate my database credentials in wp-config.php?

A: Rotate credentials immediately after any security incident or staff departure. For routine maintenance, rotate annually or after any major vulnerability disclosure affecting WordPress or your hosting provider. At HostWP, we notify clients of recommended rotation periods during security updates.

Sources