docker-composer快速建置nginx+php環境

藏色散人
發布: 2022-01-18 16:09:56
轉載
3633 人瀏覽過

本文由composer教學專欄為大家介紹怎麼使用docker-composer建構一個簡單nginx php環境,希望對需要的朋友有幫助!

目錄結構

➜  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;
 }}
登入後複製

#重點說明

fastcgi_pass   php8:9000;
登入後複製

php8: php容器的名稱,如果你想要設定多個php版本,只要將php的設定複製一份就可以,填入對應的php容器名稱

 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
登入後複製
fastcgi_param  SCRIPT_FILENAME  /html$fastcgi_script_name;
登入後複製

/html :php專案對應到【php 容器的目錄】(紅色)

##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 #注意点二
登入後複製

注意點一:

./nginx/html :本機你的php專案位址

/usr/share/nginx/html: nginx預設的存取位址

注意點二:

./nginx/html :本機你的php專案位址

/html: 這裡位址是將你本地的php程式碼對應到php的容器當中,一般是和你nginx配置的位址是一致的(紅色)

Tip:請留意兩處紅色的區域的關聯,這樣一個簡單的nginx php關聯的環境就配置成功了。

踩坑指南:

當使用

-link# 時,連接容器的自訂連接埠將失效,並舉例

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
登入後複製

以上是docker-composer快速建置nginx+php環境的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:learnku.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!