Docker's Nginx with PHP

1. Purpose

The PHP container and container Nginx communicate, began to build PHP development environment

2. Configure Nginx

拉取Nginx镜像
localhost:nginx_fpm sukangshen$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
743f2d6c1f65: Already exists
6bfc4ec4420a: Pull complete
688a776db95f: Pull complete
Digest: sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68
Status: Downloaded newer image for nginx:latest
Use nginx nginx mirroring application container opening
localhost:nginx_fpm sukangshen$ docker run -d --name myNginx -p 8082:80  -v ~/www/my_test/nginx_fpm/html:/usr/share/nginx/html nginx
9da9bbb80a88650ca91a45bc2fa2fdc7dab59b50392ecf34edfd8e3c118d20ef

3. Configure PHP

Pull PHP image

localhost:nginx_fpm sukangshen$ docker pull php:7.2-fpm
7.2-fpm: Pulling from library/php
743f2d6c1f65: Pull complete
6307e89982cc: Pull complete
807218e72ce2: Pull complete
5108df1d03f8: Pull complete
82ffe3f78be2: Pull complete
17c66bee581c: Pull complete
03011f3bcf78: Pull complete
28b6a4179c3e: Pull complete
b77fa38a9e9c: Pull complete
79928611774d: Pull complete
Digest: sha256:c50c0cffd2af10291d8b8fe3dd410aed3eca302b8163e2ab34cd1bfc6feae495
Status: Downloaded newer image for php:7.2-fpm
Php-fpm using mirroring application container opening php-fpm
localhost:nginx_fpm sukangshen$ docker run -d --name myFpm -p 9000:9000 -v ~/www/my_test/nginx_fpm/html:/usr/share/nginx/html php:7.2-fpm
e040f89262732ec8e3951ce38c444fa23f5693f943fc1860e98bbec63dec41a1

3. Check whether a successful start

localhost:nginx_fpm sukangshen$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
9da9bbb80a88        nginx               "nginx -g 'daemon of…"   18 minutes ago      Up 18 minutes       0.0.0.0:8082->80/tcp     myNginx
e040f8926273        php:7.2-fpm         "docker-php-entrypoi…"   18 minutes ago      Up 18 minutes       0.0.0.0:9000->9000/tcp   myFpm

4. View the IP address of PHP

docker inspect 容器名  | grep "IPAddress"
localhost:nginx_fpm sukangshen$ docker inspect myFpm | grep "IPAddress"
Error: No such object: myFpm
localhost:nginx_fpm sukangshen$ docker inspect optimistic_varahamihira | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

5. Modify the configuration nginx

In the container is not vim command, so you can not modify the configuration file directly in the container. So we have to solve this problem through alternative ways, or only vim installed in each container.

First, log on to the corresponding container, check the configuration path, which will be used in the following modifications.

docker exec -it myNginx /bin/bash

##查看nginx的配置文件
/etc/nginx/conf.d/default.conf

Exit the command line, do not use the exit, because the exit vessel will stop. As used herein, ctrl + p + q to exit the container.
Copy using a dedicated configuration file copy command to the host, then (this is the alternative method) in the host editor

docker cp myNginx:/etc/nginx/conf.d/default.conf ./default.conf

Here used to query the last step of the configuration file path information

In the host portion php modify the configuration file, as follows:

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;
}

Again use the Copy command to copy it into the container and then into the container again, nginx reload configuration file

docker cp ./default.conf myNginx:/etc/myNginx:/etc/nginx/conf.d/default.conf

Nginx into the container reload the configuration file

docker exec -it myNginx /bin/bash
service nginx reload

Then a successful visit, good night

14585178-a9ed10c2a00e34b7.png
image.png

Guess you like

Origin blog.csdn.net/weixin_33762130/article/details/91030247