# How to Use New Relic With PHP & WordPress

This post is part of a series called Performance Monitoring With New Relic.
Using New Relic to Monitor Your Android App
Optimizing Application Performance with New Relic for iOS

This sponsored post features a product relevant to our readers while meeting our editorial guidelines for being objective and educational.

We've previously covered how to set up New Relic for a Rails app, as well as spent a lot of time looking at how to use the New Relic UI. And while the UI is very similar regardless of the language and framework you're using, actually getting New Relic set up can be radically different. Today, we will look at how to monitor a PHP application using New Relic. More specifically, we will set up a basic WordPress installation and get some performance data about it, in the New Relic dashboards.

Getting New Relic set up for Ruby is very much environment agnostic. We simply add the agent gem to our application at which point no matter how we deploy our app (Passenger + Apache, Thin + Nginx etc.), the gem will do the rest of the work to ensure we get our performance metrics. With the PHP version of the agent, the environment is a lot more important, as the agent is installed and lives on the box where the application will be deployed, rather than being part of any particular app.

Let's set up a sandbox for us to play with (using an EC2 instance) and get a basic WordPress install up and running.

## Setting Up Our Sandbox

We won't go into too much detail here as most of the things we need to do are well-documented elsewhere. But, here is a basic outline.

We need to launch an EC2 instance with Ubuntu Server 12.04 LTS on it. If you don't want to set up an EC2 instance, you can just create a virtual machine instead using VirtualBox (or your VM tool of choice). If you're setting up an EC2 instance, you need to remember to do the following:

• download your key (if you've created a new one during the set up process), so that you can SSH into your instance
• add an extra rule to any security group you give to your instance to allow HTTP connections to the instance (so that we can actually access our WordPress blog via the browser later on)

Aside from that, everything else should be pretty straight forward and you should end up with a running instance (or virtual machine) that's ready for the next step.

We now need to install Apache, PHP and MySQL. With Ubuntu Server, it should be a simple matter of running the following commands:

You will need to select LAMP in the UI and you will also need to enter your MySQL password when prompted to do so (I just leave it blank since we don't care about this box being secure in any way). Once the installation is complete, we can then run a few commands to make sure that everything installed without trouble.

First check that Apache is installed:

Secondly, check that we have PHP:

And then check that we have MySQL:

We may also need to check that PHP is actually enabled in our Apache config, but since we installed lamp-server using tasksel we can be pretty sure that it is (and we can always do a quick phpinfo() script if we really want to check).

We can now install WordPress. Before we actually download it, we need to set up a database for it so. We can just follow the instructions from the Codex:

I am going to call our new installation myblog1 (so the database for it is also named myblog1). We now need to run the following commands to get our blog running (don't forget to sudo when necessary):

Now fill in your database name, username and password into the config file (the hostname is localhost which is there by default). At this point you should be able to go to your browser, hit the right URL (in my case http://ec2-107-20-122-116.compute-1.amazonaws.com/myblog1) and WordPress will do its thing (it might not hurt to restart Apache before doing this sudo apache2 service restart).

Our sandbox is now complete and we can get started with installing New Relic.

## Installing New Relic

As I mentioned previously, the PHP New Relic agent resides on the box, it therefore makes sense that you can install it using the operating system's package manager (apt-get since we're using Ubuntu). The first thing to do is to import the New Relic repository key:

Now we add the New Relic repository itself to the system:

At this point, we can use standard apt commands to install the agent:

This fetches the PHP agent package from the repository and puts the agent install script on the system. The script is called newrelic-install and it lives in /usr/bin, so you should be able to run in from anywhere. The script is also somewhat unfortunately named, as you can use it to both install and uninstall New Relic from your system. In order to install New Relic, we need to run:

The script is interactive and will ask you to input your license key. You can find this by pressing the big red button when you're setting up a new PHP application within the New Relic UI.

If you have more than one PHP installation on your system, it will also ask you to pick which installation New Relic should be installed for (you can pick any number of installations including all of them). If you only have the one PHP on your system, the script will just use that one. It's worth noting that if your version of PHP is older than 5.2, the script will bail out as older versions are not supported.

If everything goes well, you should see the following message:

The script will then print out some extra information for you including the location of the log files:

As well as the fact that you need to restart your web server (and PHP-FPM if you're using it).

If you do restart your server and tail the daemon log, you should see something like this:

Something named PHP Application is reporting. This is somewhat generic and totally doesn't look like our WordPress blog, but it's a good start. What this means is that all applications on your web server run and report as the same application to New Relic. This application has a default name of PHP Application

In our case, since we're only running one application (our WordPress install), we could actually jump into the New Relic UI and get reasonable stats for our blog. But, of course, we want to give our blog a better name and just in case, we want our server to serve more than one application. We also want to see how to separate apps from each other. We will look at how to do this shortly, but before we do, let's see what actually makes up a PHP agent.

## What a Healthy Install Looks Like

There are two parts to the New Relic PHP agent. The first is a PHP extension, it's a shared object called newrelic.so. If we look at the agent configuration file:

We can see it listed right at the top:

This is the thing that will actually collect the stats from your apps, but it will not send the stats to New Relic, this is the job of the proxy daemon.

The agent daemon is a proxy between the PHP extension and the New Relic servers. Essentially, the PHP extension will give the data it collects to the daemon and the daemon will do things like batch it up and figure out when to send it to the server. You need to always make sure that the daemon is running, otherwise no data will be sent to New Relic. Luckily, by default, whenever you restart your server, the PHP extension will try to detect if the daemon is running and will start it, if it isn't.

A running proxy daemon has two processes. One is a monitor process and the second is the worker. The worker actually does the job of communicating with the New Relic servers, the monitoring process simply watches the worker and if the worker dies, for whatever reason, it will spawn a new one. You can stop the daemon by running:

Which will send a shutdown signal to the monitoring process. The monitoring process will then kill the worker process and then shut itself down. If you ever need to kill the daemon manually, make sure you terminate the monitoring process first before killing the worker (otherwise a new worker will get spawned - obviously).

## Configuring the Agent (and the Proxy Daemon)

We've already seen the New Relic PHP agent configuration file /etc/php5/cli/conf.d/newrelic.ini. Both the agent and the daemon are configured using this file.

This file is very well documented with all the options and their default values listed. Let's talk about the format of this file. The New Relic Ruby agent can be configured via YAML, which is a well-known format. The PHP agent is simply a text file, but we need a little bit of structure. Each variable in the file has one of four types (String, Boolean, Number, Duration). String and number are self-explanatory, boolean values can be true, on or 1 to indicate truthiness and false, off or 0 to indicate falsiness. Durations are strings with a particular format, for example: "1w3d23h10m" indicates one week, three days, 23 hours and ten minutes. The values for durations can be as granular as microseconds.

All variables in the file also have a 'scope'. There are three possible scopes: SYSTEM, PERDIR and SCRIPT. Variables that have SYSTEM scope can only be set in the global configuration file. Variables with a PERDIR scope can be set in the global configuration file and can also be overridden on a per directory basis. Variables with script scope can be global, per directory and can also be overridden programmatically.

For example, the most common configuration variable is newrelic.appname. This variable is a String type, it has a default value of PHP Application (now we know why we saw that value in the log file after we installed the agent and restarted the server). The scope of this variable is PERDIR which gives us an idea about how to override the application name for our WordPress blog.

There are many other variables controlling things like the location of the log files, whether or not sql queries are recorded, the log level of the log output and so . I encourage you to study the newrelic.ini file to familiarise yourself with the options.

## Configuring a Separate App for Our WordPress Blog

We want to see a separate app in the New Relic UI for our WordPress blog, so let's see how we can get that going. Your options for per directory configuration are different depending on your stack, if you're using PHP-FPM, the steps are different than if you're using Nginx. In our case, since we're running straight Apache, we have two options.

Firstly, if we have a virtual host for our app, we can insert an IfModule block into our virtual host block and modify the name of the app there:

But I don't have a special virtual host just for the blog, so the other option is to use a .htaccess file. Ensure that you actually allow .htaccess files, by dumping the following in to your main virtual host:

We can now put an .htaccess file in the top level directory of our blog and put the following in it:

The format is exactly the same as if I was putting it into the IfModule block. Now we just need to bounce our server. If we tail the daemon logs when the server restarts, we will see the following:

The PHP Application is still there, but now it has a friend which is our WordPress blog. And here it is in the UI:

We will now receive metrics as we browse our blog, both for the front-end and the admin interface. Since New Relic supports WordPress out of the box, the metrics should be broken up sensibly (when a framework is unsupported, the metrics will tend to be lumped together).

## Updating the Agent & Daemon

New Relic is a complex piece of software and it's good to keep it up to date as bugs are fixed regularly and new features are added. Since we installed everything using apt-get, keeping things up to date is easy. We simply do the same thing we did to install it:

If we haven't installed another PHP on the system, we now simply need to restart our server and we'll be up to date. If there is a new PHP, we may need to re-run the newrelic-install script.

There is only one caveat to all of this. In certain situations, you may be running the New Relic proxy daemon separately from the agent, meaning that restarting the server does not automatically launch the daemon if one isn't running (this is called external daemon mode). There could be multiple reasons why you'd want to do this, for example, you might want a different user to own the daemon process so the logs are only visible to that user. In this situation, you have to remember to both restart the server and restart the proxy daemon. If you don't, you'll see 'protocol mismatch' errors in the logs.

## Conclusion

As you can see, getting New Relic set up for a PHP application is very different from getting it set up for Ruby, your actual application doesn't even factor in the process, while the environment where you deploy is central. Luckily this means that if you're using any supported PHP framework, the process for setting up New Relic is exactly the same. Besides WordPress, most of the popular PHP frameworks are supported including Cake, Symphony and Laravel (version 4 and up). It is also possible to use New Relic with an unsupported framework, but you will have to put in some serious effort to get the metrics to make sense.