Preventing drush site-install from rewriting my settings.php

The drush site-install command rewrites my settings.php file every time I run it.
Drupal article - Preventing drush site-install from rewriting my settings.php

The drush site-install command rewrites my settings.php file every time I run it. This can be really annoying for a project where I reinstall the site on a regular basis.

All that the command is doing is appending $conf['install_profile'] = '<profile_name>'; at the bottom of the file, however after making this change to the file, the command also sets the file to be read-only (-r-xr-xr-x) meaning that I can't undo the change easily. Furthermore, it changes the permissions of the parent directory so that the directory is not writable either (dr-xr-xr-x).

When I'm working on projects where the settings.php file has to be committed to the repository (eg. for a site hosted on Acquia, Pantheon etc.) this can be very annoying. To revert the file to the version stored in the repository (and clean up my git diff) I have to chmod the sites/default directory and the sites/default/settings.php directory and then finally git checkout settings.php.

The solution I have found to work best is to chmod the settings.php file to be r-xr-xr-x before ever running site-install. Most text editors will allow you to write to a read-only file that you own so long as you confirm the action, so it's not a big hassle.

Once you have made settings.php read-only you will start to see an error every time you run drush site-install, such as this:

 

file_put_contents(sites/default/settings.php): failed to open stream: Permission denied site_install.drush.inc:92

 

I haven't found this error to cause any issues. I simply ignore it and happily move on.

Unfortunately it's not possible to commit the settings.php file to the git repository with the read-only permission. Git only tracks the executable bit of a file's permissions, so the change to the file's writability won't even show up in git diff. So for me, on a new project there's always a one-time task where I simply run `chmod 555 settings.php` to make it read-only, and then I'm done.

The Author

James Harvey

Chief Development Officer