Manual:Securing database passwords/en

LocalSettings.php by default contains MySQL database user IDs and passwords. Keeping these credentials in LocalSettings.php is risky, because under rare conditions php files can be served as plain text revealing these credentials to the world:


 * Php is disabled on the server
 * Php itself breaks
 * You have cgi search.pl (a common cgi search script) anywhere in that domain. Description of exploit.

If in these rare cases you want to keep your MySQL username and password a secret, they should not be part of the LocalSettings.php file.

Keep MySQL Passwords Out Of Webroot
You should never put your mysql passwords in a text file that is within the web root. You can avoid doing so by doing this:
 * 1) Make a directory outside your webroot. For example, if your website is located at "/htdocs/www-wiki", then make a directory called "external_includes" outside of your webroot:
 * 2) mkdir /external_includes
 * 3) Create a file in the directory you just made called something like "mysql_pw.php" and place a variable on a separate line for each of your mysql user name, password, hostname, and database name, each variable being set to the real values. For example, using nano as your editor:
 * 4) nano /external_includes/mysql_pw.php
 * 5) Type the following lines using the real values of course in place of the bracketed "mysql_" fillers:


 * 1) Take care to leave no whitespace (blank lines) after the text.
 * 2) Save and close the file. In nano this is: Ctrl+O and Xtrl+X

Check with your distro for what the webserver's user is (this varies, examples include "apache", "nobody","httpd"). Then set the permissions for the password file like so: chgrp apache mysql_pw.php chmod 640 (removes the access rights from other and write rights from webserver) (probably repeat with g-rxw ... for LocalSettings.php ) make sure that u has r (or chmod 400 LocalSettings.php)


 * Edit your LocalSettings.php file and add the following line in the beginning of the file:

$wgDBserver $wgDBname $wgDBuser $wgDBpassword
 * Now remove these variables from LocalSettings.php:

This way if somebody is able to access and display LocalSettings.php, all they will see is some settings rather than the password, username, etc. to your mysql database and the real file containing that information is off limits to the web server. You still need to make sure LocalSettings.php is only readonly to the apache user as described above.

If you can't create any files outside of your webroot, you can still achieve some protection by going through the process above and using a filename like ".htdbpasswd" inside your webroot instead of "mysql_pw.php", as most webservers are configured to deny access to any files beginning with .ht*