New Relic, cPanel, Apache and FastCGI PHP

May 29th, 2013 by Richy B. Leave a reply »

We tend to operate RedHat Enterprise/CentOS based cPanel web hosting accounts for ourselves and our customers and for security, they are configured running PHP under suPHP via CGI/FastCGI. However, for server monitoring, we wanted to be able to use NewRelic for monitoring the sites on an individual level but without the customers having to add newrelic.appname to each of their applications – and we didn’t want each site to have to have its own php.ini, and under suPHP PHP ignores any php_value settings in the Apache configuration. So what to do?

Install New Relic
Well, first of all install New Relic according to your server architecture, then run “newrelic-install” and select all. Now we move on to the cPanel specific setup.

Getting PHP to notice the NewRelic PHP Module
In WHM (:2087), select “PHP Configuration Editor” and then “Advanced mode”. Search for Core “extension” and change the list from something like:
/usr/lib/php/extensions/no-debug-non-zts-20090626/apc.so, pdo.so, pdo_sqlite.so, sqlite.so, pdo_mysql.so, timezonedb.so, uploadprogress.so
to:
/usr/lib/php/extensions/no-debug-non-zts-20090626/apc.so, pdo.so, pdo_sqlite.so, sqlite.so, pdo_mysql.so, timezonedb.so, uploadprogress.so, newrelic.so

Restart Apache (via either WHM or /scripts/restartsrv_apache on the command line) and check everything is still working.

Configuring the newrelic.appname settings – Apache
From from command line/shell, cd into /var/cpanel/templates/apache2_2 (or whichever version of Apache you are running) and copy vhost.default to vhost.local (cp vhost.default vhost.local) if there isn’t already a vhost.local file. Now open that file up in your favourite text editor (for simplicities sake, I’m using nano : nano -w /var/cpanel/templates/apache2_2/vhost.local).

Near the top of the file it will say something like:

<VirtualHost[% FOREACH ipblock IN vhost.ips %] [% ipblock.ip %]:[% ipblock.port %][% END %]>
ServerName [% wildcard_safe(vhost.servername) %]
[% IF vhost.serveralias_array.size -%]
[% FOREACH alias IN vhost.serveralias_array -%]
ServerAlias [% alias %]
[% END -%]

Add a suitable “SetEnv newrelic_appname” line. For example, I want all of the sites to be recorded against the main virtual host’s server name, so I would use:

<VirtualHost[% FOREACH ipblock IN vhost.ips %] [% ipblock.ip %]:[% ipblock.port %][% END %]>
ServerName [% wildcard_safe(vhost.servername) %]
SetEnv newrelic_appname [% wildcard_safe(vhost.servername) %]
[% IF vhost.serveralias_array.size -%]
[% FOREACH alias IN vhost.serveralias_array -%]
ServerAlias [% alias %]
[% END -%]

You could also use the user’s username SetEnv newrelic_appname [% % vhost.user %] if you wanted to.

Use the Distiller to check everything looks good:
/usr/local/cpanel/bin/apache_conf_distiller --update
and if it does, rebuild the Apache configuration:
/usr/local/cpanel/bin/build_apache_conf
and again restart Apache and test.

Now, there is an environment variable called “newrelic_appname” available for usage. But we’re not using it yet: so we move onto:

Configuring the newrelic.appname settings – PHP
Using a <?php phpinfo(); ?> script find where your php.ini file – ours was in /usr/local/lib/php.ini . Open it open in your text editor again (nano -w /usr/local/lib/php.ini) and search for the newrelic.appname setting which should appear (Press Ctrl+W and then type appname in nano). You should see something like:

; Setting: newrelic.appname
; Type : string
; Scope : per-directory
; Default: “PHP Application”
; Info : Sets the name of the application that metrics will be reported into.
; This can in fact be a list of up to 3 application names, each of
; which must be separated by a semi-colon. The first name in any such
; list is considered the ‘primary’ application name and must be unique
; for each account / license key.
;
;newrelic.appname = “PHP Application”

;
; Beginning with version 3.0 of the agent, the daemon can be automatically
; started by the agent. There is no need to start the daemon before starting

Remove the colon and the start of the line and change it to read from ${newrelic_appname} for example:

; for each account / license key.
;
newrelic.appname = “${newrelic_appname}”

;
; Beginning with version 3.0 of the agent, the daemon can be automatically

This will read in the newrelic_appname environment variable upon run and dynamically replace it in the PHP settings. Restart Apache again (just to be safe) and check a few sites are working. Within a couple of minutes, you should see the records appear in NewRelic.

All done! Have fun!

This post is over 6 months old.

This means that, despite my best intentions, it may no longer be accurate.

This blog holds over 12 years of archived content - during that time, I may have changed my opinion of something, technology will have advanced (and old "best standards" may no longer be the case), my technology "know how" has improved etc etc - it would probably take me a considerable amount of time to update all the archival entries: and defeat the point of keeping them anyway.

Please take these posts for what they are: a brief look into my past, my history, my journey and "caveat emptor".

8 comments

  1. Jim Walker says:

    I was really excited at seeing your article.

    Did all the usual installation attempts, found your article and tried your recommendations. Sadly, remains a fail.

    Other than the notes in article, you have any other words of wisdom in regard to following your notes and things still noop?

    Many thanks,
    Jim

  2. Tim says:

    I followed this guide and it has worked a treat, except for one little issue which i think is character encoding related.

    I went with the username as the appname and it has worked.

    But it adds a “â” to the start and end of the appname in newrelic. Do you know where this is being added or how I can strip it out ?

    • Richy B. says:

      I assume you have:
      SetEnv newrelic_appname [% wildcard_safe(vhost.user) %]
      or
      SetEnv newrelic_appname [% vhost.user %]

      I would then guess that the â is being generated either by a “hidden character” in that line (manually type it instead of “cut and pasting” in case of your text editor/page you are copying from having “hidden” characters such as HTML markup which may interfere: ensure you do the same to the settings in /usr/local/lib/php.ini ). What does < ?php phpinfo(); ?> report the newrelic_appname as?

      Have a look at /var/log/newrelic/newrelic-daemon.log : if the â symbol appears in there, then it is definitely being produced on your server some where.

      • Tim says:

        Hi, the problem was that the ” in the php.ini file.

        I have removed those and it is now reporting correctly. It was the logs that highlighted the problem as they were showing quotes in the app names which weren’t reaching the dashboard.

      • Tim says:

        Hi, the problem was that the ” in the php.ini file.

        I have removed those and it is now reporting correctly. It was the logs that highlighted the problem as they were showing quotes in the app names which weren’t reaching the dashboard.

  3. Gulam says:

    I tried this exactly as per the instructions, but when I try to connect to mu application, it just says – waiting for data.

    Before I did ths, I had configured it for standard PHP Applications, this was working fine.

    What else can I do or check?

  4. no says:

    No, it does not work.

    newrelic doesn’t take into account ${newrelic_appname} and just reports as default “PHP Aplication”.

gamy-dance
%d bloggers like this: