Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nginx + php-fpm example app for docs #939

Merged
merged 6 commits into from Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/README.md
@@ -0,0 +1,4 @@
This folder contains examples to be used in our public documentation: https://docs.datadoghq.com/tracing/setup/php/.

Changes in this folder have to be carefully reviewed to make sure it is in sync with the relative section in the
public docs.
1 change: 1 addition & 0 deletions examples/nginx-php-fpm/.env
@@ -0,0 +1 @@
DD_TRACER_VERSION=0.46.0
13 changes: 13 additions & 0 deletions examples/nginx-php-fpm/Dockerfile_php_fpm
@@ -0,0 +1,13 @@
FROM php:7.4-fpm

ARG DD_TRACER_VERSION

ADD https://github.com/DataDog/dd-trace-php/releases/download/${DD_TRACER_VERSION}/datadog-php-tracer_${DD_TRACER_VERSION}_amd64.deb /tmp/dd-trace.deb
RUN dpkg -i /tmp/dd-trace.deb

RUN mkdir -p /var/www/public
ADD public /var/www/public

ADD www.conf /usr/local/etc/php-fpm.d/www.conf

CMD php-fpm
42 changes: 42 additions & 0 deletions examples/nginx-php-fpm/README.md
@@ -0,0 +1,42 @@
# Nginx + php-fpm example

Example of how to configure tracing with nginx and php-fpm.

## Nginx configuration

Nothing specific has to change here. [default.conf](default.conf) file you see in this example has no changes required specific to the tracing library.

*Note*: if you come from an older version you might have used `fastcgi_param`s to configure the tracer. This approach is now deprecated in favor of `env` directive in the fpm pool configuration. See next section.

## PHP-FPM configuration

You can configure the Datadog PHP tracing library via [environment variables](https://docs.datadoghq.com/tracing/setup/php/#environment-variable-configuration).

By default environment variables set in the host are not visible to PHP-FPM process.

If `clear_env` is not set or `clear_env=1` then `env` directive can be used in the `www.conf` file to set a specific setting. See this app's [www.conf](./www.conf) file for an example.

If `clear_env=0` then environment variables values from the host are visibile to the PHP process and using the `env` directive is not required.

See PHP-FPM's [configuration page](https://www.php.net/manual/en/install.fpm.configuration.php) for more details.

## How to run this app

*Note*: You need the environment variable `DATADOG_API_KEY` set on your machine with your api key.

From this directory

```
docker-compose build

docker-compose up -d
```

Then you can access the sample `index.php` file at [http://localhost:8888](http://localhost:8888).

```
$ curl localhost:8888
Hi
```

After a few seconds your traces will be visible in your dashboard: [US](https://app.datadoghq.com/apm/traces) or [EU](https://app.datadoghq.eu/apm/traces).
21 changes: 21 additions & 0 deletions examples/nginx-php-fpm/default.conf
@@ -0,0 +1,21 @@
server {
listen 80;

access_log /dev/stdout;
error_log /dev/stderr;

server_name localhost;

root /var/www/public;
index index.php;

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
27 changes: 27 additions & 0 deletions examples/nginx-php-fpm/docker-compose.yml
@@ -0,0 +1,27 @@
version: '3.6'

services:

php-fpm:
build:
context: .
dockerfile: Dockerfile_php_fpm
args:
DD_TRACER_VERSION: ${DD_TRACER_VERSION}

nginx:
image: nginx
ports:
- 8888:80
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
- ./public:/var/www/public

agent:
image: datadog/agent:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- /proc/:/host/proc/:ro
- /sys/fs/cgroup/:/host/sys/fs/cgroup:ro
environment:
- DD_API_KEY=${DATADOG_API_KEY}
3 changes: 3 additions & 0 deletions examples/nginx-php-fpm/public/index.php
@@ -0,0 +1,3 @@
<?php

echo "Hi!\n";
25 changes: 25 additions & 0 deletions examples/nginx-php-fpm/www.conf
@@ -0,0 +1,25 @@
[www]

user = www-data
group = www-data
listen = 0.0.0.0:9000

pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10

; clear_env = no

; You can use container's environment variables here: e.g. $SOME_ENV
env[DD_AGENT_HOST] = agent
env[DD_SERVICE_NAME] = my-service
; env[DD_TRACE_DEBUG] = true

; Logging
catch_workers_output = yes
php_flag[display_errors] = on
php_admin_value[error_log] = /proc/self/fd/2
php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 32M