Home > Development Tools > composer > docker-composer quickly builds nginx+php environment

docker-composer quickly builds nginx+php environment

藏色散人
Release: 2022-01-18 16:09:56
forward
3868 people have browsed it

This article is provided by the tutorial column of composer to 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
Copy after login

index.php

<?php
/**
 * Created by OrangBus
 * User email: orangbus40400@gmail.com
 * website: orangbus.cn
 * blog: doc.orangbus.cn
 * github: github.com/orangbus
 */echo phpinfo();
Copy after login

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;
 }}
Copy after login

Important note

fastcgi_pass   php8:9000;
Copy after login

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:/html
Copy after login
fastcgi_param  SCRIPT_FILENAME  /html$fastcgi_script_name;
Copy after login

/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 #注意点二
Copy after login

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 example

version: '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:3306
Copy after login

The 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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template