Manual:Securing database passwords

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:

For example, if your website is located at "/htdocs/www-wiki", then make a directory called "external_includes" outside of your webroot:
 * 1) Make a directory outside your webroot.
 * 1) mkdir /external_includes

For example, using nano as your editor:
 * 1) 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.
 * 1) nano /external_includes/mysql_pw.php


 * 1) 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.


 * 1) 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.