Container with Apache2 and PHP


At first, I couldn't find a Docker image that includes both Apache2 and PHP, so for this reason (and for learning purposes), I gathered the necessary information on how to create an appropriate container myself. This also has the advantage that I can add exactly the packages I need for the CMS I chose, GRAV.

To do this, we first create a directory called webcontainer, where we then place a docker-compose.yml file and a Dockerfile file:

mkdir webcontainer

cd webcontainer

bash -c "cat > docker-compose.yml" <<EOF
services:
  apache:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:80
    volumes:
      - /home/jgreven/Projects/IdeaProjects/ai-telligence.info:/var/www/html
      - /home/jgreven/Projects/IdeaProjects/ai-telligence.info/logs:/var/log/apache2
      - /home/jgreven/Projects/IdeaProjects/ai-telligence.info/docker_apache2.conf:/etc/apache2/apache2.conf
EOF

bash -c "cat > Dockerfile" <<EOF
FROM ubuntu:latest

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    apache2 \
    php8.3 \
    php8.3-curl \
    php8.3-gd \
    php8.3-imagick \
    php8.3-mbstring \
    php8.3-xml \
    php8.3-yaml \
    php8.3-zip \
    libapache2-mod-php8.3 \
    composer \
    git \
    less \
    mc \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN a2enmod php8.3
RUN a2enmod rewrite

EXPOSE 80

CMD ["apache2ctl", "-D", "FOREGROUND"]
EOF

bash -c "cat > docker_apache2_conf" <<EOF
DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

# These need to be set in /etc/apache2/envvars
# User ${APACHE_RUN_USER}
# Group ${APACHE_RUN_GROUP}
User ubuntu
Group ubuntu

HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
EOF

Of course, you need to adjust the path names according to your specific circumstances. You can then start the container with:

docker compose up --build -d

You can display the running containers with the command:

docker ps

You can ‘enter’ the container with the following command:

docker exec -it <CONTAINER-NAME> bash

Of course you have to adjust the name of the container. By the way, completion with the TAB key works here!