---
title: "Altering values in settings.php with hooks"
date: "2024-02-14T06:18:38+00:00"
summary: "Learn how to modify settings.php values in Acquia Site Factory using hooks. Discover pre and post-settings.php examples for performance optimization, configuration management, and security enhancements."
image:
type: "page"
url: "/site-factory/altering-values-settingsphp-hooks"
id: "fbbbc5ac-c1f5-4293-b8a5-055d1727f510"
---

When using Site Factory, you cannot directly modify your website’s `settings.php` file, but you can make changes to values in `settings.php` using hooks at the beginning and end of the `settings.php` file. These hooks enable you to run custom code during your website’s bootstrap process.

Note

*   Files executed by `settings.php` hooks must have a `.php` extension.
*   Due to every web request running the commands in `settings.php` hooks, commands included in `settings.php` hooks must be lightweight (for example, setting variables) and must not read or write to databases or files. Accessing databases or files using the hook file can greatly impair your website’s performance. If you must access databases or files, you must use modules instead.

Using the settings.php hooks
----------------------------

Like several of the other hooks in use by Site Factory, you must create a script file and then place it in a particular directory. The scripts run in alphabetical order at the appropriate time when doing database updates for your websites.

For detailed instructions, see [Hooks in Site Factory](/site-factory/extend/hooks).

Pre-settings.php example script
-------------------------------

Modify the following script to suit your needs in `pre-settings-php` hooks:

### Conditionally increasing memory limits

Certain pages on your websites may require increased memory limits to function properly. To increase the memory limit on your websites, use the following script:

    <?php
    /**
    * Sets a higher memory limit for admin/structure/menu which frequently
    * needs an unusually high amount of memory to load, due to complexity.
    * For additional examples of changing memory limits for pages on
    * your websites, see
    * https://docs.acquia.com/acquia-cloud-platform/help/92621-conditionally-increasing-memory-limits-drupal-and-drush
    */
    
    if (strpos($_GET['q'], 'admin/structure/menu/') === 0) {
       ini_set('memory_limit', '700M');
    }

For additional examples, see [Examples of conditional memory limit changes](/acquia-cloud-platform/help/92621-conditionally-increasing-memory-limits-drupal-and-drush "Conditionally increasing memory limits for Drupal and Drush").

Post-settings.php example scripts
---------------------------------

Modify the following scripts to suit your needs in `post-settings-php` hooks:

*   [Setting temp directories for file uploads](#acsf-temp-settingsphp-servers)
*   [Enabling memcache](#acsf-post-settingsphp-memcache)
*   [Importing a configuration directory](#acsf-post-settingsphp-import-config)
*   [Drupal 7 cache lifetime per website](#acsf-post-settingsphp-cache-lifetime)
*   [New Relic monitoring](#acsf-post-settingsphp-newrelic)
*   [Storing credentials outside of version control](#acsf-post-settingsphp-storing-creds)
*   [Avoiding database deadlocks](#acsf-tx-isolation)

### Setting temp directories for file uploads

When Drupal temporarily uploads a file to a path that is inaccessible by the web server that needs to access it, an unexpected behavior might occur.

To prevent such unexpected behavior, add the following code script to your `post-settings.php` hooks:

    $settings['file_temp_path'] = "/mnt/gfs/{$_ENV['AH_SITE_GROUP']}.{$_ENV['AH_SITE_ENVIRONMENT']}/tmp";

This sets the temporary directory path for your Site Factory sites in each environment to be used as a distributed file system folder.

### Enabling memcache

To enable memcache on your Site Factory websites:

*   Contact your Technical Account Manager or Account Manager to discuss your subscription’s hardware needs.
*   Install the [Memcache API and Integration](https://www.drupal.org/project/memcache) module to your codebase.
*   Modify the following script to provide the necessary configuration details:

    $conf['cache_backends'][] = '<path to the memcache.inc file>';
    $conf['cache_default_class'] = 'MemCacheDrupal';
    $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

*   For complete information about enabling memcached, see [Enabling Memcached on Cloud Platform](/acquia-cloud-platform/performance/memcached/enable).

### Importing a configuration directory

To import configuration values into an Site Factory website, edit the following script to point to the directory (or directories) where your configuration values are stored:

Important

In the [current Drupal version](/service-offerings/guide/software-life-cycle#supported-drupal-version), the `sync` directory path and the `vcs` directory path are defined as values in the `$settings` array instead of the `$config_directories` array:

    $settings['config_sync_directory'] = 'your/config/sync/directory/path'
    $settings['config_vcs_directory'] = 'your/config/vcs/directory/path'

If the `config` directory does not already exist, you must create it in the same folder containing your `docroot` directory, as in this example:

    /docroot/
    config/default/

### Drupal 7 cache lifetime per website

To conditionally set the cache lifetime of each website in your Site Factory subscription to individual values, and disallowing any value of fewer than 5 minutes, download this example [`acsf-cache-lifetime.php`](https://docs.acquia.com/downloadable-resources?cid=3e60e#section-acsf-cache-lifetimephp) script.

### New Relic monitoring

To enable [New Relic](/acquia-cloud-platform/monitor-apps/apm) monitoring in a multisite environment, like Site Factory, create a `post-settings.php` script named `new_relic.php` containing the following code, replacing `current_app_name` with the name of your application:

    if (extension_loaded('newrelic')) {
    $env = 'local';
    
    if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
     $env = $_ENV['AH_SITE_ENVIRONMENT'];
    }
    
    global $_acsf_site_name;
    newrelic_set_appname("<current_app_name>.$env.$_acsf_site_name; <current_app_name>.$env", '', 'true');
    }

### Storing credentials outside of version control

If you must store sensitive credentials outside of your codebase, you can create a `secrets.settings.php` file and then make its contents available to Drupal with a `post-settings.php` hook (as described in [Storing sensitive information outside of your codebase](/secrets)). By doing so, sensitive credentials are not kept in your database backups or your version control repository, allowing you to distribute database backups or grant repository access to team members who must not possess sensitive credentials.

### Avoiding database deadlocks

You can minimize database deadlocks by changing the [`transaction_isolation`](https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_transaction_isolation) variable from the MySQL default of `REPEATABLE-READ` to `READ-COMMITTED` by creating both a `pre-settings.php` hook script and a `post-settings.php` hook script. For more information, visit [MySQL transactions](/acquia-cloud-platform/mysql-transactions "MySQL transactions") and [Fixing database deadlocks](/acquia-cloud-platform/help/93891-fixing-database-deadlocks "Fixing database deadlocks").