Home>Article>Development Tools> docker-composer quickly builds nginx+php environment
This article is provided by the tutorial column ofcomposerto introduce how to use docker-composer to build a simple nginx php environment. I hope it will be helpful to friends in need!
Directory structure
➜ Study tree ├── conf ├── docker-compose.yaml ├── nginx │ ├── conf │ │ └── laravel.conf │ └── html │ └── index.php
nginx.conf
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass php8:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /html$fastcgi_script_name; include fastcgi_params; }}Important note
fastcgi_pass php8:9000;php8:The name of the php container. If you want to configure multiple php versions, you only need to copy the php configuration and fill in the corresponding php Container name
php8: # php的容器名称 image: php:8.0-fpm restart: always volumes: - ./nginx/html:/html-------------------------------- php74: # 对应的nginx配置文件为:fastcgi_pass php74:9000; image: php:8.0-fpm restart: always volumes: - ./nginx/html:/htmlfastcgi_param SCRIPT_FILENAME /html$fastcgi_script_name;/html: PHP project is mapped to [php container directory] (red)
docker-compose
version: '3.5'services: nginx: image: nginx:latest restart: always ports: - 8010:80 volumes: - ./nginx/html/:/usr/share/nginx/html # 注意点一 - ./nginx/conf/:/etc/nginx/conf.d/ links: - php8 php8: image: php:8.0-fpm restart: always volumes: - ./nginx/html:/html #注意点二Note 1:
./nginx/html: Your php project address on this machine
/usr/share/nginx/html: nginx default access address
Note two:
./nginx/html: Your php project address on this machine
/html: The address here is to map your local php code to the php container. It is usually the same as the address configured by your nginx (red)
Tip: Please pay attention to two things The association of the red area, such a simple nginx php association environment has been configured successfully.
Pitfall guide:
When using
-link
, the custom port connecting to the container will be invalid, for exampleversion: '3.5'services: php8: image: php:8.0-fpm restart: always volumes: - ./nginx/html:/html links: # 如果使用 links ,当我们php程序中填写mysql端口的时候应该是 3306 而不是 3307,但是我们外部是需要用3307端口去连接mysql的 - mysql mysql: image: mysql:latest ports: - 3307:3306The above is the detailed content of docker-composer quickly builds nginx+php environment. For more information, please follow other related articles on the PHP Chinese website!