|

Setting Up a PHP Development Environment on Windows Using Docker

Share

Introduction

Setting up a PHP development environment can be a daunting task, especially when you want to ensure consistency across different setups. Docker simplifies this process by providing a containerized environment that can be easily replicated. In this guide, we’ll walk you through the steps to set up a PHP development environment on Windows using Docker, including Apache, MySQL, and phpMyAdmin.

You can set up a robust PHP development environment on Windows using Docker. This setup ensures consistency and makes it easy to manage your development environment.

Prerequisites

Before we begin, make sure you have the following installed on your Windows machine:

  • Docker Desktop

Step 1: Install Docker

  1. Download Docker Desktop: Go to the Docker website and download Docker Desktop for Windows.
  2. Install Docker Desktop: Follow the installation instructions. Make sure to enable the required Windows features ( WSL 2) during the installation process.
  3. Start Docker Desktop: Once installed, start Docker Desktop and ensure it’s running.

Step 2: Create a Dockerfile

Create a Dockerfile in your project directory. This file will define the PHP environment.

dockerfile

# Use the official PHP 8.2 image
FROM php:8.2-apache

# Install necessary PHP extensions
RUN docker-php-ext-install pdo pdo_mysql

# Enable Apache mod_rewrite
RUN a2enmod rewrite

# Copy project files to the container
COPY . /var/www/html

# Set working directory
WORKDIR /var/www/html

Step 3: Create a Docker Compose File

Create a docker-compose.yml file in your project directory. This file will define the services (like PHP, MySQL, and phpMyAdmin) that your application needs.

yaml

version: '3.8'

services:
  web:
    build: .
    ports:
      - "7080:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - db
    networks:
      - mynetwork

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - mynetwork

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - "8080:80"
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: root
    networks:
      - mynetwork

networks:
  mynetwork:
    driver: bridge

volumes:
  db_data:

Step 4: Build and Run the Docker Containers

  1. Open a terminal: PowerShell or cmd terminal ok!.
    Navigate to your project directory.
  2. Build the Docker image: Run the following command to build the Docker image:
docker-compose build
  1. Start the Docker containers: Run the following command to start the containers:

docker-compose up

Step 5: Access Your PHP Application

Open your web browser and go to http://localhost:7080. You should see your PHP application running with PHP 8.2.

Step 6: Access phpMyAdmin

Open your web browser and go to http://localhost:8080. You should see the phpMyAdmin login page. Use the following credentials to log in:

  • Server: db
  • Username: root
  • Password: root

Step 7: Place Your Code

can you expand a little of where the /var/www/html is in my local machine?

Certainly! Let’s clarify how the /var/www/html directory in the Docker container relates to your local machine.

Understanding /var/www/html in Docker

When you use Docker, the /var/www/html directory is inside the Docker container. This directory is the default document root for the Apache web server in the container. However, this directory doesn’t exist on your local machine in the same way. Instead, Docker maps a directory from your local machine to this directory inside the container.

Mapping Local Directory to Container Directory

In your docker-compose.yml file, you have a volume mapping that looks like this:

yaml

services:
  web:
    build: .
    ports:
      - "7080:80"
    volumes:
      - .:/var/www/html

This line:

yaml

- .:/var/www/html

means that the current directory on your local machine (denoted by .) is mapped to the /var/www/html directory inside the Docker container.

Example Directory Structure

Here’s an example of how your project directory might look on your local machine:

C:\Users\YourUsername\YourProject
|-- Dockerfile
|-- docker-compose.yml
|-- index.php
|-- other-files-and-directories

Keep in mind that c:\Users\YourUsername\YourProject is just an example that depends on where you created the docker image in my case i started all of this at: c:\LocalData\DevEnv

Place your PHP code in the root directory of your project, where your Dockerfile and docker-compose.yml are located. This directory will be mapped to the container’s /var/www/html directory.

You should place your PHP code and other project files in the same directory where your Dockerfile and docker-compose.yml are located. For example, if your project directory on Windows is C:\Users\YourUsername\YourProject, you should place your PHP files and other project files in this directory.

Where to Place Your Code

You should place your PHP code and other project files in the same directory where your Dockerfile and docker-compose.yml are located. For example, if your project directory on Windows is C:\Users\YourUsername\YourProject, you should place your PHP files and other project files in this directory.

Create an index.html that has <h1>Hello You are Here!</> inside the at the YourProject directory and go to

PHP Application: http://localhost:7080

phpMyAdmin: http://localhost:8080

UPDATING THE Docker Container:

So if you need to change or update the docker contain you can delete and rebuild from scratch after you update files: dockerfile and docker-compose.yml

docker-compose down
docker-compose build
docker-compose up

Alternatively: You can YOUDATE the running container without rebuilding it from scratch!

  1. Update Your Dockerfile or docker-compose.yml: Make any necessary changes to your Dockerfile or docker-compose.yml file.
  2. Run the Update Command: Use the following command to update your running containers:
docker-compose up --build

This command will rebuild the services that have changed and restart the containers, applying the updates without needing to stop and remove the existing containers manually.

Adding WordPress to your development Environment

I decided that at this time, want to keep WordPress outside my docker container ( do not have any good reasons other than the fact that I may need to delete /change/ update the core at any time. What I did next:

  1. Download zip file with latest copy WordPress from wordpress.org
  2. create a directory called wordpress off the YourProject directory
  3. the extract zip file to the …\YourProject\wordpress
  4. go to http://localhost:7080/wordpress
  5. and initiate the wordpress setup
    • Note 1: when asked for the Database credentials, use whatever you used in the YML file above. In my case it was:
    • MYSQL Parameters:
    • Host: db
    • Port: 3306 (default MySQL port so no need to enter)
    • Database Name: mydatabase
    • User: user
    • Password: password
  6. Then I tried to install a plugins, and it asked me for FTP credentials, so I had to add the following line to the wp-config.php file:
    • define(‘FS_METHOD’, ‘direct’);

Similar Posts