Setting up a local development environment that includes PHP, Apache, and MySQL can be a complex task. However, with the power of Docker and Docker Compose, this process becomes significantly easier and more efficient. In this article, we’ll walk you through the step-by-step process of deploying PHP, Apache, and MySQL using Docker Compose. We’ll cover the necessary configuration, provide examples, and showcase sample outputs along the way.

Advertisement

Prerequisites

Before getting started, make sure you have Docker and Docker Compose installed on your system. You can download and install Docker from the official Docker website (https://www.docker.com/) and Docker Compose from the Docker Compose documentation (https://docs.docker.com/compose/install/). Once you have both installed, you’re ready to proceed.

Step 1: Setting Up the Docker Compose File

Create a new directory for your project and navigate into it using your terminal or command prompt. Inside this directory, create a new file called `docker-compose.yml` and open it with a text editor of your choice. This file will contain the configuration for our PHP, Apache, and MySQL services.

Here’s a sample `docker-compose.yml` file:

In this configuration, we define two services: `php` and `mysql`. The `php` service is responsible for running our PHP code using Apache, and the mysql service is for our MySQL database. The `volumes` section mounts the src directory (where our PHP code resides) to the `/var/www/html` directory inside the php service container. The `ports` section maps port 8080 on the host machine to port 80 inside the php service container. The `depends_on` section ensures that the mysql service starts before the php service to establish the necessary connection.

Step 2: Creating a Dockerfile

Next, we need to create a Dockerfile that defines the environment for our PHP and Apache service. In the same directory as the `docker-compose.yml` file, create a new file called `Dockerfile` and open it with a text editor.

Here’s a sample Dockerfile:

This `Dockerfile` uses the official PHP image with Apache as the base image. It copies the contents of the `src` directory (where our PHP code resides) into the `/var/www/html/` directory inside the container.

Step 3: Building and Running the Docker Compose Services

Now that we have our `docker-compose.yml` file and Dockerfile in place, we can build and run the Docker Compose services. In your terminal or command prompt, navigate to the project directory (where the docker-compose.yml file is located) and run the following command:

docker-compose up -d 

This command starts the services defined in the `docker-compose.yml` file in detached mode (in the background). Docker Compose will automatically build the php service using the Dockerfile and pull the MySQL image from Docker Hub if it’s not already available locally.

Step 4: Verifying the Deployment

Once the services are up and running, we can verify the deployment by accessing the PHP application in our web browser. Open your preferred web browser and visit http://localhost:8080. You should see your PHP application running successfully.

Conclusion

In this article, we explored the process of deploying PHP, Apache, and MySQL using Docker Compose. We covered the configuration in the `docker-compose.yml` file, provided a sample `Dockerfile`, and demonstrated the deployment process. Docker Compose simplifies the setup and management of complex development environments, making it easier to work with PHP, Apache, and MySQL. With this knowledge, you can now effortlessly set up and deploy your PHP projects using Docker Compose.

I hope this article helps you in setting up your PHP, Apache, and MySQL environment with Docker Compose. Happy coding!

Share.
Leave A Reply

Exit mobile version