Docker Apache MySQL PHP MySQL Server – DAMP

Docker Apache MySQL PHP MySQL Server – DAMP

This post is meant to be a short introduction to creating a DAMP web server. DAMP is short for Docker, Apache, MySQL, PHP. You can use this DAMP server for development projects making it easy to build and test your applications or websites on your local machine.

Step 1 – Install Docker and Docker Compose
If you are on Windows or a Mac, you can download and install Docker from https://www.docker.com/products/docker-desktop

The Windows and Mac installation also includes Compose.

If you are running on a Linux platform your best bet is to follow the installation instructions for your platform

PlatformInstallation Instructions
CentOShttps://docs.docker.com/v17.12/install/linux/docker-ce/centos/
Debianhttps://docs.docker.com/v17.12/install/linux/docker-ce/debian/
Fedorahttps://docs.docker.com/v17.12/install/linux/docker-ce/fedora/
Ubuntuhttps://docs.docker.com/v17.12/install/linux/docker-ce/ubuntu/

If you are running on a linux platform you will also want to install docker-compose. Please check your installation instructions for installing docker compose. On Ubuntu is was as simple as running:

sudo apt-get install docker-compose

Step 2 – Create a Directory (folder) Structure
You are going to want to create a new directory for this project. I created one called phpserver. Once you create this directory you will create two more directories under it, once called phpdocker and another called php.

The phpdocker directory is going to hold a file containing instructions for building the php web server

the php folder will be your host your php files for your website. it is essentially the /var/www/html.

My folder layout looks something like this:

---home
     ---MyHomeDirectory
            ---phpserver
                   ---phpdocker
                   ---php

Step 3 – Create Your Dockerfile
Here’s where we are going to define our Docker image. In your phpserver directory create a plain text file with the name docker-compose.yml. Open the docker-compose.yml file and enter the following:

# ./docker-compose.yml
#20190214 - Initial Creation

version: "3.2"

services:
  db:
    image: mysql
    volumes:
      - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ARootPassword
      MYSQL_DATABASE: PhpServer
      MYSQL_USER: PhpUser
      MYSQL_PASSWORD: PhpPassword
    ports:
      - "9906:3306"

  web:
    build: ./phpdocker
    depends_on:
      - db
    ports:
      - "8888:80"
    volumes:
      - /{YOURFOLDER}/phpserver/php:/var/www/html
volumes:
    db_data: {}

Step 4 – Create a dockerfile for the Apache and PHP Server
After you have created the docker-compose.yml file you are going to create a dockerfile in the phpserver folder. This file tells docker how to build your php web server. Enter the phpserver folder and then create a text file called dockerfile. There is no extension to this file name (i.e. .txt). Open this new file for editing, enter the following contents, then save and exit the file:

FROM php:7.2-apache
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN docker-php-ext-enable mysqli

Step 5 – Create a php Page for Testing
The php folder is where you will put your files for your website. For now we will create a test file to make sure the web server is working properly. Make sure you are in the php folder. Create a file called index.php and enter the following contents:

<html>
<head>
<title>My New PHP Web Server</title>
</head>
<body>
 <?php
        echo"Hello, World!";
 ?>
</body>
</html>

Step 6 – Start your Docker Images
Return to your phpserver directory and issue the command:

sudo docker-compose up -d

If everything goes correctly your computer will start installing the images necessary to run you new containers. You should see a lot of things happening as it builds the images necessary to run an Apache and PHP server capable of talking to a MySQL server and also an image for MySQL.

Once the images are downloaded and built should be back at a shell or command prompt. Open your web browser and go to http://localhost:8888. If you get to a web page that says “Hello, World!” you have successfully built your images.