Install Blackfire.io on LfPHP

Please note that this code example uses PHP 7.2.5 (asclinux/linuxforphp-8.1:7.2.5-nts)!

Download code as text file

If you wish to install and configure Blackfire.io on Linux for PHP using PHP 7.2.5, please run this command :

docker run --rm -it -p 8181:80 asclinux/linuxforphp-8.1:7.2.5-nts /bin/bash -c "cd ; wget -O tmp http://bit.ly/2kpol3j ; /bin/bash ./tmp ; echo '<?php phpinfo();' > /srv/www/index.php ; /bin/bash"

Alternatively, you could install Blackfire.io on Linux for PHP by entering these commands manually on the container's CLI :

cd /srv/www
wget -O blackfire-agent https://packages.blackfire.io/binaries/blackfire-agent/1.17.0/blackfire-agent-linux_static_amd64
mv blackfire-agent /usr/local/bin/
chmod +x /usr/local/bin/blackfire-agent
mkdir -p /etc/blackfire
cat >/etc/blackfire/agent << 'EOF'
[blackfire]
;
; setting: ca-cert
; desc   : Sets the PEM encoded certificates
; default:
ca-cert=

;
; setting: collector
; desc   : Sets the URL of Blackfire's data collector
; default: https://blackfire.io
collector=https://blackfire.io/

;
; setting: log-file
; desc   : Sets the path of the log file. Use stderr to log to stderr
; default: stderr
log-file=stderr

;
; setting: log-level
; desc   : log verbosity level (4: debug, 3: info, 2: warning, 1: error)
; default: 1
log-level=1

;
; setting: server-id
; desc   : Sets the server id used to authenticate with Blackfire API
; default:
server-id=

;
; setting: server-token
; desc   : Sets the server token used to authenticate with Blackfire
API. It is unsafe to set this from the command line
; default:
server-token=

;
; setting: socket
; desc   : Sets the socket the agent should read traces from. Possible
value can be a unix socket or a TCP address
; default: unix:///var/run/blackfire/agent.sock on Linux,
unix:///usr/local/var/run/blackfire-agent.sock on MacOSX, and
tcp://127.0.0.1:8307 on Windows.
socket=unix:///var/run/blackfire/agent.sock

;
; setting: spec
; desc   : Sets the path to the json specifications file
; default:
spec=
EOF
mkdir -p /var/run/blackfire
touch /var/run/blackfire/agent.sock
blackfire-agent -register

Once you will have entered the last command, you will have to supply your Blackfire server credentials. These can be found in your Blackfire account at the following URL : https://blackfire.io/account#server. Once you have entered your credentials, you can start the agent by entering the following command :

blackfire-agent start &

Now, install the Blackfire client by entering the following commands :

wget -O blackfire https://packages.blackfire.io/binaries/blackfire-agent/1.17.0/blackfire-cli-linux_static_amd64
mv blackfire /usr/local/bin/
chmod +x /usr/local/bin/blackfire
blackfire config

After entering the last command, you will have to supply your Blackfire client credentials. These can also be found in your Blackfire account at the following URL : https://blackfire.io/account#client.

The final step in order to run Blackfire inside a Linux for PHP container is to install the Blackfire Probe as a PHP extension. In order to do this, please start by downloading the library and configuring it.

wget -O blackfire.so https://packages.blackfire.io/binaries/blackfire-php/1.20.0/blackfire-php-linux_amd64-php-72.so
cat >>/etc/php.ini << 'EOF'
[blackfire]
extension = blackfire.so
; On Windows use the following configuration:
; extension=php_blackfire.dll

; Sets the socket where the agent is listening.
; Possible value can be a unix socket or a TCP address.
; Defaults to unix:///var/run/blackfire/agent.sock on Linux,
; unix:///usr/local/var/run/blackfire-agent.sock on MacOSX,
; and to tcp://127.0.0.1:8307 on Windows.
;blackfire.agent_socket = unix:///var/run/blackfire/agent.sock

blackfire.agent_timeout = 0.25

; Log verbosity level (4: debug, 3: info, 2: warning, 1: error)
;blackfire.log_level = 1

; Log file (STDERR by default)
;blackfire.log_file = /tmp/blackfire.log

;blackfire.server_id =

;blackfire.server_token =
EOF
mv blackfire.so $( php -i | grep extensions | awk '{print $3}' )
/etc/init.d/php-fpm restart
/etc/init.d/httpd restart

Once the shared object is copied to the PHP extensions folder and PHP-FPM is restarted, you will be able to profile your PHP applications by using the Blackfire command line tool we installed previously or by installing and using the Blackfire browser plugin in your favorite browser.

Have a lot of fun!