After installing PHP and nginx using Docker, it took quite a while to configure nginx to run phP-fpm

1. Pull the official image

docker pull nginx
docker pull bitnami/php-fpm
Copy the code

2. Use the php-fPM image to start the php-FPM application container

docker run -d --name myFpm -v /var/www/html:/usr/share/nginx/html bitnami/php-fpm
Copy the code

-d Meaning of background container -v Specifies the mapping between the host and container. /var/www/html is the host project directory (custom), /usr/share/nginx/html is the default path of the nginx server project.

3. Open the nginx container

docker run -d --name myNginx -p 8080:80 -v /var/www/html:/usr/share/nginx/html nginx
Copy the code

-p: This parameter sets the relationship between ports. All urls to port 8080 of the host machine are forwarded to port 80 of the nginx container.

4. Check the PHp-fpm IP address to configure nginx

docker inspect myFpm | grep IPAddress

5. Modify the nginx configuration

Into the container

docker exec -it myNginx /bin/bash
Copy the code

-i: –interactive, indicates the interactive mode. -t: –tty, enable a dummy terminal. /bin/bash: it must be written. Otherwise, an error will be reported. This is when you start the pseudo-terminal and enter the bash interface, the command line interface.

View the location of the configuration file

/etc.nginx.conf.d/default.conf
Copy the code

6. Copy the configuration file to the usr directory

Go to the usr directory

/usr/share/nginx/html
Copy the code

If you do not have an nginx folder, create your own mkdir nginx folder

Copy the default configuration in the nginx container

docker cp myNginx:etc/nginx/conf.d/default.conf ./default.conf
Copy the code

This generates a default configuration in the current path

Editor vim default. Conf

Location ~ \.php${fastcgi_pass your php-fpmIP address :9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
        include        fastcgi_params;
    }
Copy the code

Save exit and synchronize the configuration to the container

docker cp ./default.conf myNginx:/etc/nginx/conf.d/default.conf
Copy the code

Go into the nginx container and reload the configuration

docker exec -it myNginx /bin/bash
service nginx reload
Copy the code

Is that it? It doesn’t exist

Access to 127.0.0.1:8080 / info. PHP

File not found.

Note the configuration in nginx

Location ~ \.php${fastcgi_pass 172.17.0.2:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
        include        fastcgi_params;
    }


Copy the code
fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name; 
Copy the code
/scripts is a directory name, which is the root directory of your site. For example, if you go to /index.php, you are going to go to /scripts/index.php on your operating system, which doesn't exist, and change it to the current nginx server path.Copy the code

127.0.0.1:8080/info.php displays the PHP extension page, and you’re done