本文由composer教程欄目給大家介紹怎么使用docker-composer構(gòu)建一個(gè)簡(jiǎn)單nginx+php環(huán)境,希望對(duì)需要的朋友有所幫助!
目錄結(jié)構(gòu)
? Study tree ├── conf ├── docker-compose.yaml ├── nginx │ ├── conf │ │ └── laravel.conf │ └── html │ └── index.php
index.php
<?php /** * Created by OrangBus * User email: orangbus40400@gmail.com * website: orangbus.cn * blog: doc.orangbus.cn * github: github.com/orangbus */echo phpinfo();
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; }}
重點(diǎn)說(shuō)明
fastcgi_pass php8:9000;
php8: php容器的名稱,如果你想配置多個(gè)php版本,只需要將php的配置復(fù)制一份就可以,填寫對(duì)應(yīng)的php容器名稱
php8: # php的容器名稱 image: php:8.0-fpm restart: always volumes: - ./nginx/html:/html-------------------------------- php74: # 對(duì)應(yīng)的nginx配置文件為:fastcgi_pass php74:9000; image: php:8.0-fpm restart: always volumes: - ./nginx/html:/html
fastcgi_param SCRIPT_FILENAME /html$fastcgi_script_name;
/html :php項(xiàng)目映射到 【php 容器的目錄】(紅色)
docker-compose
version: '3.5'services: nginx: image: nginx:latest restart: always ports: - 8010:80 volumes: - ./nginx/html/:/usr/share/nginx/html # 注意點(diǎn)一 - ./nginx/conf/:/etc/nginx/conf.d/ links: - php8 php8: image: php:8.0-fpm restart: always volumes: - ./nginx/html:/html #注意點(diǎn)二
注意點(diǎn)一:
./nginx/html :本機(jī)你的php項(xiàng)目地址
/usr/share/nginx/html: nginx默認(rèn)的訪問(wèn)地址
注意點(diǎn)二:
./nginx/html :本機(jī)你的php項(xiàng)目地址
/html: 這里地址是將你本地的php代碼映射到php的容器當(dāng)中,一般是和你nginx配置的地址是一致的 (紅色)
Tip:請(qǐng)留意兩處紅色的區(qū)域的關(guān)聯(lián),這樣一個(gè)簡(jiǎn)單的nginx+php關(guān)聯(lián)的環(huán)境就配置成功了。
踩坑指南:
當(dāng)使用-link
時(shí),連接容器的自定義端口將失效,舉例
version: '3.5'services: php8: image: php:8.0-fpm restart: always volumes: - ./nginx/html:/html links: # 如果使用 links ,當(dāng)我們php程序中填寫mysql端口的時(shí)候應(yīng)該是 3306 而不是 3307,但是我們外部是需要用3307端口去連接mysql的 - mysql mysql: image: mysql:latest ports: - 3307:3306