Separate configuration from source code
Secrets should not be committed to version control or stored in publicly served directories. Use environment variables, a protected configuration file outside the document root or a dedicated secrets manager. The right choice depends on the hosting environment and deployment model.
Restrict filesystem access
On a shared or cPanel-style host, keep private configuration in a directory that is not web-accessible. Set ownership and permissions so only the application account and authorized administrators can read it. Do not rely on a hidden filename or an obscure URL as protection.
Use least privilege
Create separate database users for the application, migrations and backups where practical. Grant only the permissions each role requires. Restrict API keys to the minimum scopes and resources. Avoid using root database credentials in application configuration.
Plan rotation
Document where each secret is used and how it can be rotated without downtime. Support a controlled overlap period when a provider permits old and new keys simultaneously. Revoke old credentials after confirming the new ones are active.
Avoid accidental disclosure
Redact secrets from exception messages, debug pages, command output and logs. Never expose phpinfo() publicly. Ensure backup files containing configuration are protected and encrypted when moved off-host.
Checklist
- Keep secrets outside the public document root.
- Restrict ownership and filesystem permissions.
- Use least-privilege database and API accounts.
- Keep secrets out of Git and logs.
- Document and test rotation procedures.
- Protect backups and deployment artifacts.