Setting Up Server Monitoring with New Relic
New Relic is an observability platform with a free tier up to 100 GB data per month. It includes APM, Infrastructure monitoring, Browser monitoring, and Alerts in one account.
Installing the Infrastructure Agent
# Automatic installation (recommended)
curl -Ls https://download.newrelic.com/install/newrelic-cli/scripts/install.sh | \
bash && sudo NEW_RELIC_API_KEY="NRAK-..." \
NEW_RELIC_ACCOUNT_ID="1234567" \
/usr/local/bin/newrelic install
# Manual installation (Ubuntu)
echo "deb http://apt.newrelic.com/debian/ newrelic non-free" | \
sudo tee /etc/apt/sources.list.d/newrelic.list
wget -q https://download.newrelic.com/548C16BF.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install newrelic-infra
# /etc/newrelic-infra.yml
license_key: your-license-key
display_name: web01.example.com
custom_attributes:
environment: production
app: myapp
region: eu-west-1
PHP APM Agent
# Install PHP agent
wget https://download.newrelic.com/php_agent/archive/10.x.x/newrelic-php5-10.x.x-linux.tar.gz
tar -xzf newrelic-php5-*.tar.gz
cd newrelic-php5-*/
sudo ./newrelic-install install
# php.ini (added automatically)
extension = newrelic.so
newrelic.license = "your-license-key"
newrelic.appname = "MyApp (Production)"
newrelic.distributed_tracing_enabled = true
newrelic.transaction_tracer.enabled = true
newrelic.transaction_tracer.threshold = 500 # ms
Custom Metrics and Events
// Custom metric
newrelic_custom_metric('Custom/OrderProcessing/Duration', $durationMs);
newrelic_custom_metric('Custom/Orders/Total', $order->total);
// Custom event
newrelic_record_custom_event('OrderPlaced', [
'orderId' => $order->id,
'userId' => $user->id,
'total' => $order->total,
'items' => $order->items->count(),
]);
// Add attributes to current transaction
newrelic_add_custom_parameter('userId', auth()->id());
newrelic_add_custom_parameter('planType', auth()->user()->plan);
// Name the transaction
newrelic_name_transaction('/orders/checkout');
Node.js APM
// First line in entry point!
require('newrelic');
// newrelic.js
exports.config = {
app_name: ['MyApp (Production)'],
license_key: process.env.NEW_RELIC_LICENSE_KEY,
logging: { level: 'info' },
allow_all_headers: true,
distributed_tracing: { enabled: true },
transaction_tracer: {
enabled: true,
transaction_threshold: 500,
},
};
NRQL Alerts (New Relic Query Language)
-- Alert: high error rate
SELECT percentage(count(*), WHERE error IS true)
FROM Transaction
WHERE appName = 'MyApp (Production)'
SINCE 5 minutes ago
-- Alert: slow transactions p95
SELECT percentile(duration, 95)
FROM Transaction
WHERE appName = 'MyApp (Production)'
SINCE 5 minutes ago
-- Dashboard: top slow database queries
SELECT average(duration), count(*)
FROM DatabaseQuery
FACET query
WHERE appName = 'MyApp (Production)'
SINCE 1 hour ago
LIMIT 20
PostgreSQL Integration
# /etc/newrelic-infra/integrations.d/postgresql-config.yml
integrations:
- name: nri-postgresql
env:
USERNAME: newrelic
PASSWORD: secure-password
HOSTNAME: localhost
PORT: 5432
DATABASE: myapp
COLLECT_DB_LOCK_METRICS: true
ENABLE_SSL: false
interval: 30s
Implementation Timeline
New Relic Infrastructure + PHP APM agent: 1 day. With custom metrics, NRQL alerts, and Browser monitoring: 2–3 days.







