Home >Backend Development >PHP Tutorial >Setting Up a Modern PHP Development Environment with Docker' data-gatsby-head='true'/>
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.
Why Docker?
Docker offers several key improvements:
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.
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'/>