Bootstrap FreeKB - PHP - Enable SSH for PHP FPM on Docker
PHP - Enable SSH for PHP FPM on Docker

Updated:   |  PHP articles

This assumes you have installed the php:fpm container on Docker.

By default, the php:fpm image has the libssh-1 package but does not have libssh-1-dev package and does not have ssh2 installed, which are required for SSH.

For this reason, it typically makes sense to build the php:fpm image using a Dockerfile. The Dockerfile should have something like this.

Notice in this example that the "pecl install" command is installing version 1.3.1 of SSH2. The various versions of pecl SSH2 are listed at https://pecl.php.net/package/ssh2.

FROM php:fpm
RUN apt-get update
RUN apt-get install -y libssh2-1 libssh2-1-dev
RUN pecl install ssh2-1.3.1
RUN docker-php-ext-enable ssh2

 

And you would then use the docker build command to create the image.

docker build --file Dockerfile --tag php:fpm_ssh2

 

The docker images command should return something like this.

REPOSITORY   TAG          IMAGE ID       CREATED             SIZE
php          fpm_ssh2     bc6a0e2b34ff   About an hour ago   491MB

 

And then use the docker run command is used to create and start the php fpm container.

docker run --detach --name php-fpm --publish 9000:9000 --restart unless-stopped php:fpm_ssh2

 

The SSH2 library should be installed, which can be seen with the php -i command.

~]$ sudo docker exec php-fpm php -i | grep -i ssh
libSSH Version => libssh2/1.9.0

 

The SSH2 module should be enabled, which can be seen with the php -m command.

~]$ sudo docker exec php-fpm php -m | grep ssh
ssh2

 

And you can now try to make a connection to an SSH server.

<?php
  $connection = ssh2_connect('server1.example.com', 22);
?>

 

And check the logs to see if the connection was successful.

sudo docker logs php-fpm

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 3a4315 in the box below so that we can be sure you are a human.