How to setup LAMP stack with Docker Compose

5 feb 2024 3 min di lettura
How to setup LAMP stack with Docker Compose
Indice dei contenuti

Introduction

In the evolving landscape of web development, leveraging the power of containerization via Docker has become a game changer, especially when deploying complex environments such as a LAMP (Linux, Apache, MySQL, PHP) stack. This tutorial aims to walk 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 your PHP environment to include additional modules and Composer, the PHP dependency manager.

Prerequisites

Before we dive into the setup process, make sure you have installed the following on your system:

  • Docker: A platform for developing, shipping, and running containerized applications.
  • Docker-Compose - A tool for defining and running multi-container Docker applications.

You can verify 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 configuration 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 setup details two main services: web for Apache and PHP and db for MySQL. Note the build:. directive under web, which indicates that Docker should use a Dockerfile in the current directory to create this service.

Step 2: Customize PHP with a Dockerfile

To embed 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: Create and run containers

With the docker-compose.yml file ready, you can now start your LAMP stack. Open a terminal, go 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 configure the network and volumes as specified.

Step 4: Access the LAMP stack

Once your 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 added 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

If necessary, replace root with the password you defined in the docker-compose.yml file.

Step 5: Manage the LAMP stack

Docker-Compose makes it easy to stop, start, and rebuild services. Use the following commands as needed:

  • To stop your containers: docker-compose down
  • To start containers again: docker-compose up -d
  • To rebuild 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, test, 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 are now able to deploy web applications quickly and efficiently, leveraging the power of containerization.

Support us with a

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.