Monitor PHP application performance with New Relic APM and Elastic APM, implement custom metrics collection, and set up comprehensive Grafana dashboards for real-time application observability and performance optimization.
Prerequisites
- PHP 8.4 or higher installed
- Web server (Apache/Nginx) running
- Database server (MySQL/PostgreSQL) configured
- Root or sudo access
What this solves
PHP applications need continuous monitoring to identify performance bottlenecks, track user experience metrics, and ensure optimal resource utilization. Application Performance Monitoring (APM) tools provide deep insights into database queries, external API calls, error rates, and response times. This tutorial shows you how to implement comprehensive PHP monitoring with New Relic APM, Elastic APM, custom metrics collection, and Grafana dashboards for production-ready observability.
Step-by-step installation
Update system packages
Start by updating your package manager to ensure you get the latest versions of all dependencies.
sudo apt update && sudo apt upgrade -y
Install PHP development tools
Install PHP development packages needed for compiling APM extensions and managing dependencies.
sudo apt install -y php-dev php-cli php-curl php-json curl build-essential
Download and install New Relic PHP agent
New Relic provides comprehensive APM monitoring with automatic instrumentation for popular PHP frameworks.
curl -L https://download.newrelic.com/php_agent/archive/10.15.0.4/newrelic-php5-10.15.0.4-linux.tar.gz -o newrelic-php-agent.tar.gz
tar -xzf newrelic-php-agent.tar.gz
cd newrelic-php5-*
sudo ./newrelic-install install
Configure New Relic APM
Set up New Relic with your license key and application settings for optimal monitoring coverage.
extension = "newrelic.so"
newrelic.license = "YOUR_NEW_RELIC_LICENSE_KEY"
newrelic.appname = "PHP Production App"
newrelic.daemon.location = "/usr/bin/newrelic-daemon"
newrelic.logfile = "/var/log/newrelic/php_agent.log"
newrelic.loglevel = "info"
newrelic.daemon.logfile = "/var/log/newrelic/newrelic-daemon.log"
newrelic.daemon.loglevel = "info"
newrelic.capture_params = true
newrelic.ignored_params = "credit_card,ssn,password"
newrelic.error_collector.enabled = true
newrelic.browser_monitoring.auto_instrument = true
newrelic.transaction_tracer.enabled = true
newrelic.transaction_tracer.threshold = "apdex_f"
newrelic.transaction_tracer.detail = 1
newrelic.transaction_tracer.record_sql = "obfuscated"
newrelic.transaction_tracer.explain_enabled = true
newrelic.transaction_tracer.explain_threshold = 500
newrelic.framework = "laravel,symfony,codeigniter"
Install Elastic APM PHP agent
Elastic APM provides open-source monitoring with deep integration into the Elastic Stack for log correlation and custom dashboards.
curl -L https://github.com/elastic/apm-agent-php/releases/latest/download/apm-agent-php_all.deb -o elastic-apm-php.deb
sudo dpkg -i elastic-apm-php.deb || sudo apt-get install -f
Configure Elastic APM
Configure the Elastic APM agent with your server details and application-specific settings for comprehensive monitoring.
extension=elastic_apm.so
elastic_apm.server_url=http://localhost:8200
elastic_apm.service_name=php-production-app
elastic_apm.service_version=1.0.0
elastic_apm.environment=production
elastic_apm.application_packages=app,src
elastic_apm.enabled=true
elastic_apm.recording=true
elastic_apm.instrument=true
elastic_apm.capture_errors=true
elastic_apm.error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT
elastic_apm.capture_spans=true
elastic_apm.span_frames_min_duration=5ms
elastic_apm.stack_trace_limit=50
elastic_apm.transaction_sample_rate=1.0
elastic_apm.central_config=true
elastic_apm.breakdown_metrics=true
elastic_apm.capture_body=transactions
elastic_apm.transaction_max_spans=500
elastic_apm.span_stack_trace_min_duration=5ms
elastic_apm.log_level=INFO
Install APM Server for Elastic Stack
Install and configure APM Server to receive and process monitoring data from your PHP applications.
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
sudo apt update
sudo apt install -y apm-server
Configure APM Server
Set up APM Server to handle incoming monitoring data and forward it to Elasticsearch for storage and analysis.
apm-server:
host: "0.0.0.0:8200"
max_header_size: 1048576
idle_timeout: 45s
read_timeout: 30s
write_timeout: 30s
shutdown_timeout: 5s
capture_personal_data: true
expvar:
enabled: true
url: "/debug/vars"
pprof:
enabled: true
output.elasticsearch:
hosts: ["localhost:9200"]
protocol: "http"
username: "elastic"
password: "your_elasticsearch_password"
index: "apm-%{[observer.version]}-{type}-%{+yyyy.MM.dd}"
template.settings:
index.number_of_shards: 1
index.number_of_replicas: 1
index.refresh_interval: 5s
setup.template:
enabled: true
pattern: "apm-*"
settings:
index:
number_of_shards: 1
number_of_replicas: 1
refresh_interval: 5s
setup.kibana:
host: "localhost:5601"
logging.level: info
logging.to_files: true
logging.files:
path: /var/log/apm-server
name: apm-server
keepfiles: 7
permissions: 0644
processors:
- add_host_metadata:
when.not.contains.tags: forwarded
- add_docker_metadata: ~
- add_kubernetes_metadata: ~
Install Prometheus for custom metrics
Install Prometheus to collect custom application metrics and system performance data for comprehensive monitoring.
sudo apt install -y prometheus prometheus-node-exporter
Configure custom PHP metrics collection
Create a custom PHP script to expose application-specific metrics in Prometheus format for detailed performance tracking.
query('SELECT COUNT(*) FROM users');
$queryTime = microtime(true) - $start;
$userCount = $stmt->fetchColumn();
} catch (PDOException $e) {
$dbConnected = 0;
$queryTime = 0;
$userCount = 0;
}
// Memory usage metrics
$memoryUsage = memory_get_usage(true);
$memoryPeak = memory_get_peak_usage(true);
// Cache hit rate (example with Redis)
try {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$info = $redis->info('stats');
$cacheHits = $info['keyspace_hits'] ?? 0;
$cacheMisses = $info['keyspace_misses'] ?? 0;
$cacheTotal = $cacheHits + $cacheMisses;
$cacheHitRate = $cacheTotal > 0 ? ($cacheHits / $cacheTotal) : 0;
$redisConnected = 1;
} catch (Exception $e) {
$cacheHitRate = 0;
$redisConnected = 0;
}
// Output Prometheus metrics
echo "# HELP php_app_db_connected Database connection status\n";
echo "# TYPE php_app_db_connected gauge\n";
echo "php_app_db_connected $dbConnected\n";
echo "# HELP php_app_query_duration_seconds Database query execution time\n";
echo "# TYPE php_app_query_duration_seconds gauge\n";
echo "php_app_query_duration_seconds $queryTime\n";
echo "# HELP php_app_users_total Total number of users\n";
echo "# TYPE php_app_users_total gauge\n";
echo "php_app_users_total $userCount\n";
echo "# HELP php_app_memory_usage_bytes Current memory usage\n";
echo "# TYPE php_app_memory_usage_bytes gauge\n";
echo "php_app_memory_usage_bytes $memoryUsage\n";
echo "# HELP php_app_memory_peak_bytes Peak memory usage\n";
echo "# TYPE php_app_memory_peak_bytes gauge\n";
echo "php_app_memory_peak_bytes $memoryPeak\n";
echo "# HELP php_app_cache_hit_rate Cache hit rate ratio\n";
echo "# TYPE php_app_cache_hit_rate gauge\n";
echo "php_app_cache_hit_rate $cacheHitRate\n";
echo "# HELP php_app_redis_connected Redis connection status\n";
echo "# TYPE php_app_redis_connected gauge\n";
echo "php_app_redis_connected $redisConnected\n";
// Application-specific business metrics
$todayOrders = 0; // Your business logic here
echo "# HELP php_app_orders_today_total Orders created today\n";
echo "# TYPE php_app_orders_today_total counter\n";
echo "php_app_orders_today_total $todayOrders\n";
?>
Configure Prometheus scraping
Configure Prometheus to scrape metrics from your PHP application endpoint and system monitoring targets.
global:
scrape_interval: 15s
evaluation_interval: 15s
external_labels:
monitor: 'php-production-monitor'
rule_files:
- "/etc/prometheus/php_rules.yml"
alerting:
alertmanagers:
- static_configs:
- targets:
- localhost:9093
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node-exporter'
static_configs:
- targets: ['localhost:9100']
- job_name: 'php-app-metrics'
static_configs:
- targets: ['localhost:80']
metrics_path: '/metrics.php'
scrape_interval: 30s
scrape_timeout: 10s
- job_name: 'nginx-exporter'
static_configs:
- targets: ['localhost:9113']
- job_name: 'mysql-exporter'
static_configs:
- targets: ['localhost:9104']
- job_name: 'redis-exporter'
static_configs:
- targets: ['localhost:9121']
Create PHP performance alerting rules
Define Prometheus alerting rules for critical PHP application performance metrics and system health indicators.
groups:
- name: php_app_alerts
rules:
- alert: PHPAppDatabaseDown
expr: php_app_db_connected == 0
for: 1m
labels:
severity: critical
annotations:
summary: "PHP application database connection failed"
description: "Database connection has been down for more than 1 minute"
- alert: PHPAppHighMemoryUsage
expr: php_app_memory_usage_bytes > 134217728 # 128MB
for: 5m
labels:
severity: warning
annotations:
summary: "PHP application high memory usage"
description: "Memory usage is {{ $value | humanize }} bytes"
- alert: PHPAppSlowQueries
expr: php_app_query_duration_seconds > 1
for: 2m
labels:
severity: warning
annotations:
summary: "PHP application slow database queries"
description: "Database queries taking more than 1 second"
- alert: PHPAppLowCacheHitRate
expr: php_app_cache_hit_rate < 0.8
for: 5m
labels:
severity: warning
annotations:
summary: "PHP application low cache hit rate"
description: "Cache hit rate is {{ $value | humanizePercentage }}"
- alert: PHPAppRedisDown
expr: php_app_redis_connected == 0
for: 1m
labels:
severity: critical
annotations:
summary: "PHP application Redis connection failed"
description: "Redis connection has been down for more than 1 minute"
Install and configure Grafana
Install Grafana for comprehensive visualization of your PHP application performance metrics and system monitoring data.
sudo apt install -y software-properties-common
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
sudo apt update
sudo apt install -y grafana
Create PHP application dashboard
Import a comprehensive Grafana dashboard configuration for monitoring PHP application performance, database metrics, and system resources.
{
"dashboard": {
"id": null,
"title": "PHP Application Performance Monitor",
"tags": ["php", "application", "performance"],
"timezone": "browser",
"time": {
"from": "now-1h",
"to": "now"
},
"refresh": "30s",
"panels": [
{
"id": 1,
"title": "Database Connection Status",
"type": "stat",
"targets": [{
"expr": "php_app_db_connected",
"legendFormat": "Database Connected"
}],
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"thresholds": {
"steps": [
{"color": "red", "value": 0},
{"color": "green", "value": 1}
]
}
}
}
},
{
"id": 2,
"title": "Memory Usage",
"type": "timeseries",
"targets": [
{
"expr": "php_app_memory_usage_bytes",
"legendFormat": "Current Memory"
},
{
"expr": "php_app_memory_peak_bytes",
"legendFormat": "Peak Memory"
}
]
},
{
"id": 3,
"title": "Query Response Time",
"type": "timeseries",
"targets": [{
"expr": "php_app_query_duration_seconds",
"legendFormat": "Query Time (seconds)"
}]
},
{
"id": 4,
"title": "Cache Hit Rate",
"type": "timeseries",
"targets": [{
"expr": "php_app_cache_hit_rate * 100",
"legendFormat": "Hit Rate %"
}]
}
]
}
}
Set correct permissions
Set appropriate file ownership and permissions for monitoring components to ensure security and proper functionality.
sudo chown www-data:www-data /var/www/metrics.php
sudo chmod 644 /var/www/metrics.php
sudo chown prometheus:prometheus /etc/prometheus/php_rules.yml
sudo chmod 644 /etc/prometheus/php_rules.yml
sudo chown grafana:grafana /var/lib/grafana/dashboards/php-app-dashboard.json
sudo chmod 644 /var/lib/grafana/dashboards/php-app-dashboard.json
Enable and start all services
Enable and start all monitoring services to begin collecting and visualizing PHP application performance data.
sudo systemctl enable --now newrelic-daemon
sudo systemctl enable --now apm-server
sudo systemctl enable --now prometheus
sudo systemctl enable --now node_exporter
sudo systemctl enable --now grafana-server
sudo systemctl restart php8.4-fpm
sudo systemctl restart nginx
Verify your setup
Test all monitoring components to ensure they are collecting and displaying PHP application performance data correctly.
# Check service status
sudo systemctl status newrelic-daemon apm-server prometheus grafana-server
Test custom metrics endpoint
curl http://localhost/metrics.php
Check Prometheus targets
curl http://localhost:9090/api/v1/targets
Verify APM server is receiving data
curl http://localhost:8200/
Check New Relic agent logs
sudo tail -f /var/log/newrelic/php_agent.log
Test Grafana access
curl -I http://localhost:3000
Verify Elastic APM data collection
curl http://localhost:8200/config/v1/agents
Access your monitoring dashboards:
- Grafana: http://localhost:3000 (admin/admin)
- Prometheus: http://localhost:9090
- New Relic: https://one.newrelic.com
- Kibana (for Elastic APM): http://localhost:5601
Common issues
| Symptom | Cause | Fix |
|---|---|---|
| New Relic not collecting data | Invalid license key or agent not loaded | Check /var/log/newrelic/php_agent.log and verify extension in php -m |
| Elastic APM connection errors | APM server not running or wrong URL | sudo systemctl status apm-server and verify server_url setting |
| Custom metrics endpoint 404 | PHP file not accessible or wrong location | Check web server configuration and file permissions with ls -la |
| Prometheus targets down | Services not running or firewall blocking | Verify service status and check netstat -tlnp for listening ports |
| Grafana dashboard empty | Data source not configured or wrong queries | Add Prometheus data source at http://localhost:9090 and test connection |
| High memory usage alerts | Memory leaks or inefficient code | Review APM transaction traces and optimize database queries |
Next steps
- Configure NGINX monitoring with Prometheus and Grafana for web server observability
- Implement log-based monitoring and alerting with Grafana and Loki for centralized logging
- Configure PHP-FPM performance optimization for high-traffic applications
- Set up Elasticsearch APM Server clustering for high availability
- Implement custom PHP performance profiling with XHProf and Tideways
Running this in production?
Automated install script
Run this to automate the entire setup
#!/usr/bin/env bash
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Script configuration
SCRIPT_NAME="$(basename "$0")"
NEW_RELIC_VERSION="10.15.0.4"
ELASTIC_APM_VERSION="1.9.0"
# Usage message
usage() {
echo "Usage: $SCRIPT_NAME [OPTIONS]"
echo "Options:"
echo " -n, --newrelic-key KEY New Relic license key"
echo " -a, --app-name NAME Application name (default: php-production-app)"
echo " -e, --elastic-url URL Elastic APM server URL (default: http://localhost:8200)"
echo " -h, --help Show this help message"
exit 1
}
# Parse command line arguments
NEW_RELIC_KEY=""
APP_NAME="php-production-app"
ELASTIC_URL="http://localhost:8200"
while [[ $# -gt 0 ]]; do
case $1 in
-n|--newrelic-key)
NEW_RELIC_KEY="$2"
shift 2
;;
-a|--app-name)
APP_NAME="$2"
shift 2
;;
-e|--elastic-url)
ELASTIC_URL="$2"
shift 2
;;
-h|--help)
usage
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
usage
;;
esac
done
# Cleanup function for error handling
cleanup() {
echo -e "${RED}Installation failed. Cleaning up temporary files...${NC}"
rm -rf /tmp/newrelic-php* /tmp/elastic-apm* /tmp/apm-server* 2>/dev/null || true
}
trap cleanup ERR
# Check if running as root or with sudo
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}This script must be run as root or with sudo${NC}"
exit 1
fi
# Auto-detect distribution
if [ -f /etc/os-release ]; then
. /etc/os-release
case "$ID" in
ubuntu|debian)
PKG_MGR="apt"
PKG_INSTALL="apt install -y"
PKG_UPDATE="apt update && apt upgrade -y"
PHP_CONF_DIR="/etc/php"
;;
almalinux|rocky|centos|rhel|ol)
PKG_MGR="dnf"
PKG_INSTALL="dnf install -y"
PKG_UPDATE="dnf update -y"
PHP_CONF_DIR="/etc"
;;
fedora)
PKG_MGR="dnf"
PKG_INSTALL="dnf install -y"
PKG_UPDATE="dnf update -y"
PHP_CONF_DIR="/etc"
;;
amzn)
PKG_MGR="yum"
PKG_INSTALL="yum install -y"
PKG_UPDATE="yum update -y"
PHP_CONF_DIR="/etc"
;;
*)
echo -e "${RED}Unsupported distribution: $ID${NC}"
exit 1
;;
esac
else
echo -e "${RED}Cannot detect distribution. /etc/os-release not found.${NC}"
exit 1
fi
echo -e "${GREEN}Detected distribution: $PRETTY_NAME${NC}"
echo -e "${GREEN}Using package manager: $PKG_MGR${NC}"
# Step 1: Update system packages
echo -e "${GREEN}[1/8] Updating system packages...${NC}"
$PKG_UPDATE
# Step 2: Install PHP development tools and dependencies
echo -e "${GREEN}[2/8] Installing PHP development tools...${NC}"
if [[ "$PKG_MGR" == "apt" ]]; then
$PKG_INSTALL php-dev php-cli php-curl php-json curl build-essential wget gnupg2
else
$PKG_INSTALL php-devel php-cli php-curl php-json curl gcc gcc-c++ make wget
fi
# Find PHP version and configuration directory
PHP_VERSION=$(php -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;")
if [[ "$PKG_MGR" == "apt" ]]; then
PHP_INI_DIR="$PHP_CONF_DIR/$PHP_VERSION/mods-available"
PHP_CLI_INI_DIR="$PHP_CONF_DIR/$PHP_VERSION/cli/conf.d"
else
PHP_INI_DIR="$PHP_CONF_DIR/php.d"
PHP_CLI_INI_DIR="$PHP_CONF_DIR/php.d"
fi
# Step 3: Create necessary directories and set permissions
echo -e "${GREEN}[3/8] Creating directories and setting permissions...${NC}"
mkdir -p /var/log/newrelic /var/log/elastic-apm
chown www-data:www-data /var/log/newrelic /var/log/elastic-apm 2>/dev/null || \
chown apache:apache /var/log/newrelic /var/log/elastic-apm 2>/dev/null || \
chown nginx:nginx /var/log/newrelic /var/log/elastic-apm 2>/dev/null || true
chmod 755 /var/log/newrelic /var/log/elastic-apm
# Step 4: Download and install New Relic PHP agent
echo -e "${GREEN}[4/8] Installing New Relic PHP agent...${NC}"
cd /tmp
curl -L "https://download.newrelic.com/php_agent/archive/$NEW_RELIC_VERSION/newrelic-php5-$NEW_RELIC_VERSION-linux.tar.gz" -o newrelic-php-agent.tar.gz
tar -xzf newrelic-php-agent.tar.gz
cd newrelic-php5-*
echo "" | ./newrelic-install install
# Step 5: Configure New Relic APM
echo -e "${GREEN}[5/8] Configuring New Relic APM...${NC}"
NEW_RELIC_INI="$PHP_INI_DIR/newrelic.ini"
if [[ "$PKG_MGR" != "apt" ]]; then
NEW_RELIC_INI="$PHP_INI_DIR/newrelic.ini"
fi
cat > "$NEW_RELIC_INI" << EOF
extension = "newrelic.so"
newrelic.license = "${NEW_RELIC_KEY:-YOUR_NEW_RELIC_LICENSE_KEY}"
newrelic.appname = "$APP_NAME"
newrelic.daemon.location = "/usr/bin/newrelic-daemon"
newrelic.logfile = "/var/log/newrelic/php_agent.log"
newrelic.loglevel = "info"
newrelic.daemon.logfile = "/var/log/newrelic/newrelic-daemon.log"
newrelic.daemon.loglevel = "info"
newrelic.capture_params = true
newrelic.ignored_params = "credit_card,ssn,password"
newrelic.error_collector.enabled = true
newrelic.browser_monitoring.auto_instrument = true
newrelic.transaction_tracer.enabled = true
newrelic.transaction_tracer.threshold = "apdex_f"
newrelic.transaction_tracer.detail = 1
newrelic.transaction_tracer.record_sql = "obfuscated"
newrelic.transaction_tracer.explain_enabled = true
newrelic.transaction_tracer.explain_threshold = 500
newrelic.framework = "laravel,symfony,codeigniter"
EOF
chmod 644 "$NEW_RELIC_INI"
# Enable New Relic module for Debian-based systems
if [[ "$PKG_MGR" == "apt" ]]; then
ln -sf "$NEW_RELIC_INI" "$PHP_CLI_INI_DIR/20-newrelic.ini" 2>/dev/null || true
fi
# Step 6: Install Elastic APM PHP agent
echo -e "${GREEN}[6/8] Installing Elastic APM PHP agent...${NC}"
cd /tmp
if [[ "$PKG_MGR" == "apt" ]]; then
curl -L "https://github.com/elastic/apm-agent-php/releases/download/v$ELASTIC_APM_VERSION/apm-agent-php_${ELASTIC_APM_VERSION}_all.deb" -o elastic-apm-php.deb
dpkg -i elastic-apm-php.deb || $PKG_INSTALL -f
else
# For RHEL-based systems, compile from source
echo -e "${YELLOW}Elastic APM requires manual compilation on RHEL-based systems${NC}"
fi
# Step 7: Configure Elastic APM
echo -e "${GREEN}[7/8] Configuring Elastic APM...${NC}"
ELASTIC_APM_INI="$PHP_INI_DIR/elastic_apm.ini"
cat > "$ELASTIC_APM_INI" << EOF
extension=elastic_apm.so
elastic_apm.server_url=$ELASTIC_URL
elastic_apm.service_name=$APP_NAME
elastic_apm.service_version=1.0.0
elastic_apm.environment=production
elastic_apm.application_packages=app,src
elastic_apm.enabled=true
elastic_apm.recording=true
elastic_apm.instrument=true
elastic_apm.capture_errors=true
elastic_apm.error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT
elastic_apm.capture_spans=true
elastic_apm.span_frames_min_duration=5ms
elastic_apm.stack_trace_limit=50
elastic_apm.transaction_sample_rate=1.0
elastic_apm.central_config=true
elastic_apm.breakdown_metrics=true
elastic_apm.capture_body=transactions
elastic_apm.transaction_max_spans=500
elastic_apm.span_stack_trace_min_duration=5ms
elastic_apm.log_level=INFO
EOF
chmod 644 "$ELASTIC_APM_INI"
# Enable Elastic APM module for Debian-based systems
if [[ "$PKG_MGR" == "apt" ]]; then
ln -sf "$ELASTIC_APM_INI" "$PHP_CLI_INI_DIR/20-elastic_apm.ini" 2>/dev/null || true
fi
# Step 8: Restart web services and verify installation
echo -e "${GREEN}[8/8] Restarting services and verifying installation...${NC}"
# Restart common web services
for service in apache2 httpd nginx php-fpm; do
if systemctl is-active --quiet "$service" 2>/dev/null; then
echo -e "${YELLOW}Restarting $service...${NC}"
systemctl restart "$service"
fi
done
# Verification checks
echo -e "${GREEN}Verifying installation...${NC}"
# Check PHP extensions
if php -m | grep -q newrelic; then
echo -e "${GREEN}✓ New Relic PHP extension loaded${NC}"
else
echo -e "${YELLOW}⚠ New Relic PHP extension not loaded${NC}"
fi
if php -m | grep -q elastic_apm 2>/dev/null; then
echo -e "${GREEN}✓ Elastic APM PHP extension loaded${NC}"
else
echo -e "${YELLOW}⚠ Elastic APM PHP extension not loaded (normal for RHEL-based systems)${NC}"
fi
# Check log directories
if [[ -d /var/log/newrelic ]]; then
echo -e "${GREEN}✓ New Relic log directory created${NC}"
fi
if [[ -d /var/log/elastic-apm ]]; then
echo -e "${GREEN}✓ Elastic APM log directory created${NC}"
fi
# Cleanup temporary files
rm -rf /tmp/newrelic-php* /tmp/elastic-apm* 2>/dev/null || true
echo -e "${GREEN}Installation completed successfully!${NC}"
echo -e "${YELLOW}Next steps:${NC}"
echo -e "1. Configure your New Relic license key in $NEW_RELIC_INI"
echo -e "2. Set up Elastic APM server if not already running"
echo -e "3. Verify monitoring data appears in your APM dashboards"
echo -e "4. Review log files in /var/log/newrelic and /var/log/elastic-apm"
Review the script before running. Execute with: bash install.sh