>本教程通過使用Docker(當前的最佳實踐)來建立PHP開發環境。 我們將介紹PHP開發環境的演變,突出顯示Docker的優勢,例如手動安裝,預配置軟件包(XAMPP/WAMP)和Vagrant。
為什麼要docker?
Docker提供了幾個關鍵改進:
> >
>先決條件:下載並安裝docker。 在Linux上,您可能需要將用戶添加到>組中並啟動Docker Service。
docker
1。項目設置:
文件。 docker-compose.yml
>(nginx):
docker-compose.yml
>這使用了最新的NGINX映像,並在主機上的映射端口80到容器中的端口80。
<code class="language-yaml">version: '3' services: web: image: nginx:latest ports: - "80:80"</code>3。運行nginx:
>打開終端,導航到項目文件夾,然後運行
。 您應該在>中查看nginx的測試頁面
4。添加卷:docker-compose up
要訪問您的項目文件,將捲添加到http://127.0.0.1
>:
創建:
docker-compose.yml
<code class="language-yaml">version: '3' services: web: image: nginx:latest ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/conf.d/nginx.conf - ./app:/app</code>(用Ctrl c停止後)。
>
5。添加php:>將PHP服務添加到docker-compose.yml
>:
<code class="language-yaml">version: '3' services: web: image: nginx:latest ports: - "80:80"</code>更新
處理php文件:nginx.conf
<code class="language-yaml">version: '3' services: web: image: nginx:latest ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/conf.d/nginx.conf - ./app:/app</code>>
。重新啟動docker。 app/public/index.php
6。自定義PHP映像(帶有擴展):<?php phpinfo(); ?>
為了更好地控制,請創建一個具有擴展名的自定義PHP圖像:
創建:
修改PHP.Dockerfile
:
<code class="language-nginx">server { listen 80 default_server; root /app/public; }</code>
rundocker-compose.yml
和
<code class="language-yaml">version: '3' services: web: # ... (Nginx configuration) php: image: php:fpm volumes: - ./app:/app</code>
7。添加mysql(mariaDB):docker-compose build
>添加MariaDB服務:docker-compose up
> 結論:
<code class="language-nginx">server { listen 80 default_server; root /app/public; index index.php index.html index.htm; location ~ \.php$ { fastcgi_pass php:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }</code>
常見問題解答:
(提供的FAQ已經結構良好,可以直接包括在內。)>
以上是使用docker' data-gatsby-head =” true”/>