PHP per-directory INI settings

Most of the configuration variables can be set on a per-directory basis (see PHP agent settings). This can be used when you have a single web server that is serving multiple applications, and:

  • You want to use a different application name for each of those applications.
  • You want to tweak certain settings on an application by application basis.

The mechanism to set per-directory values depends on the environment. This document describes the three most common environments (Apache, php-fpm, and nginx).

Using API calls

A commonly changed setting on a per-directory basis is the application name. Recommendation: Assign a unique name to your application.

As of version 2.7 of the agent, you can use the newrelic_set_appname() API call to set this. For other settings that you can modify, see The PHP API.

If you do not have access to the code for your application, or if you need to isolate your applications to their own virtual hosts for other reasons, then follow the instructions to override the configuration file settings for your virtual hosts.

Apache settings for the PHP module

When using the PHP module, Apache provides two mechanisms for setting PHP variables outside of the INI file:

  • Edit the httpd.conf file or one of the files it includes.
  • Edit the .htaccess file.

INI file per-directory configuration

INI file configuration is used when your web server serves up multiple domains.

To view each of these hosts separately in the New Relic UI:

  1. For your main domain, set newrelic.appname = "My Main Domain" in your global INI file.
  2. Override that value for each of the virtual hosts by adding php_value entries as part of your virtual host configuration.

For example:

<VirtualHost 192.168.42.43>
  ServerName www.myvhost1.com
  DocumentRoot "/path/to/vhost1/"
  ...
  <IfModule php5_module>
    php_value newrelic.appname "Virtual Host 1"
  </IfModule>
</VirtualHost>

<VirtualHost 192.168.123.45>
  ServerName www.myvhost2.com
  DocumentRoot "/path/to/vhost2/"
  ...
  <IfModule php5_module>
    php_value newrelic.appname "Virtual Host 2"
  </IfModule>
</VirtualHost>

Note: Make sure you use the proper module call for your PHP install. The name php5_module is not universal. It depends on the Linux and/or PHP distribution being used.

In the above example, the newrelic.appname is set to a different value for each virtual host. For string and number values, use php_value name VALUE, where:

  • name is the name of the INI setting to modify as listed in the PHP INI settings.
  • VALUE is the value you want to set to for that particular virtual host.

You must surround string values in quotes ("").

If you want to change a boolean setting, use the syntax php_flag name VALUE, where name is the variable name as listed in the PHP INI settings, and VALUE is either on or off.

To disable the New Relic agent completely for a virtual host, use a boolean flag:

<VirtualHost 192.168.56.78>
  ServerName www.myvhost3.com
  DocumentRoot "/path/to/vhost3/"
  ...
  <IfModule php5_module>
    php_flag newrelic.enabled off
  </IfModule>
</VirtualHost>

.htaccess per-directory configuration

The syntax used in the INI file example above can be used inside an .htaccess file.

Example:

php_value newrelic.appname "My Blog App"

This allows you to control settings on a per-directory basis from within the directories.

In this example, your web server has its document root at /data/webroot. You also have two sub-directories for specialized applications:

  • Your /data/webroot/blog contains a blog application.
  • Your /data/webroot/shop contains a shopping cart application.

You want all three portions of your site to be reported as distinct applications in the New Relic UI.

To do this:

  1. Set the name of your main application in your INI file.
  2. Override that name using an .htaccess file in each of the specialized directories.

Any part of your web server (for example, /data/webroot/something) that does not have a specific .htaccess file will use the global application name defined in the INI file.

Note: The .htaccess file must be in the top-level directory for that application. Objects in that directory, or its subdirectories, will use the value specified in the .htaccess file.

For more information about using and securing .htaccess files, see the Apache HTTP Server Tutorial.

php-fpm per-directory configuration

The FastCGI Process Manager (php-fpm) is dedicated to PHP. It spawns a number of worker processes that wait for requests. It increases performance by not re-initializing the PHP engine on each invocation, allowing each process to deal with a number of requests before it recycles.

For more information on php-fpm, see php-fpm's about page and FastCGI Process Manager on php.net.

A disadvantage to using php-fpm is that changing variables on a per-directory basis is more difficult. To do this, you must use multiple php-fpm pools, one for each virtual host or unique application.

A pool is a dedicated set of worker children that will only serve requests for that pool. Because it requires dedicated worker children, php-fpm scales poorly if you have a large number of virtual hosts or applications for which you want to set individual options.

To configure php-fpm on a per-directory basis:

  1. Set the main application name in the INI file.
  2. Set up two pools for the two additional applications.
  3. Override the application name setting in those pools.

Note: Each pool has to have a unique connection mechanism (so you can identify which pool to use in your web server configuration file).

Here is an example of php-fpm.conf:


[app1]
listen=/tmp/pool-app1.sock
php_value[newrelic.appname] = "My App 1"

[app2]
listen=/tmp/pool-app2.sock
php_value[newrelic.appname] = "My App 2"

[app3]
listen=/tmp/pool-app3.sock
php_flag[newrelic.enabled] = off

The general format of the per-pool variable settings is php_value[name] = VALUE for string or numeric variables, or php_flag[name] = VALUE for boolean values. Always surround string values with quotes (""). Boolean values must be either on or off.

Once the configuration file is set up, the web server must be instructed to use the different pools for different parts of the website. For more information, refer to the documentation for your web server.

nginx per-directory configuration

Note: This section only applies to PHP 5.3.3 or later.

Using an example similar to the Apache example, here is a small fragment of an nginx config file that shows the general procedure for passing values to your FastCGI manager based on an nginx location.


location /blog {
  fastcgi_param PHP_VALUE "newrelic.appname=My Blog";
  ...
}

location /shop {
  fastcgi_param PHP_VALUE "newrelic.appname=Shopping Cart";
  ...
}

A better approach for nginx may be to use API calls. The following code snippet gives you an example. Note: This will only work with version 2.7 and later of the agent.


  if (extension_loaded ('newrelic')) {
    newrelic_set_appname ("My App 1");
  }

Roll-up application names

If you want to have an overall view of how the server is performing across all virtual hosts or all applications, it is convenient to be able to report to more than one application at a time. For example, report to a virtual host specific application as well as a roll-up application.

To do this, set more than one application name for the newrelic.appname parameter by separating each application name with a semicolon. The primary application name is first, and the secondary application names next. You can define up to two extra application names.

For example:

newrelic.appname="Virtual Host 1;All Virtual Hosts"

This will report to two New Relic applications: "Virtual Host 1" and "All Virtual Hosts". Note: This feature is only available in version 2.4 and later of the PHP agent.

For more help

Additional documentation resources include:

If you need additional help, get support at support.newrelic.com.