docker deployment nginx+php

docker deployment nginx+php

Recently, I was learning about docker. I was going to build the nginx + php development environment. I recorded the building process. First, I gave a copy of docker-compose.yml deployment configuration

version: '3'
services:
  nginx:
    container_name: mynginx
    image: nginx
    ports:
      - '80:80'
    volumes:
      - '/etc/nginx/:/etc/nginx' #to configure
      - '/var/log/nginx/:/var/log/nginx/' #journal
      - '/data/php/yxyy/html:/data/php/yxyy/html' #Project path
  php:
    container_name: myphp
    image: php:7.2-fpm
    volumes:
      - '/data/php/yxyy/html/:/data/php/yxyy/html/'
      - '/usr/local/php7.2/:/usr/local/etc/'

The following is my whole learning process of building. The installation of dockers will not be recorded. The following records the building process of nginx+php (there are problems in the front, don't copy it)

nginx

Install a temporary nginx container first

docker run --name tmpnginx -d -p 80:80  nginx

Enter 127.0.0.1 in the browser to display the familiar interface, and enter the tmpnginx container

docker exec -it tmpnginx /bin/bash

Search profile location

find / -name nginx.conf  #Find the configuration file in / ect/nginx

Copy a copy of the container nginx configuration to the host / ect/nginx

docker cp tmpnginx:/ect/nginx /ect/nginx

Modify the nginx configuration, such as the site root directory of / etc/nginx/conf.d/default.conf. My project is in / data/php/yxyy/html

Delete the tmpnginx container and start a container to mount custom configuration files

docker run --name mynginx -d -p 80:80 \
           -v /ect/nginx:/ect/nginx \ 
           -v /var/log/nginx/:/var/log/nginx/ \
           -v /data/php/yxyy/html/:/data/php/yxyy/html/   nginx

Profile description

  1. /ect/nginx:nginx configuration directory
  2. /var/log/nginx:nginx log
  3. /data/php/yxyy/html: project directory

Enter 127.0.0.1 on the browser to verify that it is normal

PHP

The installation process of php is also similar to nginx. The image I use is php:7.2-fpm. Here is my installation process:

docker run --name tmpphp -d -p 80:80  php:7.2-fpm  #Install a temporary php:7.2-fpm container
docker exec -it tmpphp /bin/bash #Enter container
find / -name php-fpm.conf  #Find the configuration file in / usr/local/etc /
docker cp tmpnginx:/usr/local/etc/ /usr/local/php7.2/  #Copy a copy of the container configuration
docker run --name mynginx -d -p 9000:9000 \
           -v /usr/local/php7.2/:/usr/local/etc/ \ 
           -v /data/php/yxyy/html/:/data/php/yxyy/html/   php:7.2-fpm 

At this point, I think it's OK. Visit the browser and the result shows 502

502

View nginx logs as follows:

 *19 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"

There is a problem with the connection. Check whether the PHP container is up:

docker ps  #The results show that the php container is up

View the php container log:

docker logs myphp #No mistake

View host processes:

ps -ef | grep php  #php master and worker processes are also available. Pit father

Now that the processes have, there can only be a problem with their communication. telnet on the host:

telnet 127.0.0.1 9000  #It can be connected normally

Can't the containers communicate with each other? It's true to look for the data, but I want to verify:

#Enter the mynginx container telnet 127.0.0.1 9000, no telnet

#OK, then install it. After installation, telnet 127.0.0.1 9000 refuses. Come here to confirm that direct communication is not possible

Rerun the nginx container and add -- link myphp

docker run --name mynginx -d -p 80:80 --link myphp \
           -v /ect/nginx:/ect/nginx \ 
           -v /var/log/nginx/:/var/log/nginx/ \
           -v /data/php/yxyy/html/:/data/php/yxyy/html/   nginx

The ideal is beautiful, the reality is cruel, and communication is still not possible. According to the data, link is going to be abandoned. The docker I installed is relatively new. I guess this may be the reason. Start it by using docker compose cluster management. Here is the docker-compose.yml configuration:

docker-compose up -d
version: '3'
services:
  nginx:
    container_name: mynginx
    image: nginx
    ports:
      - '80:80'
    volumes:
      - '/etc/nginx/:/etc/nginx' #to configure
      - '/var/log/nginx/:/var/log/nginx/' #journal
      - '/data/php/yxyy/html:/data/php/yxyy/html' #Project path
  php:
    container_name: myphp
    image: php:7.2-fpm
    volumes:
      - '/data/php/yxyy/html/:/data/php/yxyy/html/'
      - '/usr/local/php7.2/:/usr/local/etc/'

After a meal, the operation was as fierce as a tiger. When I saw the result of 250, I still couldn't. at this time, I wanted to smash the computer and enter the container:

telnet 127.0.0.1 9000  #Or can't you connect? What will happen if you change 127.0.0.1 to the container name?
telnet myphp 9000  #Unexpectedly, at this time, a sense of happiness came leisurely. Happiness came too suddenly

Modify the nginx configuration, change 127.0.0.1 to myphp (container name) and restart. The highlight time comes, but goose. In fact, I thought too much. 502 was solved and 404 met unexpectedly

404

File not found

When I saw 404, my first reaction was that the path was wrong. After repeated confirmation, the path was OK. I think it can only be a permission problem. Check the nginx log:

*32 FastCGI sent in stderr: "Primary script unknown" while reading response header

ls -l you really don't have permission to view. When you see that you don't have permission, you smile. It's a little fun. Give me 777 permission. Unfortunately, I still can't, since I can't

Let the root user execute it all. Modify the configuration nginx.conf, user = root, www.conf configuration of PHP FMT, user = root, group = root, and restart the container to check. View the container log docker logs phpxxx display

ERROR: [pool www] please specify user and group other than root

php can't be executed by root. It's all changed to nginx. The result is still not good,

cat /etc/passwd check that there is no such user, only the WWW data user of php. Modify the nginx and php configurations, change them to the WWW data user, and restart.

Finally

Tags: PHP Docker Nginx

Posted on Fri, 26 Nov 2021 02:36:15 -0500 by dotbands