Thursday, November 18, 2021

Use Newrelic monitor Linux PHP App Service

When running your PHP application in Azure Linux App Service, you may encounter availability or performance related issues.
PHP New Relic agent is one of the APM tools that can help to provide a better understanding of what may be causing these issues.

This article shows a demonstration of how to use New Relic agent to monitor your PHP application running in Linux App Service.

 

Prerequisites

  • A New Relic account.
  • Azure App Service for Linux with built-in PHP docker image.

 

Setup and Enable PHP New Relic agent

 

Create Newrelic API keys

We can get the Newrelic API keys by navigating directly to https://one.newrelic.com/launcher/api-keys-ui.api-keys-launcher
Create "Ingest-License" key:

Hanli_Ren_0-1635911520655.png

Hanli_Ren_1-1635911540772.png

Copy the new create API key, save it in your local notepad. We will use this key later.

 

Install the PHP Newrelic Agent in the App Service

Go to https://one.newrelic.com/, login your New Relic, then click "Add more data"

Hanli_Ren_2-1635911655437.png

Select PHP in "App monitoring"

Hanli_Ren_3-1635911711944.png

Go "Begin Installation" -> "PHP standard installation"

Hanli_Ren_4-1635911908360.png

Hanli_Ren_5-1635911926893.png

 

Provide your application name and select "apt".

Hanli_Ren_6-1635911976310.png

Newrelic will provide you the installation commands

Hanli_Ren_7-1635912047837.png

 

Let's put all the commands into a bash script file.

 

#!/bin/bash

echo 'deb http://apt.newrelic.com/debian/ newrelic non-free' | tee /etc/apt/sources.list.d/newrelic.list
wget -O- https://download.newrelic.com/548C16BF.gpg | apt-key add -
apt-get update
apt-get -y install newrelic-php5
NR_INSTALL_SILENT=1 newrelic-install install

sed -i -e "s/REPLACE_WITH_REAL_KEY/<your Newrelic API key>/" \
  -e "s/newrelic.appname[[:space:]]=[[:space:]].*/newrelic.appname=\"<your application name>\"/" \
  -e '$anewrelic.distributed_tracing_enabled=true' \
  $(php -r "echo(PHP_CONFIG_FILE_SCAN_DIR);")/newrelic.ini

echo '>>>check /usr/local/etc/php/conf.d/newrelic.ini'
grep newrelic.license /usr/local/etc/php/conf.d/newrelic.ini
grep newrelic.appname /usr/local/etc/php/conf.d/newrelic.ini

 

1) In line 9, replace <your Newrelic API key> with your Newrelic API key.

     For example:

     sed -i -e "s/REPLACE_WITH_REAL_KEY/9c6fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxNRAL/" \

2) In line 10, replace <your application name> with your Azure App Service name.

    For example:

    -e "s/newrelic.appname[[:space:]]=[[:space:]].*/newrelic.appname=\"myphpwebapp\"/" \

3) To confirm Newrelic being successfully installed, I added Line 14-16 to check newrelic.ini being configured as expected.

 

 

 

 

In your Azure App Service WEBSSH, create /home/setupNewrelic.sh with the above contents.

Go to https://<your-webapp-name>.scm.azurewebsites.net/webssh/host

 

vi /home/setupNewrelic.sh

 

Past all the script contents into it, then use ":wq" to save this file.

 

 

Go to your App Service Configuration portal to set "Startup Command", point it to your /home/setupNewrelic.sh

Hanli_Ren_1-1635916537717.png

 

Enable your "App Service logs"

Hanli_Ren_0-1637218763542.png

In your /home/LogFiles/xxxx_xx_xx_xxxxxxxx_default_docker.log file, you will see the Newrelic being installed during the boot up startup.

Hanli_Ren_1-1637221910951.png

Hanli_Ren_2-1637221974873.png

 

 

Monitor your PHP Application Performance in Newrelic

Go to https://one.newrelic.com/, click "Explorer" tab, in the "APM", we should see your java app showed up in the list. The Name should match your application name defined in your setupNewrelic.sh.

Hanli_Ren_2-1635916629050.png

 

Monitor the Application performance Summary.

Hanli_Ren_3-1635916659349.png

 

Check Errors and exceptions throwed by the PHP application

Hanli_Ren_4-1635916704294.png

 

Check Service map and dependencies

Hanli_Ren_5-1635916723426.png

Hanli_Ren_6-1635916743770.png

 

Check Transactions, analysis most time consuming requests

Hanli_Ren_7-1635916761220.png

 

Check Database performance

Hanli_Ren_8-1635916784005.png

 

 

 

 

 

Posted at https://sl.advdat.com/322MTYw