Back

My LAMP Docker Composer Environment for PHP Development

This is the docker compose I use when I'm developing PHP projects. The full stack is Ubuntu 20.04 Apache2, PHP 7.4, XDebug, MariaDB.

Requirements

1. Install Docker (http://docker.io).
2. Install [Docker-compose](http://docs.docker.com/compose/install/).
3. Clone this repository on my profile on GitHub.

Configure the build

All the parameters can be set in the .env file in the root of the project. You can set the URL and MariaDB environment variables in this file then the compose will build the containers accordingly.

URL=www.example.com
MYSQL_ROOT_PASSWORD=example
MYSQL_USER=example
MYSQL_DATABASE=example
MYSQL_PASSWORD=example

Remember to add the SSL certificates for your domain in the ./contextHttpd/certs, you can follow THIS guide to create a certification authority and self sign your own certificates.

random@checkmate:~/Documents/blog/lamp_docker/contextHttpd/cert$ ll
total 16
drwxr-xr-x  4 random  staff   128 Aug 26 22:00 ./
drwxr-xr-x  6 random  staff   192 Aug 27 17:42 ../
-rw-r--r--  1 random  staff  1610 Oct 18  2019 crt
-rw-r--r--  1 random  staff  1675 Oct 18  2019 key

Remember also to add the URL domain to your host file.

Under the Hood

The Apache Docker file is parametric, you can change the URL environment variable to match the domain you want to use, the Docker compose will build the Apache configuration and create the root directory of your website based on the URL you choose.

#APACHE2 CONF
RUN touch /etc/apache2/sites-available/$URL.conf
RUN echo "<VirtualHost *:443>" >> /etc/apache2/sites-available/$URL.conf
RUN echo "ServerName $URL" >> /etc/apache2/sites-available/$URL.conf
RUN echo "ServerAdmin webmaster@localhost" >> /etc/apache2/sites-available/$URL.conf
RUN echo "DocumentRoot /var/www/$URL" >> /etc/apache2/sites-available/$URL.conf
RUN echo "SSLEngine on" >> /etc/apache2/sites-available/$URL.conf
RUN echo "SSLCertificateFile /etc/apache2/ssl/crt" >> /etc/apache2/sites-available/$URL.conf
RUN echo "SSLCertificateKeyFile /etc/apache2/ssl/key" >> /etc/apache2/sites-available/$URL.conf
RUN echo "ErrorLog \${APACHE_LOG_DIR}/cp_error.log" >> /etc/apache2/sites-available/$URL.conf
RUN echo "CustomLog \${APACHE_LOG_DIR}/cp_access.log combined" >> /etc/apache2/sites-available/$URL.conf
RUN echo "</VirtualHost>" >> /etc/apache2/sites-available/$URL.conf

RUN mkdir /var/www/$URL
RUN chown -R www-data:www-data /var/www/$URL

The URL parameter is also used in the Compose file to automatically mount the website directory in the Apache2 container to the ./www directory, you also have the Apache2 log directory automatically mounted in the ./logs/apache2 directory.

version: '3'
services:
  httpd:
    build:
      dockerfile: Dockerfile
      context: contextHttpd
      args:
        URL : ${URL}
    image: httpd
    ports:
      - "443:443"
    volumes:
      - ./www:/var/www/${URL}
      - ./logs/apache2:/var/log/apache2
    depends_on:
      - mariadb

Get in touch_