Home >Backend Development >PHP Tutorial >Setting Up a Modern PHP Development Environment with Docker' data-gatsby-head='true'/>

Setting Up a Modern PHP Development Environment with Docker' data-gatsby-head='true'/>
Christopher Nolan
Christopher NolanOriginal
2025-02-08 11:44:08149browse

This tutorial guides you through setting up a PHP development environment using Docker, the current best practice. We'll cover the evolution of PHP development environments, highlighting Docker's advantages over previous methods like manual installations, pre-configured packages (XAMPP/WAMP), and Vagrant.

Setting Up a Modern PHP Development Environment with Docker

Why Docker?

Docker offers several key improvements:

  • Resource Efficiency: Unlike virtual machines, Docker containers share the host OS kernel, consuming fewer resources.
  • Environment Consistency: Ensures your development and production environments are identical, minimizing deployment issues.
  • Isolation and Security: Containers isolate applications, preventing conflicts and enhancing security.
  • Scalability and Portability: Easily deploy and scale applications across different platforms.
  • Version Control: Manage different versions of PHP, MySQL, and Nginx easily within separate containers.

Setting Up a Modern PHP Development Environment with Docker

Setting up your Docker Environment:

Prerequisites: Download and install Docker. On Linux, you might need to add your user to the docker group and start the Docker service.

1. Project Setup: Create a project folder and a docker-compose.yml file.

2. docker-compose.yml (Nginx): This file defines your services. Start with Nginx:

<code class="language-yaml">version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"</code>

This uses the latest Nginx image and maps port 80 on your host to port 80 in the container.

3. Running Nginx: Open your terminal, navigate to your project folder, and run docker-compose up. You should see Nginx's test page at http://127.0.0.1.

4. Adding Volumes: To access your project files, add volumes to 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>

Create nginx.conf:

<code class="language-nginx">server {
    listen 80 default_server;
    root /app/public;
}</code>

Create app/public/index.html with your "Hello, World!" content. Restart Docker with docker-compose up (after stopping it with Ctrl C).

5. Adding PHP: Add a PHP service to docker-compose.yml:

<code class="language-yaml">version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"</code>

Update nginx.conf to process PHP files:

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

Create app/public/index.php with <?php phpinfo(); ?>. Restart Docker.

6. Custom PHP Image (with extensions): For better control, create a custom PHP image with extensions:

Create PHP.Dockerfile:

<code class="language-nginx">server {
    listen 80 default_server;
    root /app/public;
}</code>

Modify docker-compose.yml:

<code class="language-yaml">version: '3'
services:
  web:
    # ... (Nginx configuration)
  php:
    image: php:fpm
    volumes:
      - ./app:/app</code>

Run docker-compose build and docker-compose up.

7. Adding MySQL (MariaDB): Add a MariaDB service:

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

Connect to your database using a client like MySQL Workbench.

Conclusion: This comprehensive guide demonstrates how to create a robust and efficient PHP development environment using Docker. Remember to replace placeholder passwords with your own secure ones. The benefits of consistency, portability, and resource efficiency make Docker an invaluable tool for modern PHP development.

Setting Up a Modern PHP Development Environment with Docker

FAQs: (The provided FAQs are already well-structured and can be included directly.)

The above is the detailed content of Setting Up a Modern PHP Development Environment with Docker' data-gatsby-head='true'/>

Statement:
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