Skip to content

Integration Nginx & PHP FPM

Iva edited this page Aug 23, 2024 · 9 revisions

Nginx and Wordpress Dockers

  • Configuration File Copies from the Host (Automatic Mode)

    • Execute the copy command via the container manager compose, to copy the default file from the WordPress container to the host, as described below:

      docker cp wordpress:/etc/php/7.4/fpm/pool.d/www.conf ./srcs/requirements/wordpress/conf/.
      docker cp nginx:/etc/nginx/sites-available/default ./srcs/requirements/nginx/conf/.

      👉🏼 Rename the copied default file to nginx.conf.

      👉🏼 The command above will only work if the wordpress and nginx containers are running.

      👉🏼 The execution of this command follows the structure below:

      ‼️ docker cp <container_name> : <original_file_path> <copy_file_path>.

      • To check the names of the running containers for WordPress, execute the docker ps command in the terminal, and a list of containers will appear. In the NAMES section, you will see the names of the running containers.
      • If for some reason your containers started without a predefined name, a random name will be assigned to them. Below are tips on how to identify each container (in this case, for the tutorial):
        1. wordpress: the container where the COMMAND section is "tail -f /dev/null" will be the WordPress container.
        2. nginx: the container where the COMMAND section is "nginx -g daemon; " will be the NGINX container.
      • Pay attention to the php-fpm version, as it may change according to the installation made when creating the docker image with the Dockerfile:
      • docker cp wordpress:/etc/php/version/fpm/pool.d/www.conf
      • Be mindful of the path where the <copy_file> will be stored; it should be considered starting from the /rootInception directory.
  • Customize NGINX Configuration

    • Uncomment the configuration in the default(nginx.conf) file copied from the NGINX container that allows NGINX to pass .php scripts to FastCGI:

      location ~ \.php$ { #Line to be uncommented
          include snippets/fastcgi-php.conf; #Line to be uncommented
          fastcgi_pass 127.0.0.1:9000;   #Line to be uncommented
      } #Line to be uncommented
    • Replace the unix socket configured by default in the default(nginx.conf) file copied from the NGINX container with the TCP socket of the WordPress container on port 9000:

      fastcgi_pass 127.0.0.1:9000;   #Original line
      fastcgi_pass wordpress:9000;   #Replaced line
    • Add to the default(nginx.conf) file copied from the NGINX container, so that the NGINX server can consider .php files:

      index index.html index.htm index.nginx-debian.html;   #Original line
      index index.php index.html index.htm index.nginx-debian.html;   #Line with addition
    • Add to the Dockerfile of NGINX the command to copy the modified nginx.conf configuration file into the /etc/nginx/sites-available/ directory in the NGINX container, replacing the default file:

      FROM debian:bullseye 
      RUN apt update && apt upgrade -y && apt install -y nginx
      COPY conf/nginx.conf /etc/nginx/sites-available/default   #Added line
      ENTRYPOINT ["nginx", "-g", "daemon off;"]
  • Customize PHP-FPM Configuration

    • Replace the socket where the PHP-FPM backend will listen in the www.conf file copied from the WordPress container so that PHP-FPM accepts requests from FastCGI on the localhost of the WordPress container on port 9000:

      listen = /run/php/php7.4-fpm.sock   #Original file line
      listen = wordpress:9000   #Replaced file line
    • Add the command to create the /run/php directory, used by the PHP-FPM backend to store temporary and state files necessary for its proper functioning, and the command to change the ownership of the directory to www-data, the default user used by PHP-FPM in the Dockerfile of WordPress:

      FROM debian:bullseye
      RUN apt update && apt upgrade -y && apt install -y php7.4-fpm 
      RUN mkdir -p /run/php && chown -R www-data:www-data /run/php   #Added line
      ENTRYPOINT ["tail", "-f", "/dev/null"]

      👉🏼 The temporary files created by PHP-FPM and stored in the /run/php directory can include:

      1. /run/php/php-fpm.pid: This file contains the Process ID (PID) of the main PHP-FPM process. It is used to monitor and manage the running PHP-FPM process.
      2. /run/php/php7.4-fpm.sock: If PHP-FPM is configured to use Unix sockets instead of TCP ports, the socket will be created in this directory. Unix sockets are used for efficient communication between PHP-FPM and web servers like NGINX or Apache.
    • Add the command to copy the modified www.conf configuration file into the /etc/php/7.4/fpm/pool.d/ directory in the WordPress container to replace the original file with the customized one in the Dockerfile of WordPress:

      FROM debian:bullseye
      RUN apt update && apt upgrade -y && apt install -y php7.4-fpm 
      RUN mkdir -p /run/php && chown -R www-data:www-data /run/php
      COPY conf/www.conf /etc/php/7.4/fpm/pool.d/. #Added line
      ENTRYPOINT ["tail", "-f", "/dev/null"]
    • Replace the entire prohibited tail -f command line in the Dockerfile of WordPress with the php-fpm7.4 execution command that will run the PHP-FPM backend immediately after the container is created from the Docker image:

      FROM debian:bullseye
      RUN apt update && apt upgrade -y && apt install -y php7.4-fpm 
      RUN mkdir -p /run/php && chown -R www-data:www-data /run/php
      COPY conf/www.conf /etc/php/7.4/fpm/pool.d/.
      
      ENTRYPOINT ["tail", "-f", "/dev/null"] #Command before
      ENTRYPOINT ["php-fpm7.4", "-F"] #Command after
  • Docker Compose File Update

    • Add a volume to the docker-compose.yml file responsible for gathering the website data to be accessed by the NGINX server container and PHP-FPM in the WordPress container. Link the directory where the volume will be stored with the var/www/html directories of each container:

      version: "3.8"
      
      services:
        nginx:
          build: requirements/nginx/.
          container_name: nginx
          ports:
            - "80:80"
          volumes: #Added
            - .web:/var/www/html #Added
        wordpress:
          build: requirements/wordpress/.
          container_name: wordpress
          volumes: #Added
            - .web:/var/www/html #Added

      👉🏼 The directory structure created by the docker-compose.yml file will be modified, adding the rootInception/srcs/web/ directory if it does not exist, and considering it if it does exist. This directory will be responsible for storing the volume to be shared by the wordpress and nginx containers.

      👉🏼 The project requirements specify that the volume directory should be located at /home/login/data/ within the host. For this reason, the .web directory will be renamed to the required path later on.

    Test NGINX and PHP-FPM Interaction

    • Test the web volume created by creating an index.html and index.php file with the indicated contents below:

      <h1> It works </h1> 
      <?php phpinfo(); ?>

      👉🏼 For the test to work, clear all created Docker images and recreate them from the updated Dockerfile and docker-compose file configurations.

    • Enter the URL http://127.0.0.1/ in your preferred browser to test the volume and its sharing with NGINX and PHP.

      👉🏼 Upon confirming the address with ENTER, the browser is expected to display a default page with PHP-FPM information. Since the .php file will be prioritized by NGINX over .html, you can also test this priority by deleting the index.php file from the .web/ directory and reloading the browser. If it now displays the content of index.html, everything is configured correctly.

    ⏮️ Previous
    Next ⏭️

Clone this wiki locally