TIP

# This article: Building nginx+PHP Access Thinkphp5 Using Docker Compose Locally on Windows

Preface: Following the previous article on deploying thinkphp5 locally on Windows using Docker+nginx+MySQL, this issue uses Docker Compose to build an environment to access the thinkphp5 program.

# Step 1: Preparation work

Check if Docker Compose is installed locally (cmd terminal input Docker Compose - v detection, Docker Desktop installation is default). Make sure you understand the basic Docker Compose syntax.

# Step 2: Create the Docker Compose.yml file

It's okay if you don't understand the Docker Compose syntax, to avoid unnecessary nonsense. Directly post Docker Compose.yml content

#docker-compose VERSION
version: '3'

# Creating a service can be understood as creating a container.
services:
  # Pulling nginx images
  nginx:
    # IMAGE version
    image: nginx:latest
    # EXPORT port 80
    ports:
      - "8001:80"
      #  Mount host files (host files: container files)
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - ./html:/var/www/html
      #  Use dependencies on to specify the dependency relationships between services
    depends_on:
      - php
    # The network on which the service depends  
    networks:
     - app-network
  # Pulling a PHP-FPM image
  php:
    image: php:7.4-fpm
    volumes:
      - ./html:/var/www/html
    networks:
    - app-network
# Define network    
networks:
   # NETWORK NAME
   app-network:
   # Network driver type
    driver: bridge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# Step 3: Start Docker Compose to build the service

  1. The default. conf content of the nginx configuration file is as follows:
server {
    listen 80;
    index index.php;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/app/public;  # The public directory pointing to the thinkphp program inside the container

    # location / {
    #     try_files $uri $uri/ /index.php?$query_string;
    # }
    # Hide entrance file index.php
    location / {
        if (!-e $request_filename){
            rewrite  ^(.*)$  /index.php?s=$1  last;   break;
        }
    }

    location ~ \.php$ {
        fastcgi_pass php:9000; # The PHP container name and port to be started, using localhost: 9000 locally, do not need to be changed in this example
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include fastcgi_params;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  1. The local file directory is as follows:

目录结构 目录结构

  1. Start Build: Start Docker Compose.yml Start the cmd terminal in the directory where the Docker Compose. yml file is located and enter Docker Compose up;

The successful startup result is as follows: - d indicates running in the background without outputting any results to the terminal. 目录结构

Upon checking the Docker Desktop, the result is as follows: it has been successfully built. docker desktop

Visit localhost: 8001 to view the effect: docker desktop

Are you very familiar with it, hahaha!

Conclusion: It's fun to configure using Docker Compose. Once all the above is configured, you can happily use Thinkph5 to write business.