Using docker compose to quickly build php7+nginx+mysql5.7+redis environment in centos7

Posted by goosez22 on Sun, 29 Dec 2019 18:57:17 +0100

1. Install docker and docker compose

centos7 installs docker and docker compose

2. Docker compose common commands

#  Build the container under the folder containing docker-compose.yml
#  If you use the Dockerfile to modify the Dockerfile, execute the following again to apply the modification
docker-compose up -d

#  Stop all containers in docker-compose.yml
docker-compose stop

#  Delete all containers in docker-compose.yml
docker-compose rm

#  View the log of nginx in docker-compose.yml
docker-compose logs -f nginx

#  Restart a container in docker-compose.yml
docker-compose restart nginx
####  start start-up stop Stop it #####

3. Contents of docker-compose.yml file

Because mysql redis php nginx is installed on the host, the following ports are not default

version : '2'
services :
    mysql :
        build : ./mysql # Using the Dockerfile file
        ports :
            - "3366:3306" # Host port: container port
        environment :
            - MYSQL_ROOT_PASSWORD=123456 # Set the root password of mysql
        volumes:
            - /home/docker/mysql/data:/var/lib/mysql:rw # mysql data file
        networks:
            - mysql-net # Join the network
        container_name : mysql57 # Set container name
    redis :
        build : ./redis
        ports :
            - "127.0.0.1:6398:6379" # If you do not need to set the ip address of 127.0.0.1 in the external network access container
        environment :
            - appendonly=yes # Turn on redis password settings
            - requirepass=123456 # Set redis password
        networks:
            - redis-net
        container_name : redis40
    php :
        build : ./php
        ports :
            - "127.0.0.1:9800:9000"
        volumes :
            - /home/docker/web:/var/www/html:rw # web site directory
        networks:
            - php-net
            - mysql-net
            - redis-net
        container_name : php72
    nginx :
        build : ./nginx
        ports :
            - "8080:80" # If the host has nginx or apache installed and running, it needs to map to other ports
            - "8081:81" # Set up multiple sites
            - "8082:82"
            - "8083:83"
        depends_on :
            - "php"
        volumes :
            - /home/docker/web:/var/www/html:rw
            - /home/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
            - /home/docker/nginx/server.conf:/etc/nginx/conf.d/default.conf:ro
        networks:
            - php-net
        container_name : nginx114
networks: # Create network
    php-net:
    mysql-net:
    redis-net:

To view other documents, please click

4. If the host and the docker use the nginx access method at the same time

First, as configured above, the nginx address of the access container is = = > host IP address: Port = = > x x x. XXX. X.x: 8080

But there will be a problem = > > the host computer has turned on the firewall and checked that there is no port 8080 through the firewall, while the Internet can also visit port 8080 for web browsing

The second one is to prevent the Internet from accessing port 8080 on the basis of the first one

Step 1 = > modify the nginx configuration port in the docker-compose.yml file as follows

        ports :
            - "127.0.0.1:8080:80"
            - "127.0.0.1:8081:81"
            - "127.0.0.1:8082:82"
            - "127.0.0.1:8083:83"

The significance of this is that port 8080 can only be accessed by the host computer, and the external network scan cannot reach port 8080

For example, when configuring a port, you can only fill in the port so that all the external IP addresses can be accessed and automatically pass through the firewall, but there is no 8080 port or service in the firewall

Part 2: configure the nginx site of the host and the nginx site in the docker

Host nginx site

server {
        listen       80;
        server_name  xx1.xx.com;

        location / {
                proxy_pass http://127.0.0.1:8080;
        }

    }

server {
        listen       80;
        server_name  xx2.xx.com;
        location / {
                proxy_pass http://127.0.0.1:8081;
        }

    }

server {
        listen       80;
        server_name  xx3.xx.com;

        location / {
           proxy_pass http://127.0.0.1:8082;
         }
    }

server {
        listen       80;
        server_name  xx4.xx.com;

        location / {
           proxy_pass http://127.0.0.1:8083;
        }

    }

There are only two nginx sites in the container

server {
        listen       80;
        server_name  127.0.0.1;
        root         /var/www/html/web1;

        location / {
			index  index.html index.htm index.php l.php;
			if (!-e $request_filename) {
				rewrite  ^(.*)$  /index.php?s=/$1  last;
				break;
    		}
			autoindex  off;
        }

        location ~ \.php(.*)$  {
            fastcgi_pass   php72:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

server {
        listen       81;
        server_name  127.0.0.1;
        root         /var/www/html/web2;

        location / {
			index  index.html index.htm index.php l.php;
			if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
            }
			autoindex  off;
        }

        location ~ \.php(.*)$  {
            fastcgi_pass   php72:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

 

Topics: Docker Nginx PHP MySQL