New Relic is a great service for monitoring servers and website applications. One of the major features is New Relic’s PHP application monitoring and the ability to do per directory PHP monitoring. This is useful for web servers that run PHP but serve multiple PHP applications under different domain names/directories. Being able to create per directory PHP application monitoring allows you to get more meaningful data about a specific PHP app and gives you the ability to segment your app data easily. I’ll show you how you can automate this type of setup with an Apache setup which is controlled by DirectAdmin.
Before getting into anything, I’m assuming that you have already installed the PHP monitoring agent for your global PHP configuration, so New Relic is actually monitoring your web server running PHP generally already. If not, New Relic provides nice and detailed guides about setting up the agent and the various configuration variables.
For this guide I am using mod_php with mod_ruid2 (though that’s not a specific requirement).
You have various methods available when it comes to implementing per directory PHP monitoring, dependant on the PHP setup you have.
- Modifying the VirtualHost templates DirectAdmin uses (Recommended)
- Injecting code using the Custom HTTPD Configurations feature in DirectAdmin (Similar to method one but less cleaner)
- Setting the app value in the .htaccess
You can get a breakdown of the types of methods available from New Relic.
Using Custom VirtualHost configurations
You’ll first need to copy the VirtualHost templates DirectAdmin uses and put them in the custom folder, this will allow you to modify the VirtualHost structure without it being overwritten by future rewrite opreations. Modifying VirtualHost configs outside of DirectAdmin is not recommended due to this reason. If your already using the custom templates, don’t overwrite your current custom files, skip straight to editing.
cd /usr/local/directadmin/data/templates mkdir -p custom cp virtual_host2*.conf custom cd custom
Adding New Relic PHP monitoring to multiple domains/sites
In order for this to work you need to set the value of newrelic.appname to something else that’s not the default app name set in the newrelic.ini. This requires you to pass a new name with php_value. Depending on your PHP setup, some configurations may not allow you to override this value like this. Particularly I had problems when using suphp when testing.
The modifications required will need to be done in multiple templates depending on what level your interested in monitoring:
- virtual_host2.conf – root domain configuration
- virtual_host2_secure.conf – SSL root domain configuration
- virtual_host2_sub.conf – subdomain configuration (Optional)
- virtual_host2_secure_sub.conf – SSL subdomain configuration (Optional)
If you want to implement monitoring on subdomains, you’ll need to edit the two sub configs, if your not interested this you can skip them.
Defining app names for root domains
You’ll need to edit both virtual_host2.conf and virtual_host2_secure.conf
To make this automated, I’ll be using DirectAdmin tokens to automatically populate the appname as the website domain, so it can be applied to a template once and DirectAdmin generates all the app names for each user httpd.conf.
Find the VirtualHost block and place this code within it. In terms of placement I recommend placing it after the CustomLog and ErrorLog lines but before any Directory configuration block.
<IfModule PHP_MODULE_NAME> php_value newrelic.appname "|DOMAIN|" </IfModule>
PHP_MODULE_NAME needs to be replaced with the module name your PHP is setup with in Apache, if your not sure you can run something like this to find out the module name:
apachectl -t -D DUMP_MODULES | grep php
In my case because my setup is mod_php, its php5_module, but even if you run the same setup the module might be named differently depending on your OS so its good to check.
Defining app names for subdomains
You’ll need to edit both virtual_host2_sub.conf and virtual_host2_secure_sub.conf
In the same way we modified root domains, we can also do the same for subdomains, with one additional token:
<IfModule PHP_MODULE_NAME> php_value newrelic.appname "|SUB|.|DOMAIN|" </IfModule>
The addition of the sub token allows DirectAdmin to create app names that use the actual full subdomain rather than the root domain name, so a separate app is created. You may or may not want to do this.
Excluding a certain domain from being monitored
The setup above basically assumes you want all domains/subdomains monitored by modifying the PHP value to something different from the main newrelic.appname value. If for whatever reason you don’t want a certain site monitored as an app, you can use conditional logic such as the example below.
Excluding a domain from being monitoring by New Relic
|*if DOMAIN="domain.com"| <IfModule PHP_MODULE> php_flag newrelic.enabled off </IfModule> |*else| <IfModule PHP_MODULE_NAME> php_value newrelic.appname "|DOMAIN|" </IfModule> |*endif|
The same would apply for the subdomain VirtualHost, just with the addition of the |SUB| token as shown in previous examples. As well as DOMAIN, you could use other token values such as IP or DOCROOT if you want to create exclusion rules with different criteria.
Rewrite the httpd.conf files
Once your happy with the changes to the custom templates, you’ll need DirectAdmin to run a rewrite on all user configuration files so the changes are picked up. This can be done in one command like so:
echo "action=rewrite&value=httpd" >> /usr/local/directadmin/data/task.queue
Running this command signals a rewrite of all httpd.conf files. It will take a couple of minutes to complete after which Apache will be automatically restarted. After doing this you should see PHP applications appearing in the APM dashboard of New Relic, this can however take a couple of minutes. You can go to the Admin Control Panel of DirectAdmin and go to the Custom HTTPD configurations section, from there you can check each VirtualHost configuration for all domains running on your server to confirm everything is correct and the custom New Relic parts are showing up, checking the token values being passed to each config are correct.
If anything goes wrong or something isn’t working quite right you can revert the changes by removing custom folder and running the rewrite command above again, DirectAdmin will then revert back to the default templates.