In the evolving landscape of web development, leveraging the power of containerization through Docker has become a game-changer, especially when deploying complex environments like a LAMP (Linux, Apache, MySQL, PHP) stack. This tutorial aims to take you through a seamless process of setting up a LAMP stack using Docker-Compose, with an added twist: we’ll include a step on customizing the PHP environment to include additional modules and Composer, the PHP dependency manager.
Prerequisites
Before we dive into the setup process, ensure you have the following installed on your system:
- Docker: A platform for developing, shipping, and running applications in containers.
- Docker-Compose: A tool for defining and running multi-container Docker applications.
You can check the installation of these tools by running docker -v
and docker-compose -v
in your terminal. If you haven’t installed them yet, visit the official Docker website for installation instructions.
Step 1: Create a Docker-Compose File
Start by creating a docker-compose.yml file in your project directory. This file is crucial as it orchestrates the setup of your LAMP stack, specifying how each service should be built and interact.
version: '3'
services:
web:
build: .
container_name: apache-php
ports:
- "80:80"
volumes:
- ./www:/var/www/html
db:
image: mysql:8
container_name: mysql-server
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: secure_password
MYSQL_DATABASE: mydb
MYSQL_USER: myuser
MYSQL_PASSWORD: password
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
This configuration details two main services: web for Apache and PHP, and db for MySQL. Notice the build: .
directive under web, indicating that Docker should use a Dockerfile in the current directory to build this service.
Step 2: Customize PHP with a Dockerfile
To incorporate additional PHP modules and Composer, create a Dockerfile in the same directory as your docker-compose.yml. This Dockerfile will instruct Docker on how to prepare your custom PHP environment.
FROM php:8.3-apache
# Install additional PHP modules
RUN docker-php-ext-install pdo_mysql mysqli
# Install Composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
php -r "unlink('composer-setup.php');"
# Enable Apache mod_rewrite
RUN a2enmod rewrite
This Dockerfile extends the php:8.3-apache image by adding the pdo_mysql and mysqli extensions for database interactions and installing Composer globally.
Step 3: Building and Running Your Containers
With your docker-compose.yml file ready, you can now launch your LAMP stack. Open a terminal, navigate to your project directory where the docker-compose.yml file is located, and run the following command:
docker-compose up -d
The -d
flag runs your containers in detached mode, freeing up your terminal. Docker-Compose will pull the necessary images from Docker Hub, create the defined services, and set up the network and volumes as specified.
Step 4: Accessing Your LAMP Stack
Once the containers are up and running, you can access your Apache server by visiting http://localhost in your web browser. You should see the default Apache page or the contents of your ./www directory if you’ve added any HTML or PHP files.
To interact with your MySQL database, you can use the following command to access the MySQL CLI:
docker-compose exec db mysql -uroot -proot
Replace root with the password you defined in your docker-compose.yml
file if necessary.
Step 5: Managing Your LAMP Stack
Docker-Compose makes it easy to stop, start, and rebuild your services. Use the following commands as needed:
- To stop your containers:
docker-compose down
- To start your containers again:
docker-compose up -d
- To rebuild your services after making changes:
docker-compose up -d --build
Conclusion
Deploying a LAMP stack using Docker and Docker-Compose not only simplifies the setup process but also ensures consistency across different development, testing, and production environments. By following this step-by-step tutorial, you’ve learned how to define your services in a docker-compose.yml file, run your containers, and manage your LAMP stack effortlessly. With these skills, you’re now equipped to deploy web applications quickly and efficiently, leveraging the power of containerization.