Bootstrap FreeKB - PHP - Nginx and PHP FastCGI on Docker using UNIX socket
PHP - Nginx and PHP FastCGI on Docker using UNIX socket

Updated:   |  PHP articles

This assumes you have two containers running on the same Docker network, where one container was built from the nginx image and the other container was built from the php:fpm image

The docker container ls command can be used to ensure the containers are running.

~]# docker container ls -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED        STATUS        PORTS                                                                                                                                                 NAMES
8e894ff6f352   php:fpm               "docker-php-entrypoint"   29 hours ago   Up 29 hours   0.0.0.0:9000->9000/tcp                                                                                                                                php-fpm
24bcf99fc7bc   nginx                 "/docker-entrypoint.sh"   29 hours ago   Up 29 hours   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp

 

Let's say the server block in the /etc/nginx/conf.d/default.conf file in the nginx container contains the following. Notice that the document root directory is /var/www/html.

AVOID TROUBLE

Ensure the root directive is not defined inside of a location block.

Ensure DNS is able to resolve server_name to the Docker system that contains the PHP container otherwise the PHP file will be downloaded instead of being presented in the browser.

server {
    listen       80;
    server_name  stage.example.com;
    root         /var/www/html;
    index        index.html index.php;

    location / {
        try_files $uri $uri/ /index.php;
    }
}

 

In this example, both the nginx and PHP FPM containers will need the same exact files located at /var/www/html in each container, thus you'll almost always want the files to reside somewhere on your Docker system, and then to use the --volume option to mount the files on your Docker system to /var/www/html in the Nginx container..

docker run
--publish 80:80
--volume /path/to/html/files:/var/www/html
--volume /path/to/default.conf:/etc/nginx/conf.d/default.conf
nginx

 

And also in the PHP FPM container.

docker run
--volume /path/to/html/files:/var/www/html
--volume /path/to/www.conf:/usr/local/etc/php-fpm.d/www.conf
php:fpm

 

Ensure /usr/local/etc/php-fpm.d/www.conf in the PHP container has the following.

listen       = /run/php-fpm/www.sock
listen.owner = www-data
listen.group = www-data
listen.mode  = 0660

 

You will need to append a location block to the default.conf file for PHP. 

  • Notice in this example that fastcgi_pass unix:/run/php-fpm/www.sock, an exact match of the listen directive in the www.conf in the PHP container.
  • Notice the fastcgi_param directive contains $document_root$fastcgi_script_name. In this example, $document_root is /var/www/html. This means that both the nginx and PHP FPM containers will need the same exact files mounted to /var/www/html. $fastcgi_script_name will be the name of the PHP file that is being requested. For example, if requesting www.example.com/phpinfo.php, then $fastcgi_script_name will be phpinfo.php.
server {
    listen       80;
    server_name  stage.example.com;
    root         /var/www/html;
    index        index.html index.php;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

 




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 9c75b1 in the box below so that we can be sure you are a human.