sábado, 17 de junio de 2023

Install lamp on docker



With only these 5 files, we will be able to build an apache server containing PHP, a MySQL server, and to communicate the two to use our website.

These 5 files will have the following role:

  • app/index.php: The entry point of our website, we will write our PHP code there
  • build/mysql/Dockerfile: Docker configuration file that describes how to install MySQL
  • build/php/Dockerfile: Docker configuration file that describes how to install Apache and PHP
  • docker-compose.yml: Docker-compose configuration file, which when executed will launch the containers and run our architecture.

Writing Docker configuration

As you will have understood, all the configuration will be done within the 3 files concerning Docker. We will have two containers: One for PHP and Apache, and another for MySQL. Let’s see how to proceed:

docker-compose.yml

docker-compose.yml
version: "3.9" services: php-apache: ports: - "80:80" build: './build/php' volumes: - ./app:/var/www/html mysql: ports: - "3306:3306" build: './build/mysql' environment: MYSQL_ROOT_PASSWORD: "super-secret-password" MYSQL_DATABASE: "my-wonderful-website" volumes: - dbData:/var/lib/mysql volumes: app: dbData:

This is what our docker-compose.yml will look like, and it won’t move. There isn’t much, is there? Let’s break down its content anyway:

We find two services, which represent our two containers:

  • php-apache, will be accessible from port 80 of our machine which is the default HTTP port. The site will therefore be directly accessible from localhost. It declares that all your code, available in the “app” folder, will then be mapped to the root of the apache server, which is “/var/www/html”. Finally, the build part will define the Apache and PHP installation process, which will be in the Dockerfile of the “build/php” directory, which we will detail just after.
  • mysql, will be accessible from port 3306 of our machine which is the default port of MySQL. The site will therefore be directly accessible from localhost. The build part will define the MySQL installation process, which will be in the Dockerfile of the build/mysql directory. Two variables are declared and will be executed during the installation of MySQL. MYSQL_ROOT_PASSWORD sets the root account password, MYSQL_DATABASE the default database. It will be created if it does not exist when the container is launched. A volume is also created in order to make the data persistent, and thus not to lose the database data once the container is shut down.

Writing Dockerfiles

This established, we now need to write the two Dockerfiles, which will detail the installation procedures for the two servers. Are you ready ? Watch out, it’s going to be quick:

Dockerfile Apache-PHP

build/php/Dockerfile
FROM php:8.1-apache RUN apt-get update && \ docker-php-ext-install mysqli pdo pdo_mysql

What are we doing here?

We will simply tell Docker to fetch the existing php-apache image, which therefore contains apache and PHP at version 8.1.

Finally, we install the MySqli, PDO and PDO_MySQL extensions in PHP, which will be very useful for us to connect to our database.

Dockerfile Mysql

build/mysql/Dockerfile
FROM mysql:latest USER root RUN chmod 755 /var/lib/mysql

Here the same operation, we ask Docker to retrieve the latest version of MySQL from the existing image. Then, we make a change of rights on the “/var/lib/mysql” directory of the container that will be created, in order to allow PHP to connect to it.

The configuration is now finished (and yes, already!). We can still, before launching the build, add a simple index.php file in the “app” folder, just to check that everything works well. Without originality, I suggest this one:

app/index.php
<?php echo 'Hello world!';

Site building and testing

Now that all our files are present, we just have to launch the containers, and test the result. To do this, go to the root of the site, and simply run the command:

$ docker-compose up

https://doc4dev.com/en/create-a-web-site-php-apache-mysql-in-5-minutes-with-docker/

No hay comentarios:

Publicar un comentario